Skip to content
Arjun Singh edited this page Feb 6, 2023 · 3 revisions

Faktory::Job

Faktory::Job is a module which provides an API very similar to the Sidekiq::Worker API. You include the module in every background job class.

class SomeJob
  include Faktory::Job
  faktory_options queue: "critical", custom: { unique_for: 10.minutes }

  def perform(*args)
  end
end

SomeJob.perform_async(1, 2, "bob")
SomeJob.perform_in(5.minutes, 1, 2, "bob")
# use `set` to dynamically override or set options
SomeJob.set(custom: { unique_for: nil }).perform_in(5.minutes, 1, 2, "bob")

Low-level Access

Faktory::Client provides a low-level API for creating background jobs and accessing other Faktory commands. You typically won't need to use this API unless you are doing something unusual.

Connecting

By default, the Faktory ruby API will connect to localhost:7419. Use FAKTORY_PROVIDER and FAKTORY_URL variables to define any custom Faktory location.

# FAKTORY_URL=tcp+tls://faktory.example.com:7419 bundle exec rails server ...
# FAKTORY_PROVIDER=SOME_FAKTORY_URL SOME_FAKTORY_URL=tcp+tls://faktory.example.com:7419 bundle exec rails server ...

Instantiating a client will find and use those ENV variables. You need to set those variables for any Ruby process which uses Faktory.

c = Faktory::Client.new
c = Faktory::Client.new(debug: true) # logs any network protocol chatter

Pushing a Job

Use push(job_hash) to push a job to Faktory. There's 3-4 required elements for the hash. This is a good API to use if you are creating jobs to be processed by another language (e.g. python, go, node).

c.push({
  "jobtype" => "foo", 
  "queue" => "critical", 
  "retry" => 10, 
  "args" => [1, 2, "Some String"], 
  "custom" => { ... }
})

Read through faktory/client for the actual implementation.

Clone this wiki locally