简体   繁体   English

我可以在单个进程中运行FuelPHP Task吗?

[英]Can I run FuelPHP Task in single process ?

I want to run FuelPHP Task every minute by cron. 我想由cron每分钟运行一次FuelPHP Task。 But I don't want to run duplicate FuelPHP Task. 但是我不想运行重复的FuelPHP Task。

Do you have any idea ? 你有什么主意吗 ?

I tried to control by pid, But getmypid() return same value always... 我试图通过pid进行控制,但是getmypid()总是返回相同的值...

Another solution is (if you don't need to send data to your process, but it reads the data itself (eg. from database)) you can use a process manager with a long running task (basically in an infinite loop). 另一个解决方案是(如果不需要将数据发送到流程,但是它本身读取数据(例如从数据库中读取)),则可以将流程管理器用于长时间运行的任务(基本上在无限循环中)。 (Actually you also need a process manager in a queue-based solution as well, since it requires workers on the receiver side) (实际上,在基于队列的解决方案中,您还需要一个流程管理器,因为它需要接收方的工作人员)

I usually use Supervisord for this purpose. 我通常为此目的使用Supervisord

A simple, long running application: 一个简单,长期运行的应用程序:

while (true) {
    // do something
    sleep(1);
}

The advantage of supervisord is that it can restart your process if it stops for some reason. 主管的优点是,如果由于某种原因而停止,它可以重新启动您的进程。

For such tasks, your best option might be to use some sort of queuing system. 对于此类任务,您最好的选择可能是使用某种排队系统。 I found the best looking Bernard . 我找到了看上去最好的伯纳德 You should take a look. 你应该看看。

( Edit: I made an assumption about your task based on your need to have them not to overlap, and that's why I recommended queues. If it's something new to you, take a look at them, and decide which is better for you, if it doesn't fit best for you, someone searching for single task keywords may still find queues useful, so I leave it in the answer.) 编辑:我根据您的任务不重叠的需要对您的任务进行了假设,这就是为什么我建议使用队列。如果对您来说是新事物,请查看它们,并确定哪种对您更好它最不适合您,搜索单个任务关键字的人可能仍然发现队列有用,因此我将其留在答案中。)

If your environment doesn't allow such a solution for some reason, or you truly simply need a method to tell if the previous process is running, or not, you should just create a lockfile in the temp directory, something like mytask.lock when a task starts, and delete it when it terminates (finishes, or crashes). 如果您的环境由于某种原因而不允许这种解决方案,或者您确实只需要一种方法来告诉您先前的进程是否正在运行,则只需在temp目录中创建一个锁文件,例如mytask.lock 。任务开始,并在任务终止(完成或崩溃)时将其删除。 You can then check if the file is present. 然后,您可以检查文件是否存在。

This is a common solution for this problem, but using queue systems is much more flexible, and much more elegant. 这是解决此问题的常用解决方案,但是使用队列系统更加灵活,而且更加优雅。

Other possibility is to use pidfiles, which are not empty, but store the process ID of the first running task, so you can even terminate that, or send messages to it. 其他可能性是使用非空的pidfile,但存储第一个正在运行的任务的进程ID,因此您甚至可以终止该进程或向其发送消息。 Same with lockfiles, you put it in your temp directory, and name it like mytask.pid (or whatever you want, but *.pid is common). 与锁文件相同,将其放在temp目录中,并将其命名为mytask.pid (或任何您想要的名称,但*.pid很常见)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM