Make cleanup() calls automatic in event_handler_main.py

This commit is contained in:
Kristóf Tóth 2018-08-07 17:01:58 +02:00
parent 829c244867
commit 3bda7bc540
1 changed files with 25 additions and 9 deletions

View File

@ -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()