mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 15:31:32 +00:00
Merge pull request #15 from avatao-content/pmgeh
Rework TFW process management capabilities
This commit is contained in:
commit
feb946a1bb
@ -25,8 +25,7 @@ class WebideReloadEventHandler(FileSystemEventHandler):
|
||||
if self._paused: return
|
||||
log.debug(event)
|
||||
key = 'webide'
|
||||
self.uplink.send(key, {'key': key,
|
||||
'data': {'command': 'reload'}})
|
||||
self.uplink.send(key, {'data': {'command': 'reload'}})
|
||||
|
||||
|
||||
class DirectoryMonitor:
|
||||
|
@ -8,13 +8,13 @@ from tfw.config import tfwenv
|
||||
class SupervisorMixin:
|
||||
supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor
|
||||
|
||||
def stop_process(self):
|
||||
def stop_process(self, process_name):
|
||||
with suppress(SupervisorFault):
|
||||
self.supervisor.stopProcess(self.process_name)
|
||||
self.supervisor.stopProcess(process_name)
|
||||
|
||||
def start_process(self):
|
||||
self.supervisor.startProcess(self.process_name)
|
||||
def start_process(self, process_name):
|
||||
self.supervisor.startProcess(process_name)
|
||||
|
||||
def restart_process(self):
|
||||
self.stop_process()
|
||||
self.start_process()
|
||||
def restart_process(self, process_name):
|
||||
self.stop_process(process_name)
|
||||
self.start_process(process_name)
|
||||
|
28
lib/tfw/components/process_managing_event_handler.py
Normal file
28
lib/tfw/components/process_managing_event_handler.py
Normal file
@ -0,0 +1,28 @@
|
||||
from tfw.event_handler_base import TriggerlessEventHandler
|
||||
from tfw.components.mixins import SupervisorMixin
|
||||
from tfw.networking.event_handlers.server_connector import ServerUplinkConnector
|
||||
from tfw.config.logs import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ProcessManager(SupervisorMixin):
|
||||
def __init__(self):
|
||||
self.commands = {'start': self.start_process,
|
||||
'stop': self.stop_process,
|
||||
'restart': self.restart_process}
|
||||
|
||||
def __call__(self, command, process_name):
|
||||
self.commands[command](process_name)
|
||||
|
||||
|
||||
class ProcessManagingEventHandler(TriggerlessEventHandler):
|
||||
def __init__(self, key):
|
||||
super().__init__(key)
|
||||
self.key = key
|
||||
self.processmanager = ProcessManager()
|
||||
self.uplink = ServerUplinkConnector()
|
||||
|
||||
def handle_event(self, key, data_json):
|
||||
data = data_json['data']
|
||||
self.processmanager(data['command'], data['process_name'])
|
||||
self.uplink.send(self.key, {'data': {'process_name': data['process_name']}})
|
@ -1,7 +1,6 @@
|
||||
from os.path import isfile, join, relpath
|
||||
from glob import glob
|
||||
|
||||
from tfw.components.mixins import SupervisorMixin
|
||||
from tfw.event_handler_base import TriggerlessEventHandler
|
||||
from tfw.components.directory_monitor import DirectoryMonitor
|
||||
|
||||
@ -43,11 +42,10 @@ class FileManager:
|
||||
return relpath(self._filepath(filename), start=self._workdir)
|
||||
|
||||
|
||||
class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
||||
def __init__(self, key, directory, process_name, selected_file=None):
|
||||
class SourceCodeEventHandler(TriggerlessEventHandler):
|
||||
def __init__(self, key, directory, selected_file=None):
|
||||
super().__init__(key)
|
||||
self.filemanager = FileManager(directory, selected_file=selected_file)
|
||||
self.process_name = process_name
|
||||
|
||||
self.commands = {
|
||||
'read': self.read,
|
||||
@ -69,7 +67,6 @@ class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
||||
with self.monitor.pauser:
|
||||
try: self.filemanager.file_contents = data['content']
|
||||
except Exception: log.exception('Error writing file!')
|
||||
self.restart_process()
|
||||
return data
|
||||
|
||||
def select(self, data):
|
||||
|
@ -10,7 +10,7 @@ class TerminadoEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
||||
super().__init__(key)
|
||||
self.working_directory = tfwenv.TERMINADO_DIR
|
||||
self.process_name = process_name
|
||||
self.start_process()
|
||||
self.start_process(self.process_name)
|
||||
|
||||
def handle_event(self, key, data_json):
|
||||
log.debug('TerminadoEventHandler received event for key {}'.format(key))
|
||||
|
@ -14,8 +14,5 @@ class MessageSender:
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'message': message
|
||||
}
|
||||
response = {
|
||||
'key': self.key,
|
||||
'data': data
|
||||
}
|
||||
response = {'data': data}
|
||||
self.server_connector.send(self.key, response)
|
||||
|
@ -26,6 +26,7 @@ class ServerUplinkConnector(ZMQConnectorBase):
|
||||
self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT))
|
||||
|
||||
def send(self, key, response):
|
||||
response['key'] = key
|
||||
self._zmq_push_socket.send_multipart(serialize_all(key, response))
|
||||
|
||||
|
||||
|
@ -82,8 +82,7 @@ class FSMUpdater:
|
||||
|
||||
def generate_fsm_update(self):
|
||||
key = 'FSMUpdate'
|
||||
response = {'key': key,
|
||||
'data': {'current_state': self.fsm.state,
|
||||
response = {'data': {'current_state': self.fsm.state,
|
||||
'valid_transitions':
|
||||
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}}
|
||||
return key, response
|
||||
|
@ -1,13 +1,15 @@
|
||||
from tfw.components.source_code_event_handler import SourceCodeEventHandler
|
||||
from tfw.components.terminado_event_handler import TerminadoEventHandler
|
||||
from tfw.components.process_managing_event_handler import ProcessManagingEventHandler
|
||||
from tornado.ioloop import IOLoop
|
||||
|
||||
from tfw.config import tfwenv
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD, 'login'),
|
||||
TerminadoEventHandler('terminado', 'terminado')}
|
||||
eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD),
|
||||
TerminadoEventHandler('terminado', 'terminado'),
|
||||
ProcessManagingEventHandler('processmanager')}
|
||||
try:
|
||||
IOLoop.instance().start()
|
||||
finally:
|
||||
|
Loading…
Reference in New Issue
Block a user