Compare commits
2 Commits
9982a131e9
...
c61ffe2f75
| Author | SHA1 | Date | |
|---|---|---|---|
| c61ffe2f75 | |||
| c826d7237c |
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="TypeScriptCompiler">
|
||||||
|
<option name="useService" value="false" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
File diff suppressed because one or more lines are too long
@@ -24,6 +24,7 @@ end
|
|||||||
|
|
||||||
require_relative 'method_executor' # 日志系统在此时初始化
|
require_relative 'method_executor' # 日志系统在此时初始化
|
||||||
require_relative 'server_status_detector'
|
require_relative 'server_status_detector'
|
||||||
|
require_relative 'systemd_notifier' # 心跳系统在此时初始化
|
||||||
|
|
||||||
MethodExecutor.record("info", "mainloop", "MineSentinel started, entering mainloop.")
|
MethodExecutor.record("info", "mainloop", "MineSentinel started, entering mainloop.")
|
||||||
|
|
||||||
@@ -58,9 +59,13 @@ def main
|
|||||||
MethodExecutor.record("info", "mainloop", "Action: ForceResetServerProcess triggered.")
|
MethodExecutor.record("info", "mainloop", "Action: ForceResetServerProcess triggered.")
|
||||||
MethodExecutor.reset_minecraft_server
|
MethodExecutor.reset_minecraft_server
|
||||||
# 重启后等待服务器启动(MC服务器启动通常需要30秒~2分钟)
|
# 重启后等待服务器启动(MC服务器启动通常需要30秒~2分钟)
|
||||||
cooldown = 200 # 200秒冷却期
|
cooldown_duration = 200 # 200秒冷却期
|
||||||
|
cooldown_beg = Time.now
|
||||||
MethodExecutor.record("info", "mainloop", "Cooling down #{cooldown}s for server startup...")
|
MethodExecutor.record("info", "mainloop", "Cooling down #{cooldown}s for server startup...")
|
||||||
sleep cooldown
|
while Time.now < cooldown_beg + cooldown_duration
|
||||||
|
sleep 1
|
||||||
|
SystemdNotifier.notify("WATCHDOG=1")
|
||||||
|
end
|
||||||
MethodExecutor.record("info", "mainloop", "Cooldown finished, resuming monitoring.")
|
MethodExecutor.record("info", "mainloop", "Cooldown finished, resuming monitoring.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -70,5 +75,6 @@ end
|
|||||||
|
|
||||||
while true
|
while true
|
||||||
sleep 1.5
|
sleep 1.5
|
||||||
|
SystemdNotifier.notify("WATCHDOG=1")
|
||||||
main
|
main
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -121,4 +121,5 @@ class MethodExecutor
|
|||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class SystemdNotifier
|
||||||
|
require_relative 'method_executor'
|
||||||
|
public
|
||||||
|
|
||||||
|
@notify_socket = ENV["NOTIFY_SOCKET"]
|
||||||
|
@watchdog_usec = (ENV["WATCHDOG_USEC"] || '0').to_i
|
||||||
|
|
||||||
|
unless @notify_socket && File.socket?(@notify_socket)
|
||||||
|
MethodExecutor.record("error", "heartbeat", "NOTIFY_SOCKET is not set or is not a socket, am I a service?")
|
||||||
|
@enable_heartbeat = false
|
||||||
|
else
|
||||||
|
@enable_heartbeat = true
|
||||||
|
@socket = UNIXSocket.new(@notify_socket)
|
||||||
|
end
|
||||||
|
|
||||||
|
def enabled?
|
||||||
|
@enable_heartbeat
|
||||||
|
end
|
||||||
|
|
||||||
|
def notify_heartbeat(message)
|
||||||
|
return unless @enable_heartbeat
|
||||||
|
|
||||||
|
begin
|
||||||
|
@socket.send(message, 0)
|
||||||
|
rescue => e
|
||||||
|
MethodExecutor.error("heartbeat", "Failed to send heartbeat: #{e}")
|
||||||
|
@enable_heartbeat = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.notify(message)
|
||||||
|
MethodExecutor.record("info", "systemd", message)
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user