From 3bda7bc5403e0b7211ce8abc5cf08df9ea4ba957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 7 Aug 2018 17:01:58 +0200 Subject: [PATCH] Make cleanup() calls automatic in event_handler_main.py --- solvable/src/event_handler_main.py | 34 ++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index a3639e2..4b0b564 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -1,10 +1,12 @@ from ast import literal_eval from functools import partial +from signal import signal, SIGTERM, SIGINT, SIGHUP +from sys import exit as sysexit from tornado.ioloop import IOLoop from tfw.fsm import YamlFSM -from tfw.event_handler_base import FSMAwareEventHandler +from tfw.event_handler_base import EventHandlerBase, FSMAwareEventHandler from tfw.components import IdeEventHandler, TerminalEventHandler from tfw.components import ProcessManagingEventHandler, BashMonitor from tfw.components import TerminalCommands, LogMonitoringEventHandler @@ -99,9 +101,11 @@ class MessageFSMStepsEventHandler(FSMAwareEventHandler): ) -if __name__ == '__main__': +def main(): + # pylint: disable=possibly-unused-variable + # # TFW component EventHandlers (builtins, required for their respective functionalities) - fsm = FSMManagingEventHandler( # TFW FSM + fsm = FSMManagingEventHandler( # TFW FSM key='fsm', fsm_type=partial( YamlFSM, @@ -131,20 +135,32 @@ if __name__ == '__main__': ) # Your custom event handlers - message_fsm_steps = MessageFSMStepsEventHandler( + message_fsm_steps_eh = MessageFSMStepsEventHandler( key='test' ) # Terminal command handlers commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc') terminal.historymonitor.subscribe_callback(commands.callback) - - # Example terminal command callback terminal.historymonitor.subscribe_callback(cenator) + # Raise SystemExit on signals we should handle gracefully (make finally run) + trigger_finally = lambda a, b: sysexit() + signal(SIGTERM, trigger_finally) + signal(SIGINT, trigger_finally) + signal(SIGHUP, trigger_finally) + + # Start event handlers and clean up on stopping try: IOLoop.instance().start() finally: - eventhandlers = {fsm, ide, terminal, processmanager, logmonitor, message_fsm_steps} - for eh in eventhandlers: - eh.cleanup() + event_handlers = ( + eh for eh in locals().values() + if isinstance(eh, EventHandlerBase) + ) + for event_handler in event_handlers: + event_handler.cleanup() + + +if __name__ == '__main__': + main()