S
S
santavits2018-03-21 17:31:50
PHP
santavits, 2018-03-21 17:31:50

How to work with php and cron?

The situation is this
. For example, you have your own service
. How can I make it so that users can add their tasks, tasks should be launched using a php script at a specified time, the time is specified by the user himself, let it be every 10 minutes
In the task, the user specifies his data that should be applied in script
Well, so far I have only thought of the fact that I put the php script to execute every minute, user data will be taken from the database.
Only 2 questions torment me
1) What will happen if several users indicate the same time
For example, 3 users indicate the time the task is performed every 10 minutes
After all, the script will take the data of only one user or even give an error
2) How to substitute user data?
In order for the script to perform the task of exactly that user.
More precisely, how to specify the user id in a database query
Select * From 'table' Where 'id' =???
I will be grateful for any help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Skibin, 2018-03-21
@megafax

Then you should have a task book that, for example, starts every minute, checks the tasks it needs, and, if there are several of them, starts them all.
All this is done via CRON in the case of PHP.
BUT only your script is launched by cron, which already looks at the tasks it needs, and not the scripts of your users

R
Rsa97, 2018-03-21
@Rsa97

SELECT * 
  FROM `tasks` 
  WHERE `nextStartTime` < :currentTime;

Select all lines, run tasks.
Further, either the task is deleted if it is a one-time task, or the time of the next launch is updated
DELETE 
  FROM `tasks` 
  WHERE `nextStartTime` < :currentTime AND 0 = `interval`;
UPDATE `tasks` 
  SET `nextStartTime` = `nextStartTime` + INTERVAL `interval` MINUTE 
  WHERE `nextStartTime` < :currentTime;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question