Create Recurring job using delayed job
July 29th, 2010 § 4 Comments
basically delayed job is using for background job. but we can use it as a recurring job.
here’s the step
first, install delayed_ojb gem. you can see how to install it at
http://github.com/collectiveidea/delayed_job
then add a new library to your rails apps directory ( /Libs ) e.g: jobs.rb
module Jobs
module ScheduledJob
def self.included(base)
puts "WORK !!!"
base.extend(ClassMethods)
end
def perform_with_schedule
Delayed::Job.enqueue self, 0, self.class.schedule.from_now.getutc
perform_without_schedule
end
module ClassMethods
def method_added(name)
if name.to_s == "perform" && !@redefined
@redefined = true
alias_method_chain :perform, :schedule
end
end
def schedule
@schedule
end
def run_every(time)
@schedule = time
end
end
end
end
after that, you can include your job class to your delayed_job script :
class recurring_job
include ScheduledJob
run_every 1.hours
def perform
puts "perform job"
end
end
then run the job in the console :
RAILS_ENV=production script/delayed_job start
rake jobs:work
stop the job :
RAILS_ENV=production script/delayed_job stop
Hi, I’m new with ruby on rails.
I have problem with delayed job in my application.
I add delayed job when i send email. but gmail have daily quota, how can i get error, and delayed again that job tomorrow?!?
Any suggest?!?
thx
Hi Idris,
sorry for replyin your comment very late.. the notifications goes to my old email addres..
I thought you have the answer for your question.. pelase share with me if you don’t mind
thanks
Thanks for the ideas .. a suggestion: comments and code indentation would help to understand what you’re trying to do in the code and also help the reader detect any typo errors.
Hi Daudi,
I add indentation to the code and fix some language “error” hehe
Thanks for visit