Skip to content

worker

TVPaintWorkerCommunicator

Bases: BaseCommunicator

Modified commuicator which cares about processing jobs.

Received jobs are send to TVPaint by parsing 'ProcessTVPaintCommands'.

Source code in client/ayon_tvpaint/worker/worker.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class TVPaintWorkerCommunicator(BaseCommunicator):
    """Modified commuicator which cares about processing jobs.

    Received jobs are send to TVPaint by parsing 'ProcessTVPaintCommands'.
    """
    def __init__(self, server_url):
        super().__init__()

        self.return_code = 1
        self._server_url = server_url
        self._worker_connection = None

    def _start_webserver(self):
        """Create connection to workers server before TVPaint server."""
        loop = self.websocket_server.loop
        self._worker_connection = WorkerJobsConnection(
            self._server_url, "tvpaint", loop
        )
        asyncio.ensure_future(
            self._worker_connection.main_loop(register_worker=False),
            loop=loop
        )

        super()._start_webserver()

    def _open_init_file(self):
        """Open init TVPaint file.

        File triggers dialog missing path to audio file which must be closed
        once and is ignored for rest of running process.
        """
        current_dir = os.path.dirname(os.path.abspath(__file__))
        init_filepath = os.path.join(current_dir, "init_file.tvpp")
        with tempfile.NamedTemporaryFile(
            mode="w", prefix="a_tvp_", suffix=".tvpp"
        ) as tmp_file:
            tmp_filepath = tmp_file.name.replace("\\", "/")

        shutil.copy(init_filepath, tmp_filepath)
        george_script = "tv_LoadProject '\"'\"{}\"'\"'".format(tmp_filepath)
        self.execute_george_through_file(george_script)
        self.execute_george("tv_projectclose")
        os.remove(tmp_filepath)

    def _on_client_connect(self, *args, **kwargs):
        super()._on_client_connect(*args, **kwargs)
        self._open_init_file()
        # Register as "ready to work" worker
        self._worker_connection.register_as_worker()

    def stop(self):
        """Stop worker connection and TVPaint server."""
        self._worker_connection.stop()
        self.return_code = 0
        super().stop()

    @property
    def current_job(self):
        """Retrieve job which should be processed."""
        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

        # Prepare variables used for sendig
        success = False
        message = "Unknown function"
        data = None
        job_data = job["data"]
        workfile = job_data["workfile"]
        # Currently can process only "commands" function
        if job_data.get("function") == "commands":
            try:
                commands = ProcessTVPaintCommands(
                    workfile, job_data["commands"], self
                )
                commands.execute()
                data = commands.response_data()
                success = True
                message = "Executed"

            except Exception as exc:
                message = "Error on worker: {}".format(str(exc))

        self._worker_connection.finish_job(success, message, data)

    def main_loop(self):
        """Main loop where jobs are processed.

        Server is stopped by killing this process or TVPaint process.
        """
        while self.server_is_running:
            if self._check_process():
                self._process_job()
            time.sleep(1)

        return self.return_code

current_job property

Retrieve job which should be processed.

main_loop()

Main loop where jobs are processed.

Server is stopped by killing this process or TVPaint process.

Source code in client/ayon_tvpaint/worker/worker.py
120
121
122
123
124
125
126
127
128
129
130
def main_loop(self):
    """Main loop where jobs are processed.

    Server is stopped by killing this process or TVPaint process.
    """
    while self.server_is_running:
        if self._check_process():
            self._process_job()
        time.sleep(1)

    return self.return_code

stop()

Stop worker connection and TVPaint server.

Source code in client/ayon_tvpaint/worker/worker.py
67
68
69
70
71
def stop(self):
    """Stop worker connection and TVPaint server."""
    self._worker_connection.stop()
    self.return_code = 0
    super().stop()