Kiq v0.6.0 Kiq.Job View Source
Used to construct a Sidekiq compatible job.
The job complies with the Sidekiq Job Format, and contains the following fields:
jid
- A 12 byte random number as a 24 character hex encoded stringpid
— Process id of the worker running the job, defaults to the calling processclass
- The worker class which is responsible for executing the jobargs
- The arguments passed which should be passed to the workerqueue
- The queue where a job should be enqueued, defaults to “default”at
— A time at or after which a scheduled job should be performed, in Unix formatcreated_at
- When the job was created, in Unix formatenqueue_at
- When the job was enqueued, in Unix format
Retry & Failure Fields:
retry
- Tells the Kiq worker to retry the enqueue jobretry_count
- The number of times we’ve retried so farfailed_at
- The first time the job failed, in Unix formatretried_at
— The last time the job was retried, in Unix formaterror_message
— The message from the last exceptionerror_class
— The exception module (or class, in Sidekiq terms)backtrace
- The number of lines of error backtrace to store. Only present for compatibility with Sidekiq, this field is ignored.
Epiration Fields:
expires_in
- How long to keep a job before expiring it and skipping execution, in milliseconds
Unique Fields:
unique_for
- How long uniqueness will be enforced for a job, in millisecondsunique_until
- Allows controlling when a unique lock will be removed, valid options are “start” and “success”.unlocks_at
- When the job will be unlocked, in millisecondsunique_token
- The uniqueness token calculated from class, queue and args
Link to this section Summary
Functions
Decode an encoded job from JSON into a Job struct
Encode a job as JSON
Build a new Job
struct with all dynamic arguments populated
Convert a job into a map suitable for encoding
Extract a fully qualified worker module from a job
Link to this section Types
t() :: %Kiq.Job{ args: [any()], at: nil | Kiq.Timestamp.t(), class: binary(), created_at: Kiq.Timestamp.t(), dead: boolean(), enqueued_at: Kiq.Timestamp.t(), error_class: binary(), error_message: binary(), expires_at: nil | Kiq.Timestamp.t(), expires_in: pos_integer(), failed_at: Kiq.Timestamp.t(), jid: binary(), pid: pid(), queue: binary(), retried_at: Kiq.Timestamp.t(), retry: boolean() | non_neg_integer(), retry_count: non_neg_integer(), unique_for: pos_integer(), unique_token: binary(), unique_until: binary(), unlocks_at: nil | Kiq.Timestamp.t() }
Link to this section Functions
decode(input :: binary()) :: t() | {:error, Exception.t()}
Decode an encoded job from JSON into a Job struct.
All job keys are atomized except for those within arguments.
Example
iex> job = Kiq.Job.decode(~s({"class":"MyWorker","args":[1,2]}))
...> Map.take(job, [:class, :args])
%{class: "MyWorker", args: [1, 2]}
iex> job = Kiq.Job.decode(~s({"class":"MyWorker","args":{"a":1}}))
...> Map.get(job, :args)
%{"a" => 1}
encode(job :: t()) :: binary() | {:error, Exception.t()}
Encode a job as JSON.
During the encoding process any keys with nil
values are removed.
Build a new Job
struct with all dynamic arguments populated.
iex> Kiq.Job.new(%{class: "Worker"}) |> Map.take([:class, :args, :queue])
%{class: "Worker", args: [], queue: "default"}
To fit more naturally with Elixir the class
argument can be passed as module
:
iex> Kiq.Job.new(module: "Worker").class
"Worker"
Only “start” and “success” are allowed as values for unique_until
. Any
other value will be nullified:
iex> Kiq.Job.new(class: "A", unique_until: "start").unique_until
"start"
iex> Kiq.Job.new(class: "A", unique_until: :start).unique_until
"start"
iex> Kiq.Job.new(class: "A", unique_until: "whenever").unique_until
nil
Convert a job into a map suitable for encoding.
For Sidekiq compatibility and encodeability some values are rejected.
Specifically, the retry_count
value is dropped when it is 0.
Extract a fully qualified worker module from a job.
Examples
iex> Kiq.Job.to_module(Kiq.Job.new(module: "Kiq.Job"))
Kiq.Job