Core.JobStore behaviour (ServCore v0.1.0)

Copy Markdown View Source

Behaviour for persisting job state across VM restarts.

Forks can implement this behaviour to swap the storage backend (e.g. PostgreSQL, Redis, S3, etc.). The default Core.JobStore.Memory provides zero-durability in-memory IDs for prototyping.

Configuration

config :my_app, :job_store, Core.JobStore.SQLite
config :my_app, :job_store_opts, database: "priv/jobs.db"

All callbacks are invoked synchronously by Core.Workers.JobQueue.

Summary

Callbacks

Remove old completed / failed jobs. opts contains :max_age_days.

Fetch a single job by id.

Initialise the store (create tables, indexes, etc.).

Persist a new job. Must return the job with an assigned :id.

List jobs, optionally filtered by status.

Partially update a job by id. changes is a keyword list of fields.

Types

opts()

@type opts() :: Keyword.t()

Callbacks

cleanup(t)

@callback cleanup(Keyword.t()) :: :ok

Remove old completed / failed jobs. opts contains :max_age_days.

get_job(pos_integer)

@callback get_job(pos_integer()) :: {:ok, Core.Workers.Job.t()} | {:error, :not_found}

Fetch a single job by id.

init(opts)

@callback init(opts()) :: :ok | {:error, term()}

Initialise the store (create tables, indexes, etc.).

insert_job(t)

@callback insert_job(Core.Workers.Job.t()) ::
  {:ok, Core.Workers.Job.t()} | {:error, term()}

Persist a new job. Must return the job with an assigned :id.

list_jobs(t)

@callback list_jobs(Keyword.t()) :: [Core.Workers.Job.t()]

List jobs, optionally filtered by status.

update_job(pos_integer, changes)

@callback update_job(pos_integer(), changes :: Keyword.t()) :: :ok | {:error, term()}

Partially update a job by id. changes is a keyword list of fields.