mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-23 01:11:33 +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
|
if self._paused: return
|
||||||
log.debug(event)
|
log.debug(event)
|
||||||
key = 'webide'
|
key = 'webide'
|
||||||
self.uplink.send(key, {'key': key,
|
self.uplink.send(key, {'data': {'command': 'reload'}})
|
||||||
'data': {'command': 'reload'}})
|
|
||||||
|
|
||||||
|
|
||||||
class DirectoryMonitor:
|
class DirectoryMonitor:
|
||||||
|
@ -8,13 +8,13 @@ from tfw.config import tfwenv
|
|||||||
class SupervisorMixin:
|
class SupervisorMixin:
|
||||||
supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor
|
supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor
|
||||||
|
|
||||||
def stop_process(self):
|
def stop_process(self, process_name):
|
||||||
with suppress(SupervisorFault):
|
with suppress(SupervisorFault):
|
||||||
self.supervisor.stopProcess(self.process_name)
|
self.supervisor.stopProcess(process_name)
|
||||||
|
|
||||||
def start_process(self):
|
def start_process(self, process_name):
|
||||||
self.supervisor.startProcess(self.process_name)
|
self.supervisor.startProcess(process_name)
|
||||||
|
|
||||||
def restart_process(self):
|
def restart_process(self, process_name):
|
||||||
self.stop_process()
|
self.stop_process(process_name)
|
||||||
self.start_process()
|
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 os.path import isfile, join, relpath
|
||||||
from glob import glob
|
from glob import glob
|
||||||
|
|
||||||
from tfw.components.mixins import SupervisorMixin
|
|
||||||
from tfw.event_handler_base import TriggerlessEventHandler
|
from tfw.event_handler_base import TriggerlessEventHandler
|
||||||
from tfw.components.directory_monitor import DirectoryMonitor
|
from tfw.components.directory_monitor import DirectoryMonitor
|
||||||
|
|
||||||
@ -43,11 +42,10 @@ class FileManager:
|
|||||||
return relpath(self._filepath(filename), start=self._workdir)
|
return relpath(self._filepath(filename), start=self._workdir)
|
||||||
|
|
||||||
|
|
||||||
class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
class SourceCodeEventHandler(TriggerlessEventHandler):
|
||||||
def __init__(self, key, directory, process_name, selected_file=None):
|
def __init__(self, key, directory, selected_file=None):
|
||||||
super().__init__(key)
|
super().__init__(key)
|
||||||
self.filemanager = FileManager(directory, selected_file=selected_file)
|
self.filemanager = FileManager(directory, selected_file=selected_file)
|
||||||
self.process_name = process_name
|
|
||||||
|
|
||||||
self.commands = {
|
self.commands = {
|
||||||
'read': self.read,
|
'read': self.read,
|
||||||
@ -69,7 +67,6 @@ class SourceCodeEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
|||||||
with self.monitor.pauser:
|
with self.monitor.pauser:
|
||||||
try: self.filemanager.file_contents = data['content']
|
try: self.filemanager.file_contents = data['content']
|
||||||
except Exception: log.exception('Error writing file!')
|
except Exception: log.exception('Error writing file!')
|
||||||
self.restart_process()
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def select(self, data):
|
def select(self, data):
|
||||||
|
@ -10,7 +10,7 @@ class TerminadoEventHandler(TriggerlessEventHandler, SupervisorMixin):
|
|||||||
super().__init__(key)
|
super().__init__(key)
|
||||||
self.working_directory = tfwenv.TERMINADO_DIR
|
self.working_directory = tfwenv.TERMINADO_DIR
|
||||||
self.process_name = process_name
|
self.process_name = process_name
|
||||||
self.start_process()
|
self.start_process(self.process_name)
|
||||||
|
|
||||||
def handle_event(self, key, data_json):
|
def handle_event(self, key, data_json):
|
||||||
log.debug('TerminadoEventHandler received event for key {}'.format(key))
|
log.debug('TerminadoEventHandler received event for key {}'.format(key))
|
||||||
|
@ -14,8 +14,5 @@ class MessageSender:
|
|||||||
'timestamp': datetime.now().isoformat(),
|
'timestamp': datetime.now().isoformat(),
|
||||||
'message': message
|
'message': message
|
||||||
}
|
}
|
||||||
response = {
|
response = {'data': data}
|
||||||
'key': self.key,
|
|
||||||
'data': data
|
|
||||||
}
|
|
||||||
self.server_connector.send(self.key, response)
|
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))
|
self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT))
|
||||||
|
|
||||||
def send(self, key, response):
|
def send(self, key, response):
|
||||||
|
response['key'] = key
|
||||||
self._zmq_push_socket.send_multipart(serialize_all(key, response))
|
self._zmq_push_socket.send_multipart(serialize_all(key, response))
|
||||||
|
|
||||||
|
|
||||||
|
@ -82,8 +82,7 @@ class FSMUpdater:
|
|||||||
|
|
||||||
def generate_fsm_update(self):
|
def generate_fsm_update(self):
|
||||||
key = 'FSMUpdate'
|
key = 'FSMUpdate'
|
||||||
response = {'key': key,
|
response = {'data': {'current_state': self.fsm.state,
|
||||||
'data': {'current_state': self.fsm.state,
|
|
||||||
'valid_transitions':
|
'valid_transitions':
|
||||||
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}}
|
[{'trigger': trigger} for trigger in self.fsm.machine.get_triggers(self.fsm.state)]}}
|
||||||
return key, response
|
return key, response
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
from tfw.components.source_code_event_handler import SourceCodeEventHandler
|
from tfw.components.source_code_event_handler import SourceCodeEventHandler
|
||||||
from tfw.components.terminado_event_handler import TerminadoEventHandler
|
from tfw.components.terminado_event_handler import TerminadoEventHandler
|
||||||
|
from tfw.components.process_managing_event_handler import ProcessManagingEventHandler
|
||||||
from tornado.ioloop import IOLoop
|
from tornado.ioloop import IOLoop
|
||||||
|
|
||||||
from tfw.config import tfwenv
|
from tfw.config import tfwenv
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD, 'login'),
|
eventhandlers = {SourceCodeEventHandler('webide', tfwenv.WEBIDE_WD),
|
||||||
TerminadoEventHandler('terminado', 'terminado')}
|
TerminadoEventHandler('terminado', 'terminado'),
|
||||||
|
ProcessManagingEventHandler('processmanager')}
|
||||||
try:
|
try:
|
||||||
IOLoop.instance().start()
|
IOLoop.instance().start()
|
||||||
finally:
|
finally:
|
||||||
|
Loading…
Reference in New Issue
Block a user