在特定时间在 Rails 中执行操作
有没有办法在 ROR 中设置在特定时间触发的回调?
假设我正在举办一场在某个时间到期的比赛。假设 7 月 28 日星期一 9:00。我想设置一个观察者,在 7 月 28 日星期一 9:00 运行一个函数。 Rails 有办法做到这一点吗?
Is there a way to setup a callback in ROR that would trigger at a specific time?
Lets say I'm running a contest that expries at a certain time. Lets say Monday July 28th at 9:00. I'd like to set up an observer that ran a function at Monday July 28th at 9:00. Does rails have a method to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
延迟作业中有一个 run_at 字段。您必须在后台有一个工作进程始终运行并寻找一组要运行的作业,但如果您的应用程序经常这样做,那么它可能比总是编写新的 cron 作业更容易。
因此,您可以在
Contest
模型中有一个方法,该方法在after_create
回调中调用,该方法设置延迟作业,以便在指定日期向随机获胜者发送电子邮件这是指定的。不过,如果这是一次性的或非常罕见的交易,我会同意每当
There's a run_at field in Delayed Job. You have to have a worker process in the background always running and looking for jobs that a set to run, but if your application is doing this a lot, it might be easier than always writing new cron jobs.
So, you could have a method in your
Contest
model that gets called in aafter_create
callback that sets up a delayed job to send out an email to a random winner at the date that's specified.If it's a one time, or very infrequent deal, though, I'll agree about using whenever
您最好编写一个 ruby 脚本,在您需要的确切时间通过 crontab 运行。
You'd be better off writing a ruby script that runs via crontab at the exact time you need it to.
我同意 crontab,但我喜欢无论何时。它很好地集成到 cron 中,您可以将其与存储库一起存储,并且它与 capistrano 很好地集成。
I agree about crontab, but I like whenever. It has a nice integration into cron that you can store with your repo and it integrates nicely with capistrano.