Skip to content

execution

Execution/Submission management for the web editor.

ExecutionManager

Manages workflow executions and their event queues.

Source code in client/ayon_workflow/web_editor/execution.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
class ExecutionManager:
    """ Manages workflow executions and their event queues.
    """

    def __init__(self):
        self._lock = threading.Lock()
        self._next_run_id = 0
        self._runs: Dict[int, Dict[str, Any]] = {}

    def __new_run(self) -> Tuple[int, Dict[str, Any]]:
        with self._lock:
            self._next_run_id += 1
            run = {
                "status": "running",
                "cancel_event": threading.Event(),
                "queue": RunQueue(),
            }
            self._runs[self._next_run_id] = run
            return self._next_run_id, run

    def __get_run_by_id(self, run_id: int) -> Optional[Dict[str, Any]]:
        with self._lock:
            return self._runs.get(run_id)

    def _set_status(
        self,
        run_id: int,
        status: str,
        queue: RunQueue,
    ):
        with self._lock:
            run = self._runs.get(run_id)
            if run:
                run["status"] = status

        _STATUS_TO_EVENT = {
            "running": "execution.status",
            "done": "execution.completed",
            "failed": "execution.failed",
            "cancelled": "execution.failed",
        }
        event_type = _STATUS_TO_EVENT.get(status, "execution.status")
        queue.put({
            "event_type": event_type,
            "run_id": run_id,
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "execution_status": status,
        })

    def get_status(self, run_id: int) -> Dict[str, Any]:
        run = self.__get_run_by_id(run_id)
        return {
            "run_id": run_id,
            "status": run["status"] if run else "not_found",
        }

    def get_queue(self, run_id: int) -> Optional[RunQueue]:
        run = self.__get_run_by_id(run_id)
        return run["queue"] if run else None

    def start_execution(self, workflow: Workflow) -> Dict[str, Any]:
        """ Start a new local execution of the provided workflow.
        """
        run_id, run = self.__new_run()
        run["queue"].put({
            "event_type": "execution.status",
            "run_id": run_id,
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "execution_status": "running",
        })

        # perform the execution in a new thread
        threading.Thread(
            target=_run_workflow_with_monitoring,
            args=(
                workflow,
                run_id,
                run["queue"],
                run["cancel_event"],
                self._set_status,
            ),
            name=f"ayon-workflow-run-{run_id}",
            daemon=True,
        ).start()
        return self.get_status(run_id)

    def start_submit(
        self,
        workflow: Workflow,
        backend_dir: str,
        dispatch_graph_name: Optional[str] = None,
        project: Optional[str] = None,
    ) -> Dict[str, Any]:
        """ Export workflow, then submit to farm and watch backend_dir.
        """
        import tempfile
        from contextlib import suppress
        from ayon_workflow.workflow_execution.renderfarm import (
            submit_workflow_to_farm,
        )

        run_id, run = self.__new_run()
        queue = run["queue"]

        temp_path = None
        try:
            # Export workflow to temporary file
            with tempfile.NamedTemporaryFile(
                dir=backend_dir,
                suffix="_temp.json",
                delete=False
            ) as fh:
                temp_path = fh.name
            workflow.export_to_file(temp_path)

            # Submit workflow to farm
            submit_workflow_to_farm(
                temp_path,
                backend_dir=backend_dir,
                dispatch_graph_name=dispatch_graph_name,
                project=project,
            )

        except Exception as error:
            error_message = str(error)
            with self._lock:
                run["status"] = "failed"
            queue.put({
                "event_type": "execution.log",
                "run_id": run_id,
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "level": "ERROR",
                "message": error_message,
            })
            queue.put({
                "event_type": "execution.failed",
                "run_id": run_id,
                "timestamp": datetime.now(timezone.utc).isoformat(),
                "message": error_message,
            })
            queue.put({
                "event_type": "execution.end",
                "run_id": run_id,
                "timestamp": datetime.now(timezone.utc).isoformat(),
            })
            queue.close()
            raise
        finally:
            if temp_path:
                with suppress(OSError):
                    os.unlink(temp_path)

        queue.put({
            "event_type": "execution.status",
            "run_id": run_id,
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "execution_status": "running",
        })

        # Start watching the backend in a separate thread.
        watcher = _BackendDirWatcher(backend_dir, run_id, queue)
        threading.Thread(
            target=_watch_submit,
            args=(watcher, run_id, queue, self._set_status),
            name=f"ayon-workflow-watch-{run_id}",
            daemon=True,
        ).start()

        return self.get_status(run_id)

    def cancel(self, run_id: int) -> bool:
        """ Cancel a running workflow by its id.
        """
        with self._lock:
            run = self._runs.get(run_id)
            if run is None or run["status"] != "running":
                return False

            # TODO: current implementation is a cosmetic cancel,
            # the workflow will continue to run. Need to implement a proper
            # cancellation mechanism for execution and submission.
            run["cancel_event"].set()
            run["status"] = "cancelled"

        run["queue"].put({
            "event_type": "execution.failed",
            "run_id": run_id,
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "execution_status": "cancelled",
        })
        return True


    def cancel_all_executions(self):
        """ Cancel all currently running executions.
        """
        with self._lock:
            run_ids = [
                run_id
                for run_id, run in self._runs.items()
                if run["status"] == "running"
            ]
        for run_id in run_ids:
            self.cancel(run_id)

cancel(run_id)

Cancel a running workflow by its id.

Source code in client/ayon_workflow/web_editor/execution.py
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
def cancel(self, run_id: int) -> bool:
    """ Cancel a running workflow by its id.
    """
    with self._lock:
        run = self._runs.get(run_id)
        if run is None or run["status"] != "running":
            return False

        # TODO: current implementation is a cosmetic cancel,
        # the workflow will continue to run. Need to implement a proper
        # cancellation mechanism for execution and submission.
        run["cancel_event"].set()
        run["status"] = "cancelled"

    run["queue"].put({
        "event_type": "execution.failed",
        "run_id": run_id,
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "execution_status": "cancelled",
    })
    return True

cancel_all_executions()

Cancel all currently running executions.

Source code in client/ayon_workflow/web_editor/execution.py
461
462
463
464
465
466
467
468
469
470
471
def cancel_all_executions(self):
    """ Cancel all currently running executions.
    """
    with self._lock:
        run_ids = [
            run_id
            for run_id, run in self._runs.items()
            if run["status"] == "running"
        ]
    for run_id in run_ids:
        self.cancel(run_id)

start_execution(workflow)

Start a new local execution of the provided workflow.

Source code in client/ayon_workflow/web_editor/execution.py
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
def start_execution(self, workflow: Workflow) -> Dict[str, Any]:
    """ Start a new local execution of the provided workflow.
    """
    run_id, run = self.__new_run()
    run["queue"].put({
        "event_type": "execution.status",
        "run_id": run_id,
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "execution_status": "running",
    })

    # perform the execution in a new thread
    threading.Thread(
        target=_run_workflow_with_monitoring,
        args=(
            workflow,
            run_id,
            run["queue"],
            run["cancel_event"],
            self._set_status,
        ),
        name=f"ayon-workflow-run-{run_id}",
        daemon=True,
    ).start()
    return self.get_status(run_id)

start_submit(workflow, backend_dir, dispatch_graph_name=None, project=None)

Export workflow, then submit to farm and watch backend_dir.

Source code in client/ayon_workflow/web_editor/execution.py
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
def start_submit(
    self,
    workflow: Workflow,
    backend_dir: str,
    dispatch_graph_name: Optional[str] = None,
    project: Optional[str] = None,
) -> Dict[str, Any]:
    """ Export workflow, then submit to farm and watch backend_dir.
    """
    import tempfile
    from contextlib import suppress
    from ayon_workflow.workflow_execution.renderfarm import (
        submit_workflow_to_farm,
    )

    run_id, run = self.__new_run()
    queue = run["queue"]

    temp_path = None
    try:
        # Export workflow to temporary file
        with tempfile.NamedTemporaryFile(
            dir=backend_dir,
            suffix="_temp.json",
            delete=False
        ) as fh:
            temp_path = fh.name
        workflow.export_to_file(temp_path)

        # Submit workflow to farm
        submit_workflow_to_farm(
            temp_path,
            backend_dir=backend_dir,
            dispatch_graph_name=dispatch_graph_name,
            project=project,
        )

    except Exception as error:
        error_message = str(error)
        with self._lock:
            run["status"] = "failed"
        queue.put({
            "event_type": "execution.log",
            "run_id": run_id,
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "level": "ERROR",
            "message": error_message,
        })
        queue.put({
            "event_type": "execution.failed",
            "run_id": run_id,
            "timestamp": datetime.now(timezone.utc).isoformat(),
            "message": error_message,
        })
        queue.put({
            "event_type": "execution.end",
            "run_id": run_id,
            "timestamp": datetime.now(timezone.utc).isoformat(),
        })
        queue.close()
        raise
    finally:
        if temp_path:
            with suppress(OSError):
                os.unlink(temp_path)

    queue.put({
        "event_type": "execution.status",
        "run_id": run_id,
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "execution_status": "running",
    })

    # Start watching the backend in a separate thread.
    watcher = _BackendDirWatcher(backend_dir, run_id, queue)
    threading.Thread(
        target=_watch_submit,
        args=(watcher, run_id, queue, self._set_status),
        name=f"ayon-workflow-watch-{run_id}",
        daemon=True,
    ).start()

    return self.get_status(run_id)

RunQueue

Per run_id event queue with index/cursor-based replay.

Source code in client/ayon_workflow/web_editor/execution.py
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
class RunQueue:
    """ Per run_id event queue with index/cursor-based replay.
    """

    def __init__(self):
        self._lock = threading.Condition(threading.Lock())
        self._events: List[dict] = []
        self._closed: bool = False

    def put(self, event: dict):
        with self._lock:
            self._events.append(event)
            self._lock.notify_all()  # notify all waiting threads

    def close(self):
        with self._lock:
            self._closed = True
            self._lock.notify_all()  # notify all waiting threads

    def get_from(
        self,
        idx: int,
        timeout: float = 0.5,
    ) -> Tuple[int, List[dict], bool]:
        """ Block until new events are available or timeout.
        """
        with self._lock:
            self._lock.wait_for(
                lambda: len(self._events) > idx or self._closed,
                timeout=timeout,  # timeout while no new events
            )
            events = self._events[idx:]
            new_idx = idx + len(events)
            return new_idx, events, self._closed

get_from(idx, timeout=0.5)

Block until new events are available or timeout.

Source code in client/ayon_workflow/web_editor/execution.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def get_from(
    self,
    idx: int,
    timeout: float = 0.5,
) -> Tuple[int, List[dict], bool]:
    """ Block until new events are available or timeout.
    """
    with self._lock:
        self._lock.wait_for(
            lambda: len(self._events) > idx or self._closed,
            timeout=timeout,  # timeout while no new events
        )
        events = self._events[idx:]
        new_idx = idx + len(events)
        return new_idx, events, self._closed