本文介绍了如何确保 rake 任务一次只运行一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 crontab 在某个时间调用 rake 任务,例如:每 3 小时

I use crontab to invoke rake task at some time for example: every 3 hour

我要确保当 crontab 准备好执行 rake 任务时它可以检查 rake 任务是否正在运行.如果是这样,请不要执行.

I want to ensure that when crontab ready to execute the rake taskit can check the rake task is running. if it is so don't execute.

如何做到这一点.谢谢.

how to do this. thanks.

推荐答案

您可以为此使用锁定文件.当任务运行时,尝试获取锁,如果获得锁,则运行 rake 任务.如果你没有拿到锁,那就不要运行 rake;您可能还想在某处记录错误或警告,或者您可能会在您知道之前几周或几个月内没有做任何事情而结束您的 rake 任务.当 rake 退出时,解锁锁定文件.

You could use a lock file for this. When the task runs, try to grab the lock and run the rake task if you get the lock. If you don't get the lock, then don't run rake; you might want to log an error or warning somewhere too or you can end up with your rake task not doing anything for weeks or months before you know about it. When rake exits, unlock the lock file.

RAA 之类的东西可能会有所帮助,但我没有使用过,所以可能没有.

Something like RAA might help but I haven't used it so maybe not.

您也可以使用 PID 文件.您将在某处拥有一个文件,其中包含 rake 进程进程 ID.在启动 rake 之前,您从该文件中读取 PID 并查看进程是否正在运行;如果不是,则启动 rake 并将其 PID 写入 PID 文件.当 rake 存在时,删除 PID 文件.如果您想非常严格,您可能希望将其与锁定 PID 文件相结合,但这取决于您的特定情况.

You could also use a PID file. You'd have a file somewhere that holds the rake processes process ID. Before starting rake, you read the PID from that file and see if the process is running; if it isn't then start up rake and write its PID to the PID file. When rake exists, delete the PID file. You'd want to combine this with locking on the PID file if you want to be really strict but this depends on your particular situation.

这篇关于如何确保 rake 任务一次只运行一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 18:21