Extract process managing logic to SupervisorMixin from event handlers

This commit is contained in:
Kristóf Tóth 2018-02-06 15:34:43 +01:00
parent 8b9f7e50cb
commit c74c00c618
3 changed files with 17 additions and 9 deletions

View File

@ -1,4 +1,6 @@
import xmlrpc.client, zmq
from contextlib import suppress
from xmlrpc.client import Fault as SupervisorFault
from .config.envvars import SUPERVISOR_HTTP_URI
@ -14,6 +16,17 @@ def create_source_code_response_data(filename, content, language):
class SupervisorMixin:
supervisor = xmlrpc.client.ServerProxy(SUPERVISOR_HTTP_URI).supervisor
def stop_process(self):
with suppress(SupervisorFault):
self.supervisor.stopProcess(self.process_name)
def start_process(self):
self.supervisor.startProcess(self.process_name)
def restart_process(self):
self.stop_process()
self.start_process()
class ZMQConnectorBase:
def __init__(self, zmq_context=None):

View File

@ -1,7 +1,5 @@
from shutil import copy, rmtree, copytree
from os.path import splitext
from contextlib import suppress
from xmlrpc.client import Fault as SupervisorFault
from tfw.util import SupervisorMixin
from tfw.config import LOGIN_APP_DIR
@ -43,19 +41,16 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
data = data_json['data']
with open(self.file, 'w') as ofile:
ofile.write(data['content'])
self.supervisor.stopProcess(self.process_name)
self.supervisor.startProcess(self.process_name)
self.restart_process()
def create_initial_state(self):
with suppress(SupervisorFault):
self.supervisor.stopProcess(self.process_name)
self.stop_process()
rmtree(self.working_directory, ignore_errors=True)
copytree('source_code_server/', self.working_directory)
file = copy(self.filename, self.working_directory)
self.supervisor.startProcess(self.process_name)
self.start_process()
return file

View File

@ -17,7 +17,7 @@ class TerminadoEventHandler(EventHandlerBase, SupervisorMixin):
def setup_terminado_server(self):
rmtree(self.working_directory, ignore_errors=True)
copytree('terminado_server/', self.working_directory)
self.supervisor.startProcess(self.process_name)
self.start_process()
def handle_event(self, anchor, data_json):
log.debug('TerminadoEventHandler received event for anchor {}'.format(anchor))