S
S
Splite2014-02-14 16:23:56
Message Queues
Splite, 2014-02-14 16:23:56

How to implement continuous task execution in PHP?

Hello! There is a certain task (infinite loop) which should be carried out continuously.
The option to launch on the crown is not suitable, because. there is no way to predict how long a part of a task will take.
The application itself is on Laravel, so I think to use the queuing mechanism and Artisan. But if something happens in the process of execution, the queue will be interrupted.
How to proceed? I think to dig in the direction of PhpDaemon, but I have no idea how it works.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
Stepan, 2014-02-14
@Splite

$file='/tmp/parser_status.lock';
if(!flock($lock_file = fopen($file, 'w'), LOCK_EX | LOCK_NB))
  die("Already runninng\n");

At the beginning of the file. And cron every minute..
If something goes wrong, the lock will be released and a new script will run.
A crutch of course, but it has been working for several years :)

O
Oleg Prilepa, 2014-02-14
@OAPrilepa

Wouldn't this option work?

<?php
set_time_limit(0);
while (true) { ... }

M
Max, 2014-02-14
@AloneCoder

Yes, then you need a daemon, dig towards pcntl_fork() and examples of its use

I
Ivan Kryak, 2014-02-14
@sck_v

Just at the end of the program execution, in the same program, add it to the queue
Like this:

do_really_hard_work(params)
queue.enqueue(_FILE_)

You only have to run it once. Come on and figure out how to stop

S
Sergey Sokolov, 2014-02-14
@sergiks

I have a php process running in an endless loop grabbing tasks from gearman .
monit makes sure that the process does not fall off, and restarts it if something happens. By default, monit runs its checks once a minute - in which case this solution is hardly better than the simpler method suggested by @L3n1n

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question