Skip to content

Getting Started Ruby

Mike Perham edited this page Nov 13, 2017 · 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!

Next Steps

faktory_worker_ruby doesn't currently have an ActiveJob adapter but I expect that to be fixed quickly; that's a natural next step if you want to quickly migrate an existing Rails application.

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, priority: 3

  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_URL environment variable:

FAKTORY_URL=tcp://internal-host.private-network.example.com:7419 bundle exec faktory-worker

See the Security page for tips on using Faktory over a network.

Clone this wiki locally