PHP - 防止 Cron 中的冲突 - 文件锁定安全吗?
我正在尝试找到一种安全的方法来防止 cron 作业冲突(即,如果另一个实例已经在运行,则阻止它运行)。
我发现推荐的一些选项使用文件上的锁。
这真的是一个安全的选择吗?例如,如果脚本死亡会发生什么?锁会保留吗?
还有其他方法可以做到这一点吗?
I'm trying to find a safe way to prevent a cron job collision (ie. prevent it from running if another instance is already running).
Some options I've found recommend using a lock on a file.
Is that really a safe option? What would happen if the script dies for example? Will the lock remain?
Are there other ways of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此示例取于 http://php.net/flock 并进行了一些更改,这是一个 正确方式做你想做的事:
不要使用
/tmp
或/var/tmp
等位置,因为它们可能是系统随时清理,从而按照文档弄乱您的锁:https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.html
https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html
务必使用您控制下的位置。
致谢:
w+
而不是r+< /代码>
This sample was taken at http://php.net/flock and changed a little and this is a correct way to do what you want:
Do not use locations such as
/tmp
or/var/tmp
as they could be cleaned up at any time by your system, thus messing with your lock as per the docs:https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.html
https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html
Do use a location that is under your control.
Credits:
w+
instead ofr+
在 Symfony 框架中,您可以使用锁组件 symfony/lock
https ://symfony.com/doc/current/console/lockable_trait.html
In Symfony Framework you could use the lock component symfony/lock
https://symfony.com/doc/current/console/lockable_trait.html
我扩展了 zerkms 的概念,创建了一个可以从 cron 启动时调用的函数。
使用 Cronlocker,您可以指定一个锁名称,然后指定 cron 关闭时要调用的回调函数的名称。您可以选择提供一个参数数组来传递给回调函数。如果您需要在锁定打开时执行不同的操作,还有一个可选的回调函数。
在某些情况下,我遇到了一些异常,并希望能够捕获它们,因此我添加了一个处理致命异常的函数,应该添加该函数。我希望能够从浏览器访问该文件并绕过 cronlock,所以它是内置的。
我发现,当我经常使用它时,有些情况下我想在这个 cron 运行时阻止其他 cron 运行,所以我添加了一个可选的锁块数组,它们是要阻止的其他锁名称。
然后在某些情况下,我希望这个 cron 在其他 cron 完成后运行,因此有一个可选的锁等待数组,它们是其他锁名称,要等待直到没有一个 cron 运行。
简单的例子:
回调参数和失败函数:
阻塞和等待:
类:
也应该在公共页面上实现,或者内置到现有的致命错误处理程序中:
I've extended the concept from zerkms to create a function that can be called from the start of a cron.
Using the Cronlocker you specify a lock name, then the name of a callback function to be called if the cron is OFF. Optionally you may give an array of parameters to pass to the callback function. There's also an optional callback function if you need to do something different if the lock is ON.
In some cases I got a few exceptions and wanted to be able to trap them, and I added a function for handling fatal exceptions, which should be added. I wanted to be able to hit the file from a browser and bypass the cronlock, so that's built in.
I found as I used this a lot there were cases where I wanted to block other crons from running while this cron is running, so I added an optional array of lockblocks, which are other lock names to block.
Then there were cases where I wanted this cron to run after other crons had finished, so there's an optional array of lockwaits, which are other lock names to wait until none of which are running.
simple example:
callback parameters and failure functions:
blocking and waiting:
class:
Should also be implemented on a common page, or built into your existing fatal error handler: