Instant queue. Background processing and message driven communication tool. Faster and simplier alternative to Resque.
Add this line to your application's Gemfile:
gem 'insque', :git => 'https://github.com/Gropher/Insque.git'
And then execute:
$ bundle
Or install it yourself as:
$ gem install insque
At first you need to generate initializer and redis config file. Pass your sender name as parameter. Sender name is the unique identifier of your instance of insque. You can use several instances of insque to create message driven communication system
$ rails g insque:initializer somesender
To broadcast message use:
Insque.broadcast :message_name, {:params => {:some => :params}}
There is an easy way to use insque as background jobs processing. You can use send_later
method to call any method of your rails models in background
. You still need listener running to make this work.
@model = MyModel.first
@model.send_later :mymethod, 'some', 'params'
To start recieving messages you need to:
- Create handler method in Insque module. First part of handler name is the name of the message sender.
def somesender_message_name message
#TODO: Handle message somehow
end
- Call
listen
method in some background process or rake task:
Insque.listen
or just run bundle exec rake insque:listener
from your console.
- Call
janitor
method in some background process or rake task. Janitor will reissue failed messages or report error if message fails again. Janitor treats message as failed if it was not processed for an hour after broadcast or reissue.
Insque.janitor
or just run bundle exec rake insque:janitor
from your console.
If you want to run insque listener as a daemon consider using foreman for this.
Add foreman to your Gemfile
:
gem 'foreman', :git => 'git://github.com/Gropher/foreman.git'
Install it:
$ bundle install
Create Procfile
:
listener: bundle exec rake insque:listener
janitor: bundle exec rake insque:janitor
Run foreman from your console:
$ bundle exec foreman start
For production use modify your capistrano deploy script somewhat like this:
set :default_environment, {'PATH' => "/sbin:$PATH"} # Optional. Useful if you get errors because start or restart command not found
default_run_options[:pty] = true # Needed for RVM to work
set :sudo, 'rvmsudo' # Or just 'sudo' if you don't use RVM. You may also need to modify /etc/sudoers on server.
set :foreman_concurrency, "\"listener=4,janitor=2\"" # How many processes of each type do you want
require 'foreman/capistrano'
after "deploy:update", "foreman:export" # Export Procfile as a set of upstart jobs
after "deploy:update", "foreman:restart" # You will need upstart installed on your server for this to work.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request