From c5962fd35ebab960eacbe3d734a02598c0fa806d Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Fri, 8 Oct 2021 18:50:06 +0200 Subject: [PATCH] initial tvpaint worker connection --- .../job_workers/tvpaint_worker.py | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 openpype/modules/default_modules/hosts_job_server/job_workers/tvpaint_worker.py diff --git a/openpype/modules/default_modules/hosts_job_server/job_workers/tvpaint_worker.py b/openpype/modules/default_modules/hosts_job_server/job_workers/tvpaint_worker.py new file mode 100644 index 0000000000..0cb6d4a7a1 --- /dev/null +++ b/openpype/modules/default_modules/hosts_job_server/job_workers/tvpaint_worker.py @@ -0,0 +1,93 @@ +import signal +import time +import asyncio + +from avalon.tvpaint.communication_server import ( + BaseCommunicator, + CommunicationWrapper +) +from .base_worker import WorkerJobsConnection + + +class WorkerCommunicator(BaseCommunicator): + def __init__(self, server_url): + super().__init__(self) + + self._server_url = server_url + self._worker_connection = None + + def _start_webserver(self): + loop = self.websocket_server.loop + self._worker_connection = WorkerJobsConnection( + self._server_url, "tvpaint", loop + ) + asyncio.ensure_future( + self._worker_connection.main_loop(), loop=loop + ) + + super()._start_webserver() + + def stop(self): + self._worker_connection.stop() + super().stop() + + @property + def current_job(self): + if self._worker_connection: + return self._worker_connection.current_job + return None + + def _check_process(self): + if self.process is None: + return True + + if self.process.poll() is not None: + asyncio.ensure_future( + self._worker_connection.disconnect(), + loop=self.websocket_server.loop + ) + self._exit() + return False + return True + + def _process_job(self): + job = self.current_job + if job is None: + return + + print(job) + self._worker_connection.finish_job() + + def main_loop(self): + while self.server_is_running: + if self._check_process(): + self._process_job() + time.sleep(1) + + return self.return_code + + +def _start_tvpaint(tvpaint_executable_path, server_url): + communicator = WorkerCommunicator(server_url) + CommunicationWrapper.set_communicator(communicator) + communicator.launch([tvpaint_executable_path]) + + +def main(tvpaint_executable_path, server_url): + # Register terminal signal handler + def signal_handler(*_args): + print("Termination signal received. Stopping.") + if CommunicationWrapper.communicator is not None: + CommunicationWrapper.communicator.stop() + + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTERM, signal_handler) + + _start_tvpaint(tvpaint_executable_path, server_url) + + communicator = CommunicationWrapper.communicator + if communicator is None: + print("Communicator is not set") + return 1 + + return communicator.main_loop()