Kiq v0.1.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 string
  • pid — Process id of the worker running the job, defaults to the calling process
  • class - The worker class which is responsible for executing the job
  • args - The arguments passed which should be passed to the worker
  • queue - The queue where a job should be enqueued, defaults to “default”
  • retry - Tells the Kiq worker to retry the enqueue job
  • retry_count - The number of times we’ve retried so far
  • at — A time at or after which a scheduled job should be performed, in Unix format
  • created_at - When the job was created, in Unix format
  • enqueue_at - When the job was enqueued, in Unix format
  • failed_at - The first time the job failed, in Unix format
  • retried_at — The last time the job was retried, in Unix format
  • error_message — The message from the last exception
  • error_class — The exception module (or class, in Sidekiq terms)
  • backtrace - The number of lines of error backtrace to store, defaults to none

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

Generate a compliant, entirely random, job id

Link to this section Types

Link to this type t() View Source
t() :: %Kiq.Job{
  args: [any()],
  at: Kiq.Timestamp.t(),
  class: binary(),
  created_at: Kiq.Timestamp.t(),
  enqueued_at: Kiq.Timestamp.t(),
  error_class: binary(),
  error_message: binary(),
  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()
}

Link to this section Functions

Link to this function decode(input) View Source
decode(input :: binary()) :: t() | no_return()

Decode an encoded job from JSON into a Job struct.

All keys are atomized, including keys within arguments. This does not use String.to_existing_atom/1, so be wary of encoding large maps.

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}
Link to this function encode(job) View Source
encode(job :: t()) :: binary() | no_return()

Encode a job as JSON.

During the encoding process any keys with nil values are removed.

Link to this function new(args) View Source
new(args :: map() | Keyword.t()) :: t()

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"}

iex> Kiq.Job.new(module: "Worker") |> Map.get(:class)
"Worker"
Link to this function random_jid(size \\ 12) View Source
random_jid(size :: pos_integer()) :: binary()

Generate a compliant, entirely random, job id.

Example

iex> Kiq.Job.random_jid() =~ ~r/^[0-9a-z]{24}$/
true

iex> job_a = Kiq.Job.random_jid()
...> job_b = Kiq.Job.random_jid()
...> job_a == job_b
false