如何确保一次只运行一个 rake 任务?

发布于 2024-09-18 06:44:27 字数 151 浏览 6 评论 0原文

我想设置 rake 任务通过 cron 运行。这很容易,但不容易的是确保一次只运行该 rake 任务的一个副本。我想我可以在系统上使用 ps 来检查并退出 rake 任务(如果它已经在运行),或者我可以执行延迟作业结构,在其中序列化任务名称,完成后将其从数据库中删除,但是你们都推荐什么?

I want to setup rake tasks to run via cron. That is easy, but what is not easy is ensuring that only one copy of that rake task is running at a time. I imagine I could use ps on the system to check and then exit the rake task if it is already running, or I could do a delayed job structure where I serialize the name of the task and upon completion I remove it form the DB, but what do you all recommend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

摇划花蜜的午后 2024-09-25 06:44:33

您可以使用 https://rubygems.org/gems/only_one_rake,例如
命名空间:rails 做
desc “在线用户数”
only_one_task :online_user_count =>; : 环境做
循环 { OnlineUser.count;睡觉 1 }
结尾
结尾

You can use https://rubygems.org/gems/only_one_rake, like
namespace :rails do
desc "online user count"
only_one_task :online_user_count => :environment do
loop { OnlineUser.count; sleep 1 }
end
end

触ぅ动初心 2024-09-25 06:44:32

Unix 通过 pid 文件解决了这个问题。 Pid 文件位于 /var/run 中并包含程序进程 ID。这是手册页: http://fuse4bsd.creo.hu/ localcgi/man-cgi.cgi?pidfile+3

您可能会发现它有点过时(我同意),但它是一种经常使用且经过验证的方法。

Unix solved this issue with pid files. Pid files are located in /var/run and contain the programs process Id. Here is the man page: http://fuse4bsd.creo.hu/localcgi/man-cgi.cgi?pidfile+3

You might find it a bit old fashioned (and I agree), but it's an often used and proven method.

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