Expand description
Worker context and task tracking.
WorkerContext is responsible for managing
the execution lifecycle of a worker, tracking tasks, handling shutdown, and emitting
lifecycle events.
§Lifecycle
A WorkerContext goes through distinct phases of operation:
- Pending: Created via
WorkerContext::newand must be explicitly started. - Running: Activated by calling
WorkerContext::start. The worker becomes ready to accept and track tasks. - Paused: Temporarily halted via
WorkerContext::pause. New tasks are blocked from execution. - Resumed: Brought back to
RunningusingWorkerContext::resume. - Stopped: Finalized via
WorkerContext::stop. The worker shuts down gracefully, allowing tracked tasks to complete.
The WorkerContext itself implements Future, and can be .awaited — it resolves
once the worker is shut down and all tasks have completed.
§Task Management
Asynchronous tasks can be tracked which ensures:
- Task count is incremented before execution and decremented on completion
- Shutdown is automatically triggered once all tasks are done
Use task_count and has_pending_tasks to inspect
ongoing task state.
§Shutdown Semantics
The worker is considered shutting down if:
stop()has been called- A shutdown signal (if configured) has been triggered
Once shutdown begins, no new tasks should be accepted. Internally, a stored Waker is
used to drive progress toward shutdown completion.
§Event Handling
Worker lifecycle events (e.g., Start, Stop) are emitted automatically
and custom ones can be emitted using WorkerContext::emit.
§Request Integration
WorkerContext implements FromRequest so it can be extracted automatically in request
handlers when using a compatible framework or service layer.
§Types
WorkerContext— shared state container for a worker
Structs§
- Worker
Context - Utility for managing a worker’s context