在特定时间在 Rails 中执行操作

发布于 2024-09-09 07:37:31 字数 134 浏览 5 评论 0原文

有没有办法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所有深爱都是秘密 2024-09-16 07:37:31

延迟作业中有一个 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 a after_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

口干舌燥 2024-09-16 07:37:31

您最好编写一个 ruby​​ 脚本,在您需要的确切时间通过 crontab 运行。

You'd be better off writing a ruby script that runs via crontab at the exact time you need it to.

苍暮颜 2024-09-16 07:37:31

我同意 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文