There is such a task: the user selects the task and the time to complete it (every 5 minutes, every day, etc.). This is all I write in the database.

There is a cron.php file that runs every 5 minutes and checks if there are any active tasks in the database.

How to write in the database correctly, so that php would understand whether to complete the task or not. Suppose the user chose every 5 minutes - what to write in the database?

Tell me who worked with it. I will be grateful :-)

  • I would probably keep the start interval in minutes. The launching script somewhere in it (in the same table or in a separate one) stores the next launch time for each task, i.e. adds to the current time the stored number of minutes and writes to the table. And it starts all tasks for which the time of the next launch is less than the current one. - Mike
  • 2
    Two fields, last and interval (in minutes). And run if TIMESTAMPDIFF (MINUTE, last, NOW ())> interval - rjhdby

1 answer 1

Store in the database the interval in seconds and the date of the last run in the unix timestamp. In the database, add to the query: where (interval + laststart) <now () Thus we get all the tasks that need to be done, and after doing write the current time () in the laststart field.

  • I also wanted to do it, but I thought it was not quite right and there are other solutions. But from your answer I realized that this is correct. thanks - user3608884
  • Perhaps there are other solutions, but this seems to me the most convenient in terms of use in the code and the load. - Victor Zharikov
  • one
    @ user3608884 But in general, for reasons of speed, it would be better to keep the calculated start time, to check newstart <now (), this is better optimized, it can work by index. I certainly understand that you will have a dozen records there and optimization is pointless. but still on Feng Shui so :) - Mike
  • @Mike it seems to me that it is more correct to start practicing from 10 thousand entries, but then the question will be raised whether it is worthwhile to start so many tasks after 1 cron.php file. And at the initial stage - this option will be more convenient because the user "On the fly" will be able to change the launch interval. - Victor Zharikov
  • @Victor Zharikov But with my approach, the user will be able to specify on the fly when he wants the next launch, which is very convenient when debugging, so as not to wait an hour. And when changing the interval, no one bothers to count the newstart - Mike