Reputation: 137
I have some background tasks in my php-project. It shoud do some job if some condition satisfied. For example: if there are some orders than not yet delivered and time left to estimate delivery is less than 15 minutes, system sends notification to courier that he's probably late.
The simpliest solution - create cron task that runs php script every minute. That script will check that condition and send notification if condition is fulfilled.
Another approach is queues. I looked at gearman and rabbitmq, but as i can see they a for another usecase. They fit if you have some client that directly sends tasks. In my case i don't have any client, its just some condition in system.
And the last solution i figured is write custom php-daemon with infinite loop. In each iteration it check condition, do the job if its satisfied and sleeps for 1 minute. But there are possible problems with memory leaks, daemon restarting, etc.
So, what the best solution to this promlem in modern php?
Upvotes: 1
Views: 1597
Reputation: 1
If cron can do the job for you, then by all means use it. Why reinvent the wheel when there is a proven tool that does the job? I wrote some
while (1) {
// run forever scripts
}
and they ran months without errors; they processed message queues which were simple mysql tables. But if you do that, you will need a cron job that checks for the while (1)
process state, you will need some locking to prevent the process being started multiple times, etc. Just go with cron.
Upvotes: 0
Reputation: 3350
If you do not like to use cronjob I guess your best option is to write a daemon. I like coding in Perl and if I were you I would write a daemon in Perl.
However, If I were to write the daemon in PHP, I would run another backup script in cronjob (10 min interval for example) that will check if my php daemon is running or not and restart the daemon if not running.
Upvotes: 0
Reputation: 1
I would make a small nodejs app for this or laravel queues:
https://laravel.com/docs/5.3/queues
It's up to you.
Upvotes: -3