Skip to content

Getting Started Ruby

Mike Perham edited this page Dec 20, 2018 · 7 revisions

Want to test drive Faktory with Ruby? Here's how.

Install Faktory

See the Installation page for options but the easiest method is Homebrew on macOS:

brew tap contribsys/faktory
brew install faktory

It'll take a minute or two to build. Now run faktory in your Terminal. Leave it running and open a new terminal for our next step.

Open your browser

Open http://localhost:7420 to see the Web UI. It's empty, let's fix that! Leave your browser running.

Create a Ruby app

In your terminal, create app.rb:

require 'connection_pool'
require 'faktory'
require 'securerandom'

class SomeWorker
  include Faktory::Job

  def perform(*args)
    puts "Hello, I am #{jid} with args #{args}"
    sleep 1
  end
end

# schedule a job to execute ASAP
SomeWorker.perform_async(1,2,3)
# schedule a bunch of jobs to execute a few seconds in the future
10.times {|idx| SomeWorker.perform_in(idx, 1, 2, 3) }

and Gemfile:

source "https://rubygems.org"
gem 'faktory_worker_ruby'

Finally, run:

bundle
bundle exec faktory-worker -r ./app.rb

You should see output like this, showing 10 jobs running over the next 10 seconds:

2017-11-10T23:49:31.561Z 49747 TID-oum1sndj0 SomeWorker JID-10560ae319c36e5f INFO: done: 1.002 sec
2017-11-10T23:49:31.562Z 49747 TID-oum1sncuk SomeWorker JID-2a9824174a5f2764 INFO: done: 1.002 sec
2017-11-10T23:49:31.562Z 49747 TID-oum1sndcw SomeWorker JID-afeb409a9904e90e INFO: done: 1.002 sec
2017-11-10T23:49:31.562Z 49747 TID-oum1snd6s SomeWorker JID-036b5b7e1de9321c INFO: done: 1.002 sec
2017-11-10T23:49:31.562Z 49747 TID-oum1nssb8 SomeWorker JID-c233c86097a8e699 INFO: done: 1.002 sec

Beyond that, the faktory-worker process will sit quietly waiting for jobs which will never come. Sad!

ActiveJob

Of course FWR supports ActiveJob, Rails's background job subsystem. See the ActiveJob wiki page.

Next Steps

Your apps can use Faktory::Client to push jobs to Faktory so that your faktory-worker process will process them. In fact, faktory_worker_ruby's API is very similar to Sidekiq -- if you know Sidekiq, much of this should look very familiar:

class SomeWorker
  include Faktory::Job
  faktory_options retry: 5

  def perform(*args)
    puts "Hello, I am #{jid} with args #{args}"
    sleep 1
  end
end

SomeWorker.perform_async(1, 2, 3)

You can point faktory_worker_ruby to a custom Faktory location with the FAKTORY_PROVIDER and FAKTORY_URL environment variables:

FAKTORY_PROVIDER=FAKTORY_URL FAKTORY_URL=tcp://internal-host.private-network.example.com:7419 bundle exec faktory-worker
Clone this wiki locally