从 Linux 上的 Web 环境安排作业
我正在 Linux/Apache 上用 PHP 开发一个应用程序。我希望能够在应用程序内安排 PHP 作业(脚本)在将来的某个特定时间执行。
我知道很多人会推荐 cron 和 at,但首先我不需要重复(cron),其次也是最重要的是,我需要能够扩展的解决方案。 At 的设计并未考虑竞争条件,如果两个用户尝试同时添加一项作业,其中一个或两个可能会失败。
同样重要的是,作业在指定时间执行,而不仅仅是每分钟左右“轮询”一次。
有人可以建议这项任务的解决方案吗?谢谢。
I am developing an application in PHP on Linux/Apache. I want to be able to schedule PHP jobs (scripts) for execution at some specific time in the future from within the application.
I know that many people will recommend cron and at, but first of all I don't need recurrence (cron) and secondly and most importantly, I need the solution to be able to scale. At was not designed with race condititions in mind, and if two users try to add a job at the same time one or both may fail.
It's also important that jobs are executed at their specified time, and not just 'polled' once per minute or so.
Can anyone please suggest solutions for this task? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编写一个 php 脚本作为守护进程
该脚本在后台运行,每隔几微秒唤醒一次,检查要执行的任务。如果它有一些任务要执行,它可以分叉自己并启动任务。
如果您不喜欢脚本每隔几微秒唤醒一次,您还可以计算下一个任务的睡眠时间,然后分叉来启动它。当添加或删除任务时,只需向程序发送信号,程序就会醒来并重新计算现在需要休眠多长时间。
超链接:
http://en.wikipedia.org/wiki/Daemon_%28computer_software%29
http://php.net/manual/en/function.pcntl-fork .php
http://www.php.net/manual /en/function.pcntl-signal.php
write a php-script as a daemon
the script runs in the background, wakes up every some microseconds, checks for tasks to execute. if it has some tasks to execute, it could then fork itself and start the task.
if you don't like, that the script wakes up every some microseconds, you could also calculate the usleep time to the next task and then fork to start it. when tasks are being added or deleted, just send signal to the program and it then wakes up and recalculates how long it has to sleep now.
hyperlinks:
http://en.wikipedia.org/wiki/Daemon_%28computer_software%29
http://php.net/manual/en/function.pcntl-fork.php
http://www.php.net/manual/en/function.pcntl-signal.php
你可以尝试 gearman
you can try gearman
您可以(我会)这样做:
This is what you could(I would) do:
在 POSIX 系统上,应该有 at-daemon 它基本上是一个一次性版本cron:
可以通过 php 的 exec 调用(或者任何用于通过 php 执行 shell 命令的东西)。
重新实现现有的解决方案是非常糟糕的做法™:
On POSIX systems, there should be the at-daemon which is basically a one shot version of cron:
which could be called via php's exec (or whatever is used to execute shell commands via php).
Reimplementing already existing solutions is Very Bad Practise ™:
这是使用 MySQL 的 Yii2 解决方案。如果将查询/状态更新放在事务中,则可以在不同的机器上有多个工作人员。
cron table
Cron class
然后,您可以在班级中执行任务,例如
完成工作并在工作完成时更新工作进度。
Here is a solution for Yii2 using MySQL. If you put the query/status update in a transaction, you can have multiple workers on different machines.
cron table
Cron class
Then you can have tasks in the class like
which do the work and update the progress of the job as it completes.