Skip to content

Latest commit

 

History

History
170 lines (145 loc) · 8.77 KB

worker-interceptors.md

File metadata and controls

170 lines (145 loc) · 8.77 KB

Worker interceptors

Embedded Durable Workflow runs ordered worker interceptors around the workflow tasks, activities, queries and update validations it executes in your application's processes. Use them for audit logging, metric tags, tenant context or tracing that you want to write once instead of in every workflow and activity. The hook shape matches the Python, PHP and Rust SDK interceptors.

Configure interceptors

List interceptor classes in config/workflows.php. They are resolved from the Laravel container, so constructor injection works, and they run in the listed order; the first is the outermost wrapper.

'v2' => [
    // ...
    'interceptors' => [
        App\Workflows\AuditInterceptor::class,
    ],
],

Each interceptor implements Workflow\V2\Interceptors\WorkerInterceptor. Extend Workflow\V2\Interceptors\PassthroughWorkerInterceptor to implement only the hooks you need, and call $next($context) to continue the chain.

namespace App\Workflows;

use Illuminate\Support\Facades\DB;
use Psr\Log\LoggerInterface;
use Workflow\V2\Interceptors\ActivityInterceptorContext;
use Workflow\V2\Interceptors\PassthroughWorkerInterceptor;
use Workflow\V2\Interceptors\WorkflowTaskInterceptorContext;
use Workflow\V2\Interceptors\WorkflowTaskOutcome;

final class AuditInterceptor extends PassthroughWorkerInterceptor
{
    public function __construct(private readonly LoggerInterface $audit)
    {
    }

    public function executeWorkflowTask(WorkflowTaskInterceptorContext $context, callable $next): WorkflowTaskOutcome
    {
        $outcome = $next($context);
        // The task commits after the chain returns; log only once it has.
        DB::afterCommit(fn () => $this->audit->info('workflow task', [
            'workflow_id' => $context->workflowId,
            'run_id' => $context->runId,
            'attempt' => $context->workflowTaskAttempt,
            'recorded' => $outcome->historyEventTypes,
        ]));

        return $outcome;
    }

    public function executeActivity(ActivityInterceptorContext $context, callable $next): mixed
    {
        $this->audit->info('activity started', [
            'activity_type' => $context->activityType,
            'attempt' => $context->attemptNumber,
            'workflow_id' => $context->workflowId,
        ]);
        try {
            return $next($context);
        } finally {
            $this->audit->info('activity finished', ['activity_type' => $context->activityType]);
        }
    }
}

The container builds the configured interceptors for each task. Bind one as a singleton when it must keep state between tasks.

Hooks

Hook Context $next returns
executeWorkflowTask($context, $next) workerId, connection, taskQueue, namespace, task, taskId, workflowType, workflowId, runId, workflowTaskAttempt, historyLength, replaying (also replayState()) WorkflowTaskOutcome with historyEventTypes, runStatus, nextTaskId
executeActivity($context, $next) workerId, connection, taskQueue, namespace, task, activityType, activityClass, arguments, attemptNumber, taskId, activityExecutionId, activityAttemptId, workflowId, runId, local, activity the activity result
executeQueryTask($context, $next) workerId (always null), connection, taskQueue, namespace, task, queryName, workflowType, workflowId, runId, arguments, replayState() (always Replaying) the query result
executeUpdateValidation($context, $next) workerId (always null), connection, taskQueue, namespace, updateName, workflowType, workflowId, runId, arguments, replayState() (always Replaying) UpdateValidationOutcome with approved, rejectionMessage, exceptionType, validationErrors

task is an array snapshot of the claimed task row, never the model itself. workerId identifies the queue worker process by host and process ID. Queries and update validations run in the calling process, so their workerId is null.

Determinism

Workflow-task interceptors cannot change what a workflow records:

  • executeWorkflowTask wraps the workflow task inside its database transaction, once per task attempt. replaying is true when earlier workflow tasks already recorded commands for the run. A cold task re-runs that committed prefix first; with live execution a warm task may resume without re-running it. replayState() gives the same answer as a Workflow\V2\Enums\WorkflowReplayState, the enum Workflow::replayState() uses; the interceptor's value is fixed for the task. It is not a deduplication key: historyEventTypes lists only the events this task appended. Hook side effects run before the task commits and can repeat when an attempt is retried, so defer them with DB::afterCommit() or key them on taskId and workflowTaskAttempt.
  • The engine commits the history the workflow code produced. The outcome is a read-only view; returning a different outcome, or passing a different context to $next, changes nothing. A hook that rolls back a nested transaction around $next fails the task instead of committing a partial task.
  • A failure from the workflow code reaches the engine unchanged, even if an interceptor catches it and returns normally.
  • If an interceptor throws out of the chain, skips $next or calls it more than once, the task's transaction rolls back, so nothing is recorded. The task is released with WorkflowTaskInterceptorFailed as its last error and retried after the usual repair backoff. An outer interceptor may still catch an exception an inner interceptor threw after $next and let the task commit; a skipped or repeated $next always fails the task.
  • Hooks run while the run and task rows are locked, so keep them short. Local activities run inside the task, so a hook that keeps failing after $next makes each retry run those local activities again.
  • Hooks must not commit or roll back the surrounding transaction, and must not send commands such as signals to the run they are wrapping; a change to the run's recorded state fails the task on every attempt.
  • A workflow that resolves deferred entity references outside the task transaction runs the task in phases. The hook then runs once per phase, and the earlier phases see the internal deferral as an exception from $next.

Update-validation interceptors wrap the replay and the validator call that run before an update with an #[UpdateValidator] is accepted. Updates without a validator don't run the hook. The hooks observe the decision; they cannot change it:

  • The engine acts on the validator's own outcome. Returning a different UpdateValidationOutcome, or passing different arguments to $next, changes nothing; the validator always receives the update's arguments.
  • If a hook throws, skips $next or calls it more than once, the update is refused with UpdateValidationFailedException and nothing is recorded, so a hook can never let an update through unvalidated.
  • The hook runs inside the update's admission transaction, while the run's rows are locked, so keep it short. Defer side effects with DB::afterCommit(); writes a hook makes commit or roll back with the admission. Hook code runs outside the workflow fiber, so it cannot schedule durable work for the run.

Activity and query interceptors may change arguments with $context->withArguments(...) and may change the returned value, because an activity result is recorded once and a query records nothing. Rewritten query arguments are validated against the query contract again. Activity exceptions pass through every interceptor unless one handles them, and then go through the activity's normal retry policy. Activity interceptors wrap queued and local activities; local tells them apart. Mocked activities in WorkflowStub::fake() do not run interceptors.

Tracing and external workers

When embedded tracing is enabled, the built-in workflow and activity spans stay outermost, so interceptors run inside the active span and can add attributes or child spans.

Interceptors wrap the queue jobs, local activities and queries this package runs. Tasks handed to external SDK workers through the worker task bridges run under that SDK's own interceptors. The worker bridge's execute route, which runs a claimed workflow task in-process for a bridge caller, does not run workflow-task interceptors; local activities that task runs still go through activity interceptors.

An entry in workflows.v2.interceptors that cannot be resolved fails each path its own way: a queued workflow task is released and retried, an activity attempt fails under its retry policy, and a query throws to its caller. Check the list when you deploy.