GitHub

DeferredState

API reference for DeferredState.

The live runs of an engine, so a deferred completion can wake the run waiting on it (Effect's DeferredState).

Functions

__init__

__init__() -> None
def __init__(self) -> None:
    self._running: dict[str, tuple[WorkflowInstanceService, Fiber[Any, Any]]] = {}

track_run

track_run[A, E](instance: WorkflowInstanceService, effect: Effect[A, E, WorkflowInstance]) -> Effect[A, E]
track_run[A, E, R](instance: WorkflowInstanceService, effect: Effect[A, E, R | WorkflowInstance]) -> Effect[A, E, R]

Run effect with instance, tracked until it completes or suspends.

ParameterTypeDescription
instance
effect
@overload
def track_run[A, E](
    self, instance: WorkflowInstanceService, effect: Effect[A, E, WorkflowInstance]
) -> Effect[A, E]: ...
@overload
def track_run[A, E, R](
    self, instance: WorkflowInstanceService, effect: Effect[A, E, R | WorkflowInstance]
) -> Effect[A, E, R]: ...
def track_run[A, E, R](
    self, instance: WorkflowInstanceService, effect: Effect[A, E, R | WorkflowInstance]
) -> Effect[A, E, R]:
    """Run ``effect`` with ``instance``, tracked until it completes or
    suspends."""
    require_effect(effect, "effect")

    def body(fiber: Fiber[Any, Any]) -> Effect[A, E, R]:
        run = (instance, fiber)
        self._running[instance.execution_id] = run

        def untrack() -> None:
            if self._running.get(instance.execution_id) is run:
                del self._running[instance.execution_id]

        return effect.pipe(provide_service(WorkflowInstance, instance)).ensuring(sync(untrack))

    return _WithFiber(body)

deferred_done

deferred_done(execution_id, name) -> Effect[None]

Preempt the run of execution_id if it waits on name.

ParameterTypeDescription
execution_idstr
namestr

Returns Effect[None]

def deferred_done(self, execution_id: str, name: str) -> Effect[None]:
    """Preempt the run of ``execution_id`` if it waits on ``name``."""

    def body(current: Fiber[Any, Any]) -> Effect[None]:
        run = self._running.get(execution_id)
        if run is None:
            return succeed(None)
        instance, fiber = run
        if (
            fiber is current
            or fiber.poll_unsafe() is not None
            or name not in instance.awaited_deferreds
        ):
            return succeed(None)
        # The engine stores the completion before waking the run; its
        # replay reads it after the interrupted run reports suspension.
        instance.suspended = True
        return fiber.interrupt()

    return _WithFiber(body)