Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fnando/qe
A simple interface over several background job libraries like Resque, Sidekiq and DelayedJob.
https://github.com/fnando/qe
Last synced: 3 months ago
JSON representation
A simple interface over several background job libraries like Resque, Sidekiq and DelayedJob.
- Host: GitHub
- URL: https://github.com/fnando/qe
- Owner: fnando
- Archived: true
- Created: 2012-11-09T08:22:31.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2015-02-03T16:56:25.000Z (almost 10 years ago)
- Last Synced: 2024-07-24T10:48:48.765Z (4 months ago)
- Language: Ruby
- Size: 255 KB
- Stars: 102
- Watchers: 4
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:* [Sidekiq](http://mperham.github.com/sidekiq/)
* [Resque](https://github.com/defunkt/resque/)
* [DelayedJob](https://github.com/collectiveidea/delayed_job)
* [Qu](https://github.com/bkeepers/qu)
* [Beanstalk](https://github.com/kr/beanstalkd)/[Backburner](http://nesquena.github.com/backburner/)To set the adapter, just load the file according to your adapter:
``` ruby
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.
``` ruby
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`.
``` ruby
class MailerWorker
include Qe::Workerdef perform
Mailer.public_send(options[:mail], options).deliver
end
end
```Define our `Mailer` class.
``` ruby
class Mailer < ActionMailer::Base
def welcome(options)
@options = options
mail :to => options[:email]
end
end
```Enqueue a job to be processed asynchronously.
``` ruby
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.
``` ruby
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.
```ruby
class HelloWorker
include Qe::Workerqueue :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.``` ruby
class NotificationWorker
include Qe::Worker
include Qe::Actiondef shutdown
puts options[:message]
enddef startup
puts options[:message]
enddef 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.
``` ruby
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.
``` ruby
class MailerWorker
include Qe::Worker
include Qe::Localedef 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.
```ruby
class Mailer < ActionMailer::Base
def welcome(options)
@options = options
mail :to => options[:email]
end
endclass MailerWorker
include Qe::Worker
include Qe::ActionMailer
include Qe::Localedef mailer
Mailer
end
endMailerWorker.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`.
```ruby
require 'qe/sidekiq'class NonRetryableWorker
include Qe::Worker
options retry: falsedef 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!
``` ruby
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.``` ruby
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.``` ruby
# 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 ()
## 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.