PhoenixGenApi.WorkerPool.Worker (PhoenixGenApi v2.11.0)

Copy Markdown View Source

Individual worker process for executing tasks.

Workers are managed by the WorkerPool and execute tasks one at a time. When a task completes, the worker notifies the pool that it's available for more work.

Lifecycle

  1. Worker starts in idle state
  2. Pool assigns a task via execute/2
  3. Worker executes the task with timeout protection
  4. Worker notifies pool when done
  5. Returns to idle state

Error Handling

If a task raises an exception, the worker catches it, logs the error, and continues running. The pool is notified that the worker is done even if the task failed.

Circuit Breaker

Workers track consecutive failures. If failures exceed the threshold, the worker enters a "circuit open" state and rejects new tasks for a cooldown period. This prevents cascading failures when downstream services are unhealthy.

Summary

Functions

Returns a specification to start this module under a supervisor.

Executes a task on this worker.

Starts a worker process.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

execute(worker_pid, task)

Executes a task on this worker.

Parameters

  • worker_pid - The worker process PID
  • task - A zero-arity function to execute

start_link(opts)

Starts a worker process.

Parameters

  • opts - Keyword list with:
    • :pool_name - Name of the parent pool (required)
    • :task_timeout - Task execution timeout in milliseconds (default: 30000)