peppeocchi / php-cron-scheduler

PHP cron job scheduler
MIT License
811 stars 144 forks source link

execution in seconds #62

Closed gamadoleo closed 6 years ago

gamadoleo commented 6 years ago

Would it be possible to circumvent this scheduling method where it allows only at least every minute and is able to adjust to run for example every 10 seconds?

peppeocchi commented 6 years ago

@gamadoleo I've already replied to your question in #61

gamadoleo commented 6 years ago

ok

gamadoleo commented 6 years ago

thanks a lot, for the return, get done as recommended, now min arises a doubt imagine that I have several tasks schedules, so realize the task that is at the end of the queue is executed only when all the previous ones have already been executed, is there any way force them all to start at the same instant, without depending on the completion of the previous ones? whereas we are in the same scheduling script.

peppeocchi commented 6 years ago

to avoid this issue you might use a different approach, for example scheduling the scheduler 4 times in the same file. Example: you have your scheduler script with all the jobs that need to be executed (foreground and background jobs, it doesn't matter). Now, because some of yours scheduler executions might take more than 15s to execute, there could be a case of job overlapping. So, instead of running the scheduler 4 times on that file, set it up for a singular execution. Then create a "master scheduler" that runs 4 times and executes in the background your "child" scheduler every 15s.

eg. scheduler.php

#....
$scheduler = new Scheduler();

// These are the scripts that need to be executed every 15s
$scheduler->php('my_script.php');
$scheduler->php('my_other_script.php');
$scheduler->php('some_other_script.php');

$scheduler->run();

Now create the master_scheduler.php (please note that this is the one to be added in the crontab instead of scheduler.php

#....
$scheduler = new Scheduler();

// Run the "child" scheduler
$scheduler->php('scheduler.php');

sleep(15);
$scheduler->resetRun()->run();

sleep(15);
$scheduler->resetRun()->run();

sleep(15);
$scheduler->resetRun()->run();

Because scheduler.php can run in the background, there will be minimal or no delay at all between the execution. So you are sure that all of your jobs will run at the same time every 15s.

Hope that helps

gamadoleo commented 6 years ago

Ok, thank you very much, I analyzed the library here, the thing is that I used the -> then () method, and this makes the script to run in the foreground, and when modifying via parameter to force in the background, return script, but okay, I'll review my need.