2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-03-07 14:14:33 +00:00
|
|
|
from xmlrpc.client import Fault as SupervisorFault
|
2018-03-02 09:21:56 +00:00
|
|
|
|
2018-04-18 16:47:51 +00:00
|
|
|
from tfw import EventHandlerBase
|
2018-05-28 12:32:11 +00:00
|
|
|
from tfw.mixins import SupervisorMixin, SupervisorLogMixin
|
2018-02-27 13:11:13 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-04-06 13:21:45 +00:00
|
|
|
from .directory_monitor import with_monitor_paused
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-02-16 11:07:16 +00:00
|
|
|
|
|
|
|
|
2018-05-28 12:32:11 +00:00
|
|
|
class ProcessManager(SupervisorMixin, SupervisorLogMixin):
|
2018-02-27 15:51:10 +00:00
|
|
|
def __init__(self):
|
2018-06-04 20:16:44 +00:00
|
|
|
self.commands = {
|
|
|
|
'start': self.start_process,
|
|
|
|
'stop': self.stop_process,
|
|
|
|
'restart': self.restart_process
|
|
|
|
}
|
2018-02-20 13:45:16 +00:00
|
|
|
|
2018-02-27 14:54:31 +00:00
|
|
|
def __call__(self, command, process_name):
|
2018-03-08 15:04:35 +00:00
|
|
|
return self.commands[command](process_name)
|
2018-02-20 13:45:16 +00:00
|
|
|
|
|
|
|
|
2018-04-18 16:47:51 +00:00
|
|
|
class ProcessManagingEventHandler(EventHandlerBase):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Event handler that can manage processes managed by supervisor.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
This EventHandler accepts messages that have a data['command'] key specifying
|
2018-04-18 17:44:26 +00:00
|
|
|
a command to be executed.
|
2018-06-01 14:20:20 +00:00
|
|
|
Every message must contain a data['process_name'] field with the name of the
|
2018-04-18 17:44:26 +00:00
|
|
|
process to manage. This is the name specified in supervisor config files like so:
|
|
|
|
[program:someprogram]
|
|
|
|
|
|
|
|
Commands available: start, stop, restart, readlog
|
|
|
|
(the names are as self-documenting as it gets)
|
|
|
|
"""
|
2018-05-29 15:59:16 +00:00
|
|
|
def __init__(self, key, dirmonitor=None, log_tail=0):
|
2018-02-27 13:11:13 +00:00
|
|
|
super().__init__(key)
|
2018-02-27 14:26:16 +00:00
|
|
|
self.key = key
|
2018-03-02 09:21:56 +00:00
|
|
|
self.monitor = dirmonitor
|
2018-02-27 15:51:10 +00:00
|
|
|
self.processmanager = ProcessManager()
|
2018-05-29 15:59:16 +00:00
|
|
|
self.log_tail = log_tail
|
2018-02-16 11:07:16 +00:00
|
|
|
|
2018-03-21 16:18:16 +00:00
|
|
|
@with_monitor_paused
|
2018-04-13 18:45:34 +00:00
|
|
|
def handle_event(self, message):
|
2018-03-07 13:45:43 +00:00
|
|
|
try:
|
2018-03-08 15:11:43 +00:00
|
|
|
data = message['data']
|
2018-05-28 13:02:53 +00:00
|
|
|
try:
|
|
|
|
self.processmanager(data['command'], data['process_name'])
|
|
|
|
except SupervisorFault as fault:
|
|
|
|
message['data']['error'] = fault.faultString
|
|
|
|
finally:
|
2018-06-04 20:16:44 +00:00
|
|
|
message['data']['stdout'] = self.processmanager.read_stdout(
|
|
|
|
data['process_name'],
|
|
|
|
self.log_tail
|
|
|
|
)
|
|
|
|
message['data']['stderr'] = self.processmanager.read_stderr(
|
|
|
|
data['process_name'],
|
|
|
|
self.log_tail
|
|
|
|
)
|
2018-05-29 09:05:45 +00:00
|
|
|
return message
|
2018-03-07 13:45:43 +00:00
|
|
|
except KeyError:
|
2018-03-25 14:25:01 +00:00
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|