Share coroutines for executions
This commit is contained in:
parent
c2e8fc5733
commit
de8122c02f
1 changed files with 21 additions and 7 deletions
|
@ -147,6 +147,8 @@ $cli
|
|||
|
||||
$total = 0;
|
||||
|
||||
$delayedExecutions = []; // Group executions with same delay to share one coroutine
|
||||
|
||||
foreach ($schedules as $key => $schedule) {
|
||||
$cron = new CronExpression($schedule['schedule']);
|
||||
$nextDate = $cron->getNextRunDate();
|
||||
|
@ -164,15 +166,27 @@ $cli
|
|||
$executionStart = $nextDate->getTimestamp(); // in seconds
|
||||
$executionSleep = $executionStart - $promiseStart; // Time to wait from now until execution needs to be queued
|
||||
|
||||
\go(function() use ($executionSleep, $key, $schedules) {
|
||||
\usleep($executionSleep * 1000000); // in microseconds
|
||||
$delay = \intval($executionSleep);
|
||||
|
||||
if(!isset($delayedExecutions[$delay])) {
|
||||
$delayedExecutions[$delay] = [];
|
||||
}
|
||||
|
||||
$delayedExecutions[$delay][] = $key;
|
||||
}
|
||||
|
||||
foreach($delayedExecutions as $delay => $scheduleKeys) {
|
||||
\go(function() use ($delay, $schedules, $scheduleKeys) {
|
||||
\sleep($delay); // in seconds
|
||||
|
||||
foreach($scheduleKeys as $scheduleKey) {
|
||||
// Ensure schedule was not deleted
|
||||
if(!isset($schedules[$key])) {
|
||||
if(!isset($schedules[$scheduleKey])) {
|
||||
return;
|
||||
}
|
||||
|
||||
Console::success("Executing function at " . DateTime::now()); // TODO: Send to worker queue
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue