Core.Workers.JobQueue (ServCore v0.3.0)

Copy Markdown View Source

FIFO job queue backed by a pluggable Core.JobStore.

Each JobQueue is a GenServer that maintains an in-memory queue of job IDs and a map of job structs. Jobs are persisted via the store so they survive VM restarts (when using a durable backend such as Core.JobStore.SQLite).

Named queues

You can start multiple isolated queues by giving each a unique :name:

{Core.Workers.JobQueue, name: MyApp.EmailQueue, pool: MyApp.EmailPool}
{Core.Workers.JobQueue, name: MyApp.MediaQueue, pool: MyApp.MediaPool}

All public functions accept a server reference (atom or pid) as the first argument. The zero-arity versions operate on the default queue Core.Workers.JobQueue.

Options

  • :name – registered name for this queue (default: Core.Workers.JobQueue)
  • :pool – the WorkerPool name to notify when new jobs arrive
  • :storeCore.JobStore module (default: Core.JobStore.Memory)
  • :store_opts – options passed to the store's init/1
  • :cleanup_interval_ms – how often to purge old jobs (default: 1 hour)
  • :max_age_days – retention for completed / failed jobs (default: 7)

Worker notification

When a job is submitted (or a retry is scheduled), JobQueue calls notify_workers/1 which sends :work_available to every worker process under the configured WorkerPool. Workers use this as an immediate wake-up signal in addition to their fallback polling timer.

Summary

Functions

Get all jobs in the queue

Returns a specification to start this module under a supervisor.

Claim the next available job for processing. Marks it as :running and returns it to the worker.

Get a specific job by ID

Mark a job as completed with a result

Mark a job as failed with an error reason

Reset the queue — clears all in-memory state. Intended for test use only.

Submit a new job to the queue. Returns synchronously with the assigned job id.

Submit a job to run at a specific future time. The job is persisted immediately but enters the in-memory queue only at run_at.

Functions

all()

Get all jobs in the queue

all(opts)

all(server, opts)

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

claim_next()

Claim the next available job for processing. Marks it as :running and returns it to the worker.

claim_next(server)

get(id)

Get a specific job by ID

get(server, id)

mark_done(id, result)

Mark a job as completed with a result

mark_done(server, id, result)

mark_failed(id, reason)

Mark a job as failed with an error reason

mark_failed(server, id, reason)

reset()

Reset the queue — clears all in-memory state. Intended for test use only.

reset(server)

start_link(opts \\ [])

stats()

stats(server)

submit(payload)

Submit a new job to the queue. Returns synchronously with the assigned job id.

submit(payload, opts)

submit(server, payload, opts)

submit_at(payload, run_at)

Submit a job to run at a specific future time. The job is persisted immediately but enters the in-memory queue only at run_at.

submit_at(payload, run_at, opts)

submit_at(server, payload, run_at, opts)