Files
sentinel/main.rb
T

88 lines
3.4 KiB
Ruby
Raw Normal View History

2026-06-07 11:05:26 +08:00
# <<*>> -^v- SNOWARE APPLICATION
# usage: MINESENTINEL mainloop.
# author: S.A.
# time: 2026-06-07
2026-06-07 19:35:17 +08:00
# Copyright (c) [2026] [S.A. SNOWARE-SCU]
# [MineSentinel] is licensed under Mulan PubL v2.
# You can use this software according to the terms and conditions of the Mulan PubL v2.
# You may obtain a copy of Mulan PubL v2 at:
# http://license.coscl.org.cn/MulanPubL-2.0
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PubL v2 for more details.
2026-06-07 11:05:26 +08:00
# main.rb 顶部添加
begin
# 原有代码
rescue => e
# 强制输出到stderr
$stderr.puts "[CRITICAL] Unhandled exception: #{e.message}"
$stderr.puts e.backtrace.join("\n")
exit 1
end
2026-06-08 08:26:28 +08:00
puts ' ___ __ ___ ___ ___ '
puts ' |\/| | |\ | |__ /__` |__ |\ | | | |\ | |__ | '
puts ' | | | | \| |___ .__/ |___ | \| | | | \| |___ |___ '
puts " MineSentinel dev-rolling version by S.A. [@Snoware] and owned by SCU team."
puts " This program is licensed under Mulan PubL v2."
puts " E-mail the author for more information: SALflake@qq.com or visit https://https://swe-iss.rth1.xyz/softwares/minesentinel."
2026-06-07 11:05:26 +08:00
require_relative 'method_executor' # 日志系统在此时初始化
require_relative 'server_status_detector'
require_relative 'systemd_notifier' # 心跳系统在此时初始化
2026-06-07 11:05:26 +08:00
MethodExecutor.record("info", "mainloop", "MineSentinel started, entering mainloop.")
2026-06-07 19:35:17 +08:00
# 初次启动:MineSentinel 先于 minecraft 启动(systemd Before=),
# 等待服务器端口就绪,避免误判为崩溃
INITIAL_GRACE = 300 # 初次启动等待 300 秒
MethodExecutor.record("info", "mainloop",
"Initial startup grace period #{INITIAL_GRACE}s — waiting for server to come up...")
grace_deadline = Time.now + INITIAL_GRACE
while Time.now < grace_deadline
sleep 5
if ServerStatusDetector::MinecraftServer.self_diag[:type]
MethodExecutor.record("info", "mainloop",
"Server port is open during grace period, resuming normal monitoring.")
break
end
end
MethodExecutor.record("info", "mainloop", "Grace period ended.")
2026-06-07 11:05:26 +08:00
# Functional mainloop.
def main
chkrslt = ServerStatusDetector::MinecraftServer.self_diag
if chkrslt[:type]
MethodExecutor.record("debug", "mainloop", "Server health check passed.")
else
MethodExecutor.record("warn", "mainloop", "Server health check FAILED: #{chkrslt[:message]}")
for operation in chkrslt[:operation]
if operation == "Record"
# 消息已在 self_diag 中生成,这里仅记录操作
MethodExecutor.record("warn", "mainloop", "Action: Record -> Server died!")
elsif operation == "ForceResetServerProcess"
MethodExecutor.record("info", "mainloop", "Action: ForceResetServerProcess triggered.")
MethodExecutor.reset_minecraft_server
2026-06-07 13:10:15 +08:00
# 重启后等待服务器启动(MC服务器启动通常需要30秒~2分钟)
cooldown_duration = 200 # 200秒冷却期
cooldown_beg = Time.now
2026-06-07 13:10:15 +08:00
MethodExecutor.record("info", "mainloop", "Cooling down #{cooldown}s for server startup...")
while Time.now < cooldown_beg + cooldown_duration
sleep 1
SystemdNotifier.notify("WATCHDOG=1")
end
2026-06-07 13:10:15 +08:00
MethodExecutor.record("info", "mainloop", "Cooldown finished, resuming monitoring.")
2026-06-07 11:05:26 +08:00
end
end
end
end
while true
sleep 1.5
SystemdNotifier.notify("WATCHDOG=1")
2026-06-07 11:05:26 +08:00
main
end