GitHub

DurableDeferred

API reference for DurableDeferred.

class DurableDeferred[A, E = Never]

A named wait point of a workflow execution whose result is an A or an E failure. Make one with make.

Attributes

AttributeTypeDescription
namestr

The deferred's name, unique within its workflow.

success_schemaCodec[A, Any, Never, Never]

The schema of the deferred's success.

error_schemaCodec[E, Any, Never, Never]

The schema of the deferred's failure.

exit_schemaExitSchema[A, E, Any, Any, Never, Never]

The schema of the deferred's result.

with_activity_attemptEffect[DurableDeferred[A, E]]

This deferred named for the current activity attempt ("{name}/{attempt}").

Functions

__init__

__init__(name, *, success, error) -> None
ParameterTypeDescription
namestr
successCodec[A, Any, Never, Never]
errorCodec[E, Any, Never, Never]
def __init__(
    self,
    name: str,
    *,
    success: Codec[A, Any, Never, Never],
    error: Codec[E, Any, Never, Never],
) -> None:
    self._name = name
    self._success_schema = success
    self._error_schema = error
    self._exit_schema: ExitSchema[A, E, Any, Any, Never, Never] = ExitOf(
        to_codec_json(success), to_codec_json(error), to_codec_json(Defect())
    )

wait

wait() -> Effect[A, E, WorkflowEngine | WorkflowInstance]

The deferred's result, suspending the execution until it has one (Effect's DurableDeferred.await).

Returns Effect[A, E, WorkflowEngine | WorkflowInstance]

def wait(self) -> Effect[A, E, WorkflowEngine | WorkflowInstance]:
    """The deferred's result, suspending the execution until it has one
    (Effect's ``DurableDeferred.await``)."""
    return await_(self)

into

into[A2, E2, R](effect) -> Effect[A2, E2, R | WorkflowEngine | WorkflowInstance]

Run effect and record its exit as this deferred's result, waking executions waiting on it.

effect and the write count as one in-flight activity of the workflow, so siblings parked on durable waits do not suspend the run until the exit is recorded. If effect parks on a durable wait itself, the run suspends once the other activities finish and nothing is recorded. Completing a deferred the run waits on still replays the run.

ParameterTypeDescription
effectEffect[A2, E2, R]

Returns Effect[A2, E2, R | WorkflowEngine | WorkflowInstance]

def into[A2, E2, R](
    self: DurableDeferred[A2, E2], effect: Effect[A2, E2, R]
) -> Effect[A2, E2, R | WorkflowEngine | WorkflowInstance]:
    """Run ``effect`` and record its exit as this deferred's result,
    waking executions waiting on it.

    ``effect`` and the write count as one in-flight activity of the
    workflow, so siblings parked on durable waits do not suspend the
    run until the exit is recorded. If ``effect`` parks on a durable
    wait itself, the run suspends once the other activities finish and
    nothing is recorded. Completing a deferred the run waits on still
    replays the run."""
    require_effect(effect, "effect")
    return _record_held(effect, self, check_cache=False)

token

token() -> Effect[Token, Never, WorkflowInstance]

The token of this deferred in the current execution.

Returns Effect[Token, Never, WorkflowInstance]

def token(self) -> Effect[Token, Never, WorkflowInstance]:
    """The token of this deferred in the current execution."""
    return WorkflowInstance().map(
        lambda instance: (
            TokenParsed(
                workflow_name=instance.workflow.tag,
                execution_id=instance.execution_id,
                deferred_name=self.name,
            ).as_token
        )
    )

token_from_execution_id

token_from_execution_id(*, workflow, execution_id) -> Token
ParameterTypeDescription
workflowWorkflow[Any, Any, Any]
execution_idstr

Returns Token

def token_from_execution_id(
    self, *, workflow: Workflow[Any, Any, Any], execution_id: str
) -> Token:
    return TokenParsed(
        workflow_name=workflow.tag, execution_id=execution_id, deferred_name=self.name
    ).as_token

token_from_payload

token_from_payload[P](*, workflow, payload) -> Effect[Token]
ParameterTypeDescription
workflowWorkflow[P, Any, Any]
payloadP

Returns Effect[Token]

def token_from_payload[P](
    self, *, workflow: Workflow[P, Any, Any], payload: P
) -> Effect[Token]:
    return workflow.execution_id(payload).map(
        lambda execution_id: self.token_from_execution_id(
            workflow=workflow, execution_id=execution_id
        )
    )

done

done[A2, E2](*, token, exit) -> Effect[None, Never, WorkflowEngine]

Record exit as the result of the deferred token names, waking its execution. A token naming another deferred is a defect (Effect writes exit into that deferred, with this one's schema).

ParameterTypeDescription
tokenstr
exitExit[A2, E2]

Returns Effect[None, Never, WorkflowEngine]

def done[A2, E2](
    self: DurableDeferred[A2, E2], *, token: str, exit: Exit[A2, E2]
) -> Effect[None, Never, WorkflowEngine]:
    """Record ``exit`` as the result of the deferred ``token`` names,
    waking its execution. A token naming another deferred is a defect
    (Effect writes ``exit`` into that deferred, with this one's schema)."""

    def body() -> Generator[Effect[Any, Never, Any], Any]:
        engine = yield from WorkflowEngine()
        parsed = TokenParsed.from_string(token)
        if parsed.deferred_name != self.name:
            raise ValueError(
                f"token names the deferred {parsed.deferred_name!r}, not {self.name!r}"
            )
        yield from engine.deferred_done(
            self,
            workflow_name=parsed.workflow_name,
            execution_id=parsed.execution_id,
            deferred_name=parsed.deferred_name,
            exit=exit,
        )

    return gen(body)

succeed

succeed[A2](*, token, value) -> Effect[None, Never, WorkflowEngine]
ParameterTypeDescription
tokenstr
valueA2

Returns Effect[None, Never, WorkflowEngine]

def succeed[A2](
    self: DurableDeferred[A2, Any], *, token: str, value: A2
) -> Effect[None, Never, WorkflowEngine]:
    return self.done(token=token, exit=Success(value))

fail

fail[E2](*, token, error) -> Effect[None, Never, WorkflowEngine]
ParameterTypeDescription
tokenstr
errorE2

Returns Effect[None, Never, WorkflowEngine]

def fail[E2](
    self: DurableDeferred[Any, E2], *, token: str, error: E2
) -> Effect[None, Never, WorkflowEngine]:
    from pyfect.cause import fail as fail_cause_of

    return self.done(token=token, exit=Failure(fail_cause_of(error)))

fail_cause

fail_cause[E2](*, token, cause) -> Effect[None, Never, WorkflowEngine]
ParameterTypeDescription
tokenstr
causeCause[E2]

Returns Effect[None, Never, WorkflowEngine]

def fail_cause[E2](
    self: DurableDeferred[Any, E2], *, token: str, cause: Cause[E2]
) -> Effect[None, Never, WorkflowEngine]:
    return self.done(token=token, exit=Failure(cause))