About
Redisque is yet another lightweight and simple to use queue manager with Redis DB backend.
Installation
Run $ sudo gem install redisque
How to use
Redisque has only three methods that you will employ:
Redisque.connect(options)
with the help of this method you can customize your connection to Redis DB. options
is a Hash
and can have the keys: :address, :port, :password
and :db
.
Redisque.enqueue(queue, data)
makes possible to add task represented by data
(Hash) to queue
(String).
Redisque.workoff(queue, pause = 5, &block)
manages the queue: asks the queue
for the next entry (job that is represented with JSONized Hash) and if it gets one then the block
is executed with data received otherwise sleep for pause
time set in seconds, this way we give a chance to another processes to do their job :)
Usage scenario might look like:
in your application:
require "redisque"
[
{:id => 1, :city => 'New York'},
{:id => 2, :city => 'Pekin'},
{:id => 3, :city => 'Mumbai'},
{:id => 4, :city => 'Paris'},
{:id => 5, :city => 'Madrid'}
].each {|chunk| Redisque.enqueue('my_queue', chunk)}
in your [Worker] *.rb file:
require "redisque"
Redisque.connect(:password => 'secret')
Redisque.workoff('my_queue') do |data|
puts "Next city is: %s" % data["city"]
end
Halting Redisque:
Redisque has a key halting
and you can use it to halt the execution, i.e. in your [Worker] *.rb file:
trap('INT') do
print "1. (Q)uit now\n2. (W)ait for current task to finish\n3. (C)ontinue\n"
case gets.chomp
when 'Q'
exit
when 'W'
Redisque.halting = true
print 'Redisque instance resumes until current Job is finished'
else
end
end
trap('TERM') {exit}
require "json"
require "redisque"
...
Caution
Redisque creates backup queue when it's run with the following name convention:
original queue
name that is preceded with asterisk symbol
e.g. if your queue name was my_queue
then the backup queue name would be *my_queue
so you should keep it in mind when working with other queues that some names are already automatically reserved for backup queues
In case Worker was interrupted, backup queue data (if any) will be shifted into active queue on next Worker start, thus if task was running but didn't succeed, it's entry will be restored from backup queue. For your Worker to function properly it has to be able to redo all steps regardless the execution moment it was stopped at.
Contribute to project
Please do.
If you feel that it lacks some functionality or enhancement or anything else, please feel free to:
- clone the project
- create a branch do your changes or improvements in it and
- make a pull request
to review code and merge the branch.
Thank you
License
MIT License
Copyright (c) 2012 Yuri Belogub
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.