ecto_job v0.2.0 EctoJob.JobQueue behaviour View Source

Mixin for defining an Ecto schema for a Job Queue table in the database.

Example

defmodule MyApp.JobQueue do
  use EctoJob.JobQueue, table_name: "jobs"

  def perform(multi = %Ecto.Multi{}, job = %{}) do
    ...
  end
end

Link to this section Summary

Functions

Updates all jobs in the RESERVED or IN_PROGRESS state with expires time <= now to “AVAILABLE” state. Returns the number of jobs updated

Updates all jobs in the SCHEDULED state with scheduled time <= now to “AVAILABLE” state. Returns the number of jobs updated

Builds a query for a batch of available jobs. The batch size is determined by demand

Creates a changeset that will delete a job, confirming that the attempt counter hasn’t been increased by another worker process

Updates all jobs that have been attempted the maximum number of times to FAILED. Returns the number of jobs updated

Creates an Ecto.Multi struct with the command to delete the given job from the queue. This will be passed as the first argument to the user supplied callback function

Computes the expiry time for an IN_PROGRESS job based on the current time and attempt counter

Computes the expiry time for a job reservation to be held, given the current time

Updates a batch of jobs in AVAILABLE state ot RESERVED state with an expiry. The batch size is determined by demand. returns {count, updated_jobs} tuple

Transitions a job from RESERVED to IN_PROGRESS

Link to this section Types

Link to this type job() View Source
job() :: %:atom{
  id: integer(),
  state: state(),
  expires: DateTime.t() | nil,
  schedule: DateTime.t(),
  attempt: integer(),
  max_attempts: integer(),
  params: map(),
  notify: String.t() | nil,
  updated_at: DateTime.t(),
  inserted_at: DateTime.t()
}

Link to this section Functions

Link to this function activate_expired_jobs(repo, schema, now) View Source
activate_expired_jobs(repo(), schema(), DateTime.t()) :: integer()

Updates all jobs in the RESERVED or IN_PROGRESS state with expires time <= now to “AVAILABLE” state. Returns the number of jobs updated.

Link to this function activate_scheduled_jobs(repo, schema, now) View Source
activate_scheduled_jobs(repo(), schema(), DateTime.t()) :: integer()

Updates all jobs in the SCHEDULED state with scheduled time <= now to “AVAILABLE” state. Returns the number of jobs updated.

Link to this function available_jobs(schema, demand) View Source
available_jobs(schema(), integer()) :: Ecto.Query.t()

Builds a query for a batch of available jobs. The batch size is determined by demand

Link to this function delete_job_changeset(job) View Source
delete_job_changeset(job()) :: Ecto.Changeset.t()

Creates a changeset that will delete a job, confirming that the attempt counter hasn’t been increased by another worker process.

Link to this function fail_expired_jobs_at_max_attempts(repo, schema, now) View Source
fail_expired_jobs_at_max_attempts(repo(), schema(), DateTime.t()) :: integer()

Updates all jobs that have been attempted the maximum number of times to FAILED. Returns the number of jobs updated.

Link to this function initial_multi(job) View Source
initial_multi(job()) :: Ecto.Multi.t()

Creates an Ecto.Multi struct with the command to delete the given job from the queue. This will be passed as the first argument to the user supplied callback function.

Link to this function progress_expiry(now, attempt) View Source
progress_expiry(DateTime.t(), integer()) :: DateTime.t()

Computes the expiry time for an IN_PROGRESS job based on the current time and attempt counter

Link to this function reservation_expiry(now) View Source
reservation_expiry(DateTime.t()) :: DateTime.t()

Computes the expiry time for a job reservation to be held, given the current time.

Link to this function reserve_available_jobs(repo, schema, demand, now) View Source
reserve_available_jobs(repo(), schema(), integer(), DateTime.t()) ::
  {integer(), [job()]}

Updates a batch of jobs in AVAILABLE state ot RESERVED state with an expiry. The batch size is determined by demand. returns {count, updated_jobs} tuple.

Link to this function update_job_in_progress(repo, job, now) View Source
update_job_in_progress(repo(), job(), DateTime.t()) ::
  {:ok, job()} | {:error, :expired}

Transitions a job from RESERVED to IN_PROGRESS.

Confirms that the job reservation hasn’t expired by checking:

  • The attempt counter hasn’t been changed
  • The state is still RESERVED
  • The expiry time is in the future

Updates the state to “IN_PROGRESS”, increments the attempt counter, and sets an expiry time, proportional to the attempt counter.

Returns {:ok, job} when sucessful, {:error, :expired} otherwise.

Link to this section Callbacks

Link to this callback perform(arg0, map) View Source
perform(Ecto.Multi.t(), map()) :: term()