问题描述
我有很多Appointment
模型在一天的不同时间开始,分别是:00,:15,:30,:45 .我想触发代码以在活动开始前 1小时发送提醒.使用后台工作者触发此操作的最佳方法是什么?我也在使用发条 gem,因此我可以安排Sidekiq工人.
I have many Appointment
models that start at various times of the day, either at :00, :15, :30, :45. I'd like to trigger code to send a reminder 1 hour before the event starts. What would be the best way to use a background worker to trigger this? I'm using the clockwork gem as well so I can schedule Sidekiq workers.
推荐答案
clockwork
gem用于固定计划作业(替代cron).您将要使用sidekiq
随附的ActionMailer.delay_until
:
The clockwork
gem is for fixed schedule jobs (a replacement for cron). You'll want to use ActionMailer.delay_until
that comes with sidekiq
:
class Appointment
after_create :queue_reminder
def queue_reminder
MyMailer.delay_until(event_time - 1.hour).appointment_reminder(id)
end
end
在此处查看sidekiq
文档: https://github.com/mperham/sidekiq/wiki/Delayed-Extensions
正如shock_one所述,如果用新日期更新约会,则必须重新安排提醒并取消旧的提醒.如果Appointment
被销毁,您还想取消作业.
As shock_one mentioned, if you update an appointment with a new date, you'll have to requeue a reminder and cancel the old one. You'll also want to cancel a job if an Appointment
is destroyed.
为此,我建议您使用 sidekiq-status
和reminder_job_id
柱子.您的Appointment
模型将如下所示:
For that, I'd advise you use sidekiq-status
, and a reminder_job_id
column. Your Appointment
model would then look something like:
class Appointment
before_save :queue_reminder, if: :event_time_changed?
after_destroy :cancel_reminder, if: :reminder_job_id?
def queue_reminder
cancel_reminder if reminder_job_id
self.reminder_job_id = MyMailer.delay_until(event_time - 1.hour)
.appointment_reminder(id)
end
def cancel_reminder
Sidekiq::Status.cancel reminder_job_id
end
end
这篇关于在活动开始前一小时触发的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!