Represents a durable job obligation in Kathikon.
Jobs move through explicit states enforced by Kathikon.Job.StateMachine:
:scheduled— waiting untilscheduled_at:available— ready to be claimed:claimed— atomically claimed, not yet running:running— currently being processed (v0.1:executing):retryable— failed but will be retried after backoff:waiting_for_children— batch parent waiting on child jobs:completed— successfully finished:failed— exhausted retries or terminal worker failure:dead— in the dead-letter queue:cancelled— explicitly cancelled:discarded— permanently discarded
See docs/job_lifecycle.md.
Summary
Functions
Computes exponential backoff in seconds for the given attempt number.
Builds a new job from worker module, args, and options.
Returns true when the job can be claimed for execution at now.
Returns job history events from storage.
Normalizes legacy :executing to :running.
Converts a job struct to a map for storage callbacks and reporting.
Types
@type t() :: %Kathikon.Job{ args: map(), attempts: non_neg_integer(), available_at: DateTime.t() | nil, batch_id: String.t() | nil, cancelled_at: DateTime.t() | nil, claimant: map() | nil, claimed_at: DateTime.t() | nil, completed_at: DateTime.t() | nil, discarded_at: DateTime.t() | nil, error: term(), errors: [map()], failed_at: DateTime.t() | nil, id: String.t(), inserted_at: DateTime.t() | nil, last_error: term(), max_attempts: pos_integer(), node: node() | nil, original_job_id: String.t() | nil, parent_job_id: String.t() | nil, priority: non_neg_integer(), queue: atom(), rerun_of: String.t() | nil, result: term(), result_mode: :store | :discard, scheduled_at: DateTime.t() | nil, started_at: DateTime.t() | nil, state: :scheduled | :available | :claimed | :running | :executing | :retryable | :waiting_for_children | :completed | :failed | :dead | :cancelled | :discarded, worker: module() }
Functions
@spec backoff_seconds(non_neg_integer()) :: non_neg_integer()
Computes exponential backoff in seconds for the given attempt number.
Examples
Kathikon.Job.backoff_seconds(1)
#=> 5
Kathikon.Job.backoff_seconds(3)
#=> 45
Builds a new job from worker module, args, and options.
Prefer Kathikon.insert/3 for enqueueing — it persists the job and starts
the queue dispatcher.
Examples
job = Kathikon.Job.build(MyApp.EmailWorker, %{"to" => "a@b.com"},
queue: :emails,
priority: 2,
schedule_in: 60
)
job.state
#=> :scheduled
@spec claimable?(t(), DateTime.t()) :: boolean()
Returns true when the job can be claimed for execution at now.
Examples
job = Kathikon.Job.build(MyWorker, %{})
Kathikon.Job.claimable?(job, DateTime.utc_now())
#=> true
scheduled = Kathikon.Job.build(MyWorker, %{}, schedule_in: 3600)
Kathikon.Job.claimable?(scheduled, DateTime.utc_now())
#=> false
Returns job history events from storage.
Prefer Kathikon.history/1 in application code.
Examples
{:ok, events} = Kathikon.Job.history(job_id)
Normalizes legacy :executing to :running.
Converts a job struct to a map for storage callbacks and reporting.
Examples
Kathikon.Job.to_map(job)
#=> %{id: "...", state: :completed, worker: MyWorker, ...}