We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

run a task in every 30 second, infinitely

Hey, I have to update DataBase every 30 second with information I get from the API, what is the best way to run some code infinitely every 30 second?



77.7k
Accepted
answer
edited Dec '18

First option is using crontab if you're on a unix system. The drawback is that a brand new script will be executed every 30 seconds, and if one takes longer than 30seconds, they can overlap.

The other option is using a periodic timer from event loops . That way you have more control over the repeating task.

Crontab is specifically built for this - so absolutely use it.

I have cron tasks that occasionally take longer than their period. To combat this I create a lock file when the task starts, and delete it when it ends. If the file already exists when the task starts, then a previous iteration is still running, and the task just ends.

Well, back in the day when I had to use shared hosts w/o access to crontab but with PHP CLI access, I'd built a program with infinitive loop which will tick on a given time period and execute something.

But of course - crontab is the way to go unless you have some crazy heavy operation to carry - in this case 30 secs might overlap and kill your DB eventually as others here already pointed out.

thanks guys, I test phalcon CLI too as you mention, for now I will use CronTab, but any better solotion I am looking for