Skip to content

Getting Started Ruby

Mike Perham edited this page Nov 10, 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

# push a dummy job to Faktory for us to immediately process
$pool = ConnectionPool.new { Faktory::Client.new(debug: true) }
$pool.with do |faktory|
  faktory.push({ queue: :critical, jobtype: 'SomeWorker', jid: SecureRandom.hex(8), args:[8,2,3,"\r\n"] })
  10.times {|idx| faktory.push({ jobtype: 'SomeWorker', jid: SecureRandom.hex(8), args:[1,2,3,"\r\n"], at: (Time.now.utc + idx).iso8601 })}
end

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. Now your apps can use Faktory::Client to push jobs to Faktory so that your faktory-worker process will process them. In fact, Faktory's Ruby API is very similar to Sidekiq -- if you know Sidekiq, much of this should look familiar.

Next Steps

Faktory 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.

Clone this wiki locally