Worker is a database queue for small applications. You can easily create workers, add jobs to the queue and consume them.
All applications can benefit from background jobs, but most solutions are just too overkill for small apps. And you can always switch your queue for a better one when the database becomes a problem.
This library works only with ActiveRecord 3+.
rails install git://github.com/fnando/worker.git
sudo gem install worker
Then you have to generate a migration file. Run rails generate migration create_jobs and add the following code
class CreateJobs < ActiveRecord::Migration def self.up create_table :jobs do |t| t.string :name, :null => false t.integer :priority, :null => false, :default => 3 t.integer :tries, :null => false, :default => 5 t.integer :attempts, :null => false, :default => 0 t.text :last_error, :null => true t.datetime :run_at, :null => true t.datetime :locked_at, :null => true t.datetime :failed_at, :null => true t.datetime :finished_at, :null => true t.binary :payload, :null => false t.timestamps end add_index :jobs, [:run_at, :priority, :name, :attempts, :tries], :name => "jobs_index" end def self.down drop_table :jobs end end
If you want the source go to github.com/fnando/worker
require "worker" include Worker # The payload method is the object you send # to the queue and can be anything. worker(:mail) do payload.deliver end # Send an ActionMailer instance to the queue. queue(:mail, Notifier.welcome) # Consume the queue is simple as well! consume(:mail)
The consume part will be called probably in a Cron job or daemon.
If you're getting packet too large or server has gone away errors, read this post: skipmeamadeus.blogspot.com/2008/01/in-which-i-try-to-save-someone-from.html
If you're getting marshal data too short error, try to slightly increase the BLOB field.
create_table :mails do |t| t.binary :mail, :limit => 5.megabytes end
Nando Vieira - simplesideias.com.br
(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.
Copyright © 2010 Nando Vieira, released under the MIT license