Exq.Middleware.Behaviour behaviour (exq v0.25.0)

View Source

Worker middleware configured through Exq's :middleware list.

Lifecycle callbacks run in the worker GenServer, not the Task executing the job, and return the pipeline. Prefer before_work/1, after_processed_work/1, and after_failed_work/1 for setup, cleanup, and notifications.

Use the optional around_perform/2 only when code must run inside the worker Task, such as attaching process-local context.

Summary

Callbacks

Runs in the worker GenServer when the Task raises, exits, is killed, or is canceled.

Runs in the worker GenServer after normal Task completion.

Wraps worker execution inside the Task, after job metadata is associated.

Runs in the worker GenServer before the worker Task starts.

Callbacks

after_failed_work(%Exq.Middleware.Pipeline{})

@callback after_failed_work(%Exq.Middleware.Pipeline{
  assigns: term(),
  event: term(),
  halted: term(),
  terminated: term(),
  worker_pid: term()
}) :: %Exq.Middleware.Pipeline{
  assigns: term(),
  event: term(),
  halted: term(),
  terminated: term(),
  worker_pid: term()
}

Runs in the worker GenServer when the Task raises, exits, is killed, or is canceled.

pipeline.assigns.error and pipeline.assigns.error_message describe the failure.

after_processed_work(%Exq.Middleware.Pipeline{})

@callback after_processed_work(%Exq.Middleware.Pipeline{
  assigns: term(),
  event: term(),
  halted: term(),
  terminated: term(),
  worker_pid: term()
}) :: %Exq.Middleware.Pipeline{
  assigns: term(),
  event: term(),
  halted: term(),
  terminated: term(),
  worker_pid: term()
}

Runs in the worker GenServer after normal Task completion.

pipeline.assigns.result contains the result returned by worker execution or its middleware.

around_perform(%Exq.Middleware.Pipeline{}, function)

(optional)
@callback around_perform(
  %Exq.Middleware.Pipeline{
    assigns: term(),
    event: term(),
    halted: term(),
    terminated: term(),
    worker_pid: term()
  },
  (%Exq.Middleware.Pipeline{
     assigns: term(),
     event: term(),
     halted: term(),
     terminated: term(),
     worker_pid: term()
   } ->
     {%Exq.Middleware.Pipeline{
        assigns: term(),
        event: term(),
        halted: term(),
        terminated: term(),
        worker_pid: term()
      }, term()})
) ::
  {%Exq.Middleware.Pipeline{
     assigns: term(),
     event: term(),
     halted: term(),
     terminated: term(),
     worker_pid: term()
   }, term()}

Wraps worker execution inside the Task, after job metadata is associated.

Both next.(pipeline) and this callback return {pipeline, result}. The first configured middleware is outermost.

Prefer the lifecycle callbacks unless Task-local execution is needed. Cleanup inside the Task is not guaranteed on hard kills or cancellation; use after_failed_work/1 when cleanup must survive Task termination.

After-work callbacks receive the returned assignments. On failure, they receive the last pipeline passed to next or returned by a completed callback.

Returning without calling next is treated as normal completion.

before_work(%Exq.Middleware.Pipeline{})

@callback before_work(%Exq.Middleware.Pipeline{
  assigns: term(),
  event: term(),
  halted: term(),
  terminated: term(),
  worker_pid: term()
}) :: %Exq.Middleware.Pipeline{
  assigns: term(),
  event: term(),
  halted: term(),
  terminated: term(),
  worker_pid: term()
}

Runs in the worker GenServer before the worker Task starts.

Prepare shared data in pipeline.assigns.