Skip to content

Instantly share code, notes, and snippets.

@2called-chaos
Created October 13, 2014 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2called-chaos/25b097fe9e805699a0e7 to your computer and use it in GitHub Desktop.
Save 2called-chaos/25b097fe9e805699a0e7 to your computer and use it in GitHub Desktop.
Automatic Server Restart (MCL handler)
module Mcl
Mcl.reloadable(:AutomaticServerRestart)
class AutomaticServerRestart < Handler
def setup
@stop_at = 1.hour.from_now
end
def tick!
# only check every 240 MCL ticks == 60 seconds
# wouldn't really hurt to check every tick though
if app.eman.tick % 240 == 0
if @stop_at && @stop_at.past?
@stop_at = nil # don't call restart_server multiple times
restart_server
end
end
end
def restart_server
announce_server_restart
sleep 3 # also blocks MCL
# you could also invoke stop but be aware of
# https://bugs.mojang.com/browse/MC-63802
if true
$mcl_reboot = true
else
$mcl.server.invoke("/stop")
end
end
end
end
@2called-chaos
Copy link
Author

announce_server_restart is coming from here https://github.com/2called-chaos/mcl/blob/master/lib/mcl/handler/helper.rb#L38

why those methods get called: https://github.com/2called-chaos/mcl/blob/master/lib/mcl/handler/api.rb

why don't we have to reset @stop_at? Because MCL reboots when the server exits and therefore initializes all handlers again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment