Make --log operate simultaneously to other commands

This commit is contained in:
Griatch 2018-01-20 00:06:34 +01:00
parent a2d63c9582
commit 9460614150

View file

@ -75,7 +75,9 @@ PPROFILER_LOGFILE = None
TEST_MODE = False TEST_MODE = False
ENFORCED_SETTING = False ENFORCED_SETTING = False
TAIL_LOG_MODE = False
REACTOR_RUN = False
NO_REACTOR_STOP = False
# communication constants # communication constants
@ -557,12 +559,17 @@ class AMPLauncherProtocol(amp.AMP):
return {"status": ""} return {"status": ""}
def _reactor_stop():
if not NO_REACTOR_STOP:
reactor.stop()
def send_instruction(operation, arguments, callback=None, errback=None): def send_instruction(operation, arguments, callback=None, errback=None):
""" """
Send instruction and handle the response. Send instruction and handle the response.
""" """
global AMP_CONNECTION global AMP_CONNECTION, REACTOR_RUN
if None in (AMP_HOST, AMP_PORT, AMP_INTERFACE): if None in (AMP_HOST, AMP_PORT, AMP_INTERFACE):
print(ERROR_AMP_UNCONFIGURED) print(ERROR_AMP_UNCONFIGURED)
@ -610,9 +617,7 @@ def send_instruction(operation, arguments, callback=None, errback=None):
point = endpoints.TCP4ClientEndpoint(reactor, AMP_HOST, AMP_PORT) point = endpoints.TCP4ClientEndpoint(reactor, AMP_HOST, AMP_PORT)
deferred = endpoints.connectProtocol(point, AMPLauncherProtocol()) deferred = endpoints.connectProtocol(point, AMPLauncherProtocol())
deferred.addCallbacks(_on_connect, _on_connect_fail) deferred.addCallbacks(_on_connect, _on_connect_fail)
if not reactor.running: REACTOR_RUN = True
reactor.run()
def _parse_status(response): def _parse_status(response):
"Unpack the status information" "Unpack the status information"
@ -661,12 +666,12 @@ def query_status(callback=None):
pstatus, sstatus, ppid, spid, pinfo, sinfo = _parse_status(response) pstatus, sstatus, ppid, spid, pinfo, sinfo = _parse_status(response)
print("Portal: {} (pid {})\nServer: {} (pid {})".format( print("Portal: {} (pid {})\nServer: {} (pid {})".format(
wmap[pstatus], ppid, wmap[sstatus], spid)) wmap[pstatus], ppid, wmap[sstatus], spid))
reactor.stop() _reactor_stop()
def _errback(fail): def _errback(fail):
pstatus, sstatus = False, False pstatus, sstatus = False, False
print("Portal: {}\nServer: {}".format(wmap[pstatus], wmap[sstatus])) print("Portal: {}\nServer: {}".format(wmap[pstatus], wmap[sstatus]))
reactor.stop() _reactor_stop()
send_instruction(PSTATUS, None, _callback, _errback) send_instruction(PSTATUS, None, _callback, _errback)
@ -705,14 +710,14 @@ def wait_for_status(portal_running=True, server_running=True, callback=None, err
if callback: if callback:
callback(prun, srun) callback(prun, srun)
else: else:
reactor.stop() _reactor_stop()
else: else:
if retries <= 0: if retries <= 0:
if errback: if errback:
errback(prun, srun) errback(prun, srun)
else: else:
print("Connection to Evennia timed out. Try again.") print("Connection to Evennia timed out. Try again.")
reactor.stop() _reactor_stop()
else: else:
reactor.callLater(rate, wait_for_status, reactor.callLater(rate, wait_for_status,
portal_running, server_running, portal_running, server_running,
@ -727,14 +732,14 @@ def wait_for_status(portal_running=True, server_running=True, callback=None, err
if callback: if callback:
callback(portal_running, server_running) callback(portal_running, server_running)
else: else:
reactor.stop() _reactor_stop()
else: else:
if retries <= 0: if retries <= 0:
if errback: if errback:
errback(portal_running, server_running) errback(portal_running, server_running)
else: else:
print("Connection to Evennia timed out. Try again.") print("Connection to Evennia timed out. Try again.")
reactor.stop() _reactor_stop()
else: else:
reactor.callLater(rate, wait_for_status, reactor.callLater(rate, wait_for_status,
portal_running, server_running, portal_running, server_running,
@ -759,14 +764,14 @@ def start_evennia(pprofiler=False, sprofiler=False):
def _fail(fail): def _fail(fail):
print(fail) print(fail)
reactor.stop() _reactor_stop()
def _server_started(response): def _server_started(response):
print("... Server started.\nEvennia running.") print("... Server started.\nEvennia running.")
if response: if response:
_, _, _, _, pinfo, sinfo = response _, _, _, _, pinfo, sinfo = response
print_info(pinfo, sinfo) print_info(pinfo, sinfo)
reactor.stop() _reactor_stop()
def _portal_started(*args): def _portal_started(*args):
print("... Portal started.\nServer starting {} ...".format( print("... Portal started.\nServer starting {} ...".format(
@ -779,7 +784,7 @@ def start_evennia(pprofiler=False, sprofiler=False):
print("Portal is already running as process {pid}. Not restarted.".format(pid=ppid)) print("Portal is already running as process {pid}. Not restarted.".format(pid=ppid))
if srun: if srun:
print("Server is already running as process {pid}. Not restarted.".format(pid=spid)) print("Server is already running as process {pid}. Not restarted.".format(pid=spid))
reactor.stop() _reactor_stop()
else: else:
print("Server starting {}...".format("(under cProfile)" if sprofiler else "")) print("Server starting {}...".format("(under cProfile)" if sprofiler else ""))
send_instruction(SSTART, server_cmd, _server_started, _fail) send_instruction(SSTART, server_cmd, _server_started, _fail)
@ -790,7 +795,7 @@ def start_evennia(pprofiler=False, sprofiler=False):
Popen(portal_cmd, env=getenv(), bufsize=-1) Popen(portal_cmd, env=getenv(), bufsize=-1)
except Exception as e: except Exception as e:
print(PROCESS_ERROR.format(component="Portal", traceback=e)) print(PROCESS_ERROR.format(component="Portal", traceback=e))
reactor.stop() _reactor_stop()
wait_for_status(True, None, _portal_started) wait_for_status(True, None, _portal_started)
send_instruction(PSTATUS, None, _portal_running, _portal_not_running) send_instruction(PSTATUS, None, _portal_running, _portal_not_running)
@ -808,11 +813,11 @@ def reload_evennia(sprofiler=False, reset=False):
def _server_restarted(*args): def _server_restarted(*args):
print("... Server re-started.") print("... Server re-started.")
reactor.stop() _reactor_stop()
def _server_reloaded(status): def _server_reloaded(status):
print("... Server {}.".format("reset" if reset else "reloaded")) print("... Server {}.".format("reset" if reset else "reloaded"))
reactor.stop() _reactor_stop()
def _server_stopped(status): def _server_stopped(status):
wait_for_status_reply(_server_reloaded) wait_for_status_reply(_server_reloaded)
@ -844,7 +849,7 @@ def stop_evennia():
""" """
def _portal_stopped(*args): def _portal_stopped(*args):
print("... Portal stopped.\nEvennia shut down.") print("... Portal stopped.\nEvennia shut down.")
reactor.stop() _reactor_stop()
def _server_stopped(*args): def _server_stopped(*args):
print("... Server stopped.\nStopping Portal ...") print("... Server stopped.\nStopping Portal ...")
@ -864,7 +869,7 @@ def stop_evennia():
def _portal_not_running(fail): def _portal_not_running(fail):
print("Evennia is not running.") print("Evennia is not running.")
reactor.stop() _reactor_stop()
send_instruction(PSTATUS, None, _portal_running, _portal_not_running) send_instruction(PSTATUS, None, _portal_running, _portal_not_running)
@ -876,7 +881,7 @@ def stop_server_only():
""" """
def _server_stopped(*args): def _server_stopped(*args):
print("... Server stopped.") print("... Server stopped.")
reactor.stop() _reactor_stop()
def _portal_running(response): def _portal_running(response):
_, srun, _, _, _, _ = _parse_status(response) _, srun, _, _, _, _ = _parse_status(response)
@ -886,11 +891,11 @@ def stop_server_only():
send_instruction(SSHUTD, {}) send_instruction(SSHUTD, {})
else: else:
print("Server is not running.") print("Server is not running.")
reactor.stop() _reactor_stop()
def _portal_not_running(fail): def _portal_not_running(fail):
print("Evennia is not running.") print("Evennia is not running.")
reactor.stop() _reactor_stop()
send_instruction(PSTATUS, None, _portal_running, _portal_not_running) send_instruction(PSTATUS, None, _portal_running, _portal_not_running)
@ -903,7 +908,7 @@ def query_info():
def _got_status(status): def _got_status(status):
_, _, _, _, pinfo, sinfo = _parse_status(status) _, _, _, _, pinfo, sinfo = _parse_status(status)
print_info(pinfo, sinfo) print_info(pinfo, sinfo)
reactor.stop() _reactor_stop()
def _portal_running(response): def _portal_running(response):
query_status(_got_status) query_status(_got_status)
@ -926,6 +931,8 @@ def tail_server_log(filename, rate=1):
rate (int, optional): How often to poll the log file. rate (int, optional): How often to poll the log file.
""" """
global REACTOR_RUN
def _file_changed(filename, prev_size): def _file_changed(filename, prev_size):
"Get size of file in bytes, get diff compared with previous size" "Get size of file in bytes, get diff compared with previous size"
new_size = os.path.getsize(filename) new_size = os.path.getsize(filename)
@ -991,7 +998,6 @@ def tail_server_log(filename, rate=1):
else: else:
if max_lines: if max_lines:
# first startup # first startup
print(" Tailing logfile {} ...".format(filename))
new_lines = new_lines[-max_lines:] new_lines = new_lines[-max_lines:]
# print to stdout without line break (log has its own line feeds) # print to stdout without line break (log has its own line feeds)
@ -1002,7 +1008,7 @@ def tail_server_log(filename, rate=1):
reactor.callLater(rate, _tail_file, filename, file_size, line_count, max_lines=100) reactor.callLater(rate, _tail_file, filename, file_size, line_count, max_lines=100)
reactor.callLater(0, _tail_file, filename, 0, 0, max_lines=20) reactor.callLater(0, _tail_file, filename, 0, 0, max_lines=20)
reactor.run() REACTOR_RUN = True
# ------------------------------------------------------------ # ------------------------------------------------------------
@ -1669,8 +1675,6 @@ def main():
Run the evennia launcher main program. Run the evennia launcher main program.
""" """
global TAIL_LOG_MODE
# set up argument parser # set up argument parser
parser = ArgumentParser(description=CMDLINE_HELP, formatter_class=argparse.RawTextHelpFormatter) parser = ArgumentParser(description=CMDLINE_HELP, formatter_class=argparse.RawTextHelpFormatter)
@ -1724,8 +1728,6 @@ def main():
# make sure we have everything # make sure we have everything
check_main_evennia_dependencies() check_main_evennia_dependencies()
TAIL_LOG_MODE = args.tail_log
if not args: if not args:
# show help pane # show help pane
print(CMDLINE_HELP) print(CMDLINE_HELP)
@ -1755,8 +1757,8 @@ def main():
if args.altsettings: if args.altsettings:
# use alternative settings file # use alternative settings file
sfile = args.altsettings[0]
global SETTINGSFILE, SETTINGS_DOTPATH, ENFORCED_SETTING global SETTINGSFILE, SETTINGS_DOTPATH, ENFORCED_SETTING
sfile = args.altsettings[0]
SETTINGSFILE = sfile SETTINGSFILE = sfile
ENFORCED_SETTING = True ENFORCED_SETTING = True
SETTINGS_DOTPATH = "server.conf.%s" % sfile.rstrip(".py") SETTINGS_DOTPATH = "server.conf.%s" % sfile.rstrip(".py")
@ -1772,6 +1774,15 @@ def main():
print(ERROR_INITSETTINGS) print(ERROR_INITSETTINGS)
sys.exit() sys.exit()
if args.tail_log:
# set up for tailing the server log file
global NO_REACTOR_STOP
NO_REACTOR_STOP = True
if not SERVER_LOGFILE:
init_game_directory(CURRENT_DIR, check_db=False)
tail_server_log(SERVER_LOGFILE)
print(" Tailing logfile {} ...".format(SERVER_LOGFILE))
if args.dummyrunner: if args.dummyrunner:
# launch the dummy runner # launch the dummy runner
init_game_directory(CURRENT_DIR, check_db=True) init_game_directory(CURRENT_DIR, check_db=True)
@ -1786,7 +1797,8 @@ def main():
run_menu() run_menu()
elif option in ('status', 'info', 'start', 'reload', 'reset', 'stop', 'sstop', 'kill', 'skill'): elif option in ('status', 'info', 'start', 'reload', 'reset', 'stop', 'sstop', 'kill', 'skill'):
# operate the server directly # operate the server directly
init_game_directory(CURRENT_DIR, check_db=True) if not SERVER_LOGFILE:
init_game_directory(CURRENT_DIR, check_db=True)
if option == "status": if option == "status":
query_status() query_status()
elif option == "info": elif option == "info":
@ -1844,17 +1856,12 @@ def main():
kwargs = ", ".join(["--%s" % kw for kw in kwargs]) kwargs = ", ".join(["--%s" % kw for kw in kwargs])
print(ERROR_INPUT.format(traceback=exc, args=args, kwargs=kwargs)) print(ERROR_INPUT.format(traceback=exc, args=args, kwargs=kwargs))
elif not TAIL_LOG_MODE: elif not args.tail_log:
# no input; print evennia info # no input; print evennia info (don't pring if we're tailing log)
print(ABOUT_INFO) print(ABOUT_INFO)
if TAIL_LOG_MODE: if REACTOR_RUN:
# start the log-tail last reactor.run()
if not SERVER_LOGFILE:
init_game_directory(CURRENT_DIR, check_db=False)
tail_server_log(SERVER_LOGFILE)
if __name__ == '__main__': if __name__ == '__main__':