delayed_job vs. cron
I have a system where users come in to go through an application process that has multiple parts - sometimes users will save their progress and come back later.
I want to send users an e-mail if they haven't come back in 48 hours - would it be best to do this using cron
, delayed_job
, or whenever
?
I've noticed that whenever I run operations in the console (such as bundle install
or rake db:migrate
) it runs cron as well, which makes me suspicious that we may have instances where users get multiple reminders in the same day.
What 开发者_JS百科are your recommendations for this?
First of all, Whenever and Cron are synonymous. All Whenever does is provide a way for you to write cronjobs using Ruby (which is awesome, I love Whenever).
Delayed_job is not the answer here. You definitely want to use cronjobs. Create a method on your Application model that will get applications which have an updated_at
value of < 2.days.ago
and e-mail its applicant.
def notify_stale_applicants
@stale_applications = Application.where('updated_at < ?', 2.days.ago) # or 48.hours.ago
@stale_applications.each do |app|
UserMailer.notify_is_stale(app).deliver
end
end
And your UserMailer:
def notify_is_stale(application)
@application = application
mail(:to => application.user.email, :from => "Application Status <status@yourdomain.com>", :subject => "You haven't finished your Application!"
end
Using whenever to create this cron:
every :day, :at => '8am' do
runner 'Application.notify_stale_applicants'
end
精彩评论