Delayed Job
Delayed Job is a plugin used to execute long running jobs in background. For example, downloads, uploads, external interactions etc. And you can also specify when to start a delayed job.
To install delayed_job plugin:
script/plugin install git://github.com/tobi/delayed_job.git
After installation you need to create a table for the delayed job to save the jobs.
script/generate migration create_table_for_delayed_job
And add the following script to the migration file.
class CreateTableForDelayedJob < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |t|
t.integer :priority, :default => 0
t.integer :attempts, :default => 0
t.text :handler
t.text :last_error
t.datetime :run_at
t.datetime :locked_at
t.datetime :failed_at
t.string :locked_by
t.timestamps
end
end
def self.down
drop_table :delayed_jobs
end
end
Now run the migration:
rake db:migrate
To add a job to delayed_job you can use the enqueue method of Delayed Job. First you have to create a Job Class, for example we need to send a notification mail using delayed job. Then we create a lib file with the content:
Class DelayedNotificationMail
attr_accessor :id
def initialize(id)
self.id = id
end
def perform
end
Now we can add job to the delayed_job using:
Delayed::Job.enqueue DelayedNotificationMail.new(@user_id), 0, 1.hour.from_now
To run the jobs in background you have to execute the rake task:
rake jobs:work
This will start a process which will run the jobs in the queue, and will run the job according to the delay specified in the job.
To clear all the jobs in queue you can use the rake task:
rake jobs:clear
For more details you can go to the GitHub
blog comments powered by Disqus
To install delayed_job plugin:
script/plugin install git://github.com/tobi/delayed_job.git
After installation you need to create a table for the delayed job to save the jobs.
script/generate migration create_table_for_delayed_job
And add the following script to the migration file.
class CreateTableForDelayedJob < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |t|
t.integer :priority, :default => 0
t.integer :attempts, :default => 0
t.text :handler
t.text :last_error
t.datetime :run_at
t.datetime :locked_at
t.datetime :failed_at
t.string :locked_by
t.timestamps
end
end
def self.down
drop_table :delayed_jobs
end
end
Now run the migration:
rake db:migrate
To add a job to delayed_job you can use the enqueue method of Delayed Job. First you have to create a Job Class, for example we need to send a notification mail using delayed job. Then we create a lib file with the content:
Class DelayedNotificationMail
attr_accessor :id
def initialize(id)
self.id = id
end
def perform
UserMailer.deliver_notification_mail (User.find(id))
endend
Now we can add job to the delayed_job using:
Delayed::Job.enqueue DelayedNotificationMail.new(@user_id), 0, 1.hour.from_now
To run the jobs in background you have to execute the rake task:
rake jobs:work
This will start a process which will run the jobs in the queue, and will run the job according to the delay specified in the job.
To clear all the jobs in queue you can use the rake task:
rake jobs:clear
For more details you can go to the GitHub