防止管理命令一次运行多个 [英] Preventing management commands from running more than one at a time

查看:151
本文介绍了防止管理命令一次运行多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个长时间运行的进程,由Django管理命令触发,需要在相当频繁的基础上运行。这个过程应该每隔5分钟通过一个cron作业运行,但我想阻止它运行的第二个进程的实例,在罕见的情况下,第一次花费超过5分钟。

I'm designing a long running process, triggered by a Django management command, that needs to run on a fairly frequent basis. This process is supposed to run every 5 min via a cron job, but I want to prevent it from running a second instance of the process in the rare case that the first takes longer than 5 min.

我曾想过创建一个触摸文件,当管理进程启动时创建,并在进程结束时删除。然后,第二个管理命令进程将检查以确保在运行之前触摸文件不存在。但是,如果一个进程在没有正确删除触摸文件的情况下突然死机,这似乎是一个问题。似乎有一个更好的方法来做这个检查。

I've thought about creating a touch file that gets created when the management process starts and is removed when the process ends. A second management command process would then check to make sure the touch file didn't exist before running. But that seems like a problem if a process dies abruptly without properly removing the touch file. It seems like there's got to be a better way to do that check.

有没有人知道任何好的工具或模式,以帮助解决这类问题?

Does anyone know any good tools or patterns to help solve this type of issue?

推荐答案

因为这个原因,我喜欢有一个长时间运行的进程,使其工作离开共享队列。长期运行我的意思是它的寿命比一个单一的工作单位更长。然后,该过程由某些守护服务(例如 supervisord )控制,该服务可以在崩溃时接管重新启动进程的控制。这将工作适当地委派给知道如何管理进程生命周期的任务,并使您不必担心脚本范围内posix进程的性质。

For this reason I prefer to have a long-running process that gets its work off of a shared queue. By long-running I mean that its lifetime is longer than a single unit of work. The process is then controlled by some daemon service such as supervisord which can take over control of restarting the process when it crashes. This delegates the work appropriately to something that knows how to manage process lifecycles and frees you from having to worry about the nitty gritty of posix processes in the scope of your script.

如果你有一个队列,你也有能力启动多个进程,每个可以把工作从队列和处理它们,但这听起来像你的问题的范围。

If you have a queue, you also have the luxury of being able to spin up multiple processes that can each take jobs off of the queue and process them, but that sounds like it's out of scope of your problem.

这篇关于防止管理命令一次运行多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆