From d3bcf2e4366093049b5b372fbdbe14f902ae35be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Fri, 11 Dec 2015 18:51:23 +0100 Subject: Add automatic server shutdown Wrapper can be set to stop server if no player is online for certain time. --- mcwrapper | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'mcwrapper') diff --git a/mcwrapper b/mcwrapper index b260d6f..f758707 100755 --- a/mcwrapper +++ b/mcwrapper @@ -9,6 +9,7 @@ import datetime import traceback import atexit from threading import Thread +from threading import Timer import importlib.machinery as imp ################################################################################# # Load configuration @@ -50,6 +51,30 @@ if not 'verbose_level' in vars(conf): conf.verbose_level = 0 if not 'server' in vars(conf): conf.server = dict() +if not 'timeout' in vars(conf): + conf.timeout = 0 + +################################################################################# + +def autoshutdown_enable(): + global shutdownTimeout + if (conf.timeout > 0): + if (conf.verbose_level > 0): + print("Automatic shutdown after " + str(conf.timeout) + + " min.") + shutdownTimeout = Timer(conf.timeout * 60.0, __server_send_stop__) + shutdownTimeout.start(); + pass + +def autoshutdown_disable(): + global shutdownTimeout + try: + shutdownTimeout.cancel() + del shutdownTimeout + if (conf.verbose_level > 0): + print("Automatic shutdown disabled.") + except NameError: + pass ################################################################################# @@ -70,6 +95,7 @@ def __user_join__(username): with open(playersFile, 'a') as f: players.add(username) f.write(username + '\n') + autoshutdown_disable() def __user_leave__(username): global playerCount @@ -83,6 +109,8 @@ def __user_leave__(username): f.writelines(players) if players: f.write('\n') + if (not players): + autoshutdown_enable() def __server_start__(): if conf.verbose_level >= 0: @@ -130,6 +158,7 @@ def __parse_line__(line): print("Server start.") with open(statusFile, 'w') as f: f.write(__STATUSSTRINGS__[2] + '\n') + autoshutdown_enable() elif ': Stopping the server' in line: print("Server stop.") with open(statusFile, 'w') as f: -- cgit v1.2.3