Qe
A simple interface over several background job libraries like Resque, Sidekiq and DelayedJob.
Usage
In this wild world where a new asynchronous job processing library is released every once in a while, Qe tries to keep a unified interface that works with the most famous libraries:
To set the adapter, just load the file according to your adapter:
require "qe/resque"
require "qe/qu"
require "qe/delayed_job"
require "qe/beanstalk"
You also need to require the library you're going to use. If you're using Rails with Bundler, you can simple require the correct file and dependency.
source :rubygems
gem "rails", "3.2.8"
gem "sidekiq"
gem "qe", :require => "qe/sidekiq"
gem "resque"
gem "qe", :require => "qe/resque"
gem "qu"
gem "qe", :require => "qe/qu"
gem "backburner"
gem "qe", :require => "qe/beanstalk"
Create a worker that will send e-mails through ActionMailer
.
class MailerWorker
include Qe::Worker
def perform
Mailer.public_send(options[:mail], options).deliver
end
end
Define our Mailer
class.
class Mailer < ActionMailer::Base
def welcome(options)
@options = options
mail :to => options[:email]
end
end
Enqueue a job to be processed asynchronously.
MailerWorker.enqueue({
:mail => :welcome,
:email => "[email protected]",
:name => "John Doe"
})
You can specify when this job must be processed by setting the :run_at
option.
MailerWorker.enqueue({
:mail => :follow_up,
:email => "[email protected]",
:name => "John Doe",
:run_at => 5.days.from_now
})
Setting the queue name
Sometimes setting the queue name is important.
class HelloWorker
include Qe::Worker
queue :hello
end
Defining actions
Sometimes you want to create several actions in a single worker, just because is easier. Instead of manually dispatch the action on your perform method, you can
just add the Qe::Action
module.
class NotificationWorker
include Qe::Worker
include Qe::Action
def shutdown
puts options[:message]
end
def startup
puts options[:message]
end
def default
puts options[:message]
end
end
Now, you can just enqueue jobs by defining the :action
option. If no action is defined, then the default is executed.
NotificationWorker.enqueue(:action => :shutdown, :message => "shutting down")
NotificationWorker.enqueue(:action => :startup, :message => "starting up")
NotificationWorker.enqueue(:message => "wat?")
The action must be a existing public method. If not defined, Qe::Action::MissingActionError
exception is raised.
Working with I18n
When you're working in internationalized app, you need to perform some jobs with the correct locale set (sending an e-mail, maybe?). Instead of doing it manually, you can just use the Qe::Locale
extension.
This extension will set the :locale
option when you enqueue some job and set it to I18n.locale
when you perform it. Just include it after the Qe::Worker
module.
class MailerWorker
include Qe::Worker
include Qe::Locale
def perform
Mailer.public_send(options[:mail], options).deliver
end
end
This approach is good enough, but if you're using ActionMailer, try the Qe::ActionMailer
instead, combining this extension as well.
ActionMailer integration
Qe comes with an extension to send e-mails through ActionMailer. You can set the mailer and which mail will be sent.
class Mailer < ActionMailer::Base
def welcome(options)
@options = options
mail :to => options[:email]
end
end
class MailerWorker
include Qe::Worker
include Qe::ActionMailer
include Qe::Locale
def mailer
Mailer
end
end
MailerWorker.enqueue(
:mail => :welcome,
:name => "John Doe",
:email => "[email protected]"
)
If the mailer()
method isn't defined, the Qe::ActionMailer::AbstractMethodError
exception will be raised.
If the :mail
option isn't defined, the Qe::ActionMailer::MissingMailNameError
exception will be raised.
Sidekiq
You can set Sidekiq options by using the method Sidekiq::Worker.options
.
require 'qe/sidekiq'
class NonRetryableWorker
include Qe::Worker
options retry: false
def perform
# do something...
end
end
Development support
Qe comes with development support. Instead of starting up workers on development environment, you can use the Qe::Immediate
adapter, which executes your worker right away!
Qe.adapter = Qe::Immediate
If you're using Rails, you can add the line above to your config/environments/development.rb
file.
Testing support
Qe also comes with testing support. Just require the qe/testing.rb
file
and a fake queuing adapter will be used. All enqueued jobs will be stored
at Qe.jobs
. Note that this method is only available on testing mode.
require "qe/testing"
Qe.adapter = Qe::Testing
If you are using RSpec, you can require the qe/testing/rspec.rb
file
instead. This will reset Qe.jobs
before every spec and will add a
enqueue
matcher.
# Add the following like to your spec_helper.rb file
require "qe/testing/rspec"
describe "Enqueuing a job" do
it "enqueues job" do
expect {
# do something
}.to enqueue(MailerWorker).with(:email => "[email protected]")
end
end
Maintainer
- Nando Vieira (http://nandovieira.com.br)
License:
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.