peppeocchi / php-cron-scheduler

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

cron seconds #61

Closed gamadoleo closed 6 years ago

gamadoleo commented 6 years ago

is there any way to use this library to run tasks in less time every minute? ie every 15 seconds for example?

peppeocchi commented 6 years ago

Hi, the limitation comes from the crontab and there is no built-in functionality to run in less then a minute. That being said, you can run the scheduler multiple times with a sleep between each execution.

<?php

$scheduler = new Scheduler;
$scheduler->php('myscript.php');

$scheduler->run();

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

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

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

Of course this solution only works if your jobs run in less than a second (you might get fancy and calculate the time it takes on each run and adjust the sleep time).

GhaziTriki commented 2 years ago

@peppeocchi I have tried the following and it works, could be added to the docs or even implemented as run at second:

<?php

  $this->scheduler->php($this->documentRoot, null, ['/cli/task/run' => ''])->everyMinute()->before(
            function (): void {
                sleep(5);
            }
        );;