Core.Workers.Worker (ServCore v0.3.0)

Copy Markdown View Source

Background job worker. Polls the JobQueue every @poll_interval ms, claims the next available job, executes it, and updates its status.

Designed to run in a pool via Core.Workers.WorkerPool. Each worker process operates independently — there is no cross-worker coordination needed because JobQueue serializes claim_next/1 via GenServer.call.

Custom workers

To create a domain-specific worker, copy this module and override perform_work/1. You must keep the same start_link/1 signature so WorkerPool can start it. The worker should read :id and :pool from opts and register under a unique name derived from the pool.

Options (passed by WorkerPool)

  • :id – unique integer ID within the pool
  • :queue – the JobQueue name to poll (default: Core.Workers.JobQueue)
  • :pool – the WorkerPool name this worker belongs to

Execution flow

  1. The worker receives :work_available (immediate wake-up from JobQueue) or :work (fallback timer every @poll_interval ms).
  2. It calls JobQueue.claim_next/1 to atomically claim the next :queued job.
  3. It calls perform_work/1 with the job struct — this is your hook.
  4. On success it calls JobQueue.mark_done/3; on exception it calls JobQueue.mark_failed/3.

Telemetry events emitted

  • [:core, :job, :start] — when a job begins executing metadata: %{job_id: id, attempt: n, payload: map}
  • [:core, :job, :stop] — when a job completes successfully measurements: %{duration: native_time} metadata: %{job_id: id}
  • [:core, :job, :error] — when a job raises an exception measurements: %{duration: native_time} metadata: %{job_id: id, error: string}

Summary

Functions

Returns a specification to start this module under a supervisor.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

perform_work(job)

start_link(opts \\ [])