Skip to content

Commit e57efc9

Browse files
authored
Merge pull request #24 from netlogix/feat/break-poll-loop-even-if-there-are-remaining-jobs
feat: Ensure poll loop breaks periodically even with active jobs
2 parents bd85b55 + 2795125 commit e57efc9

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

Classes/Command/SchedulerCommandController.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
*/
2323
class SchedulerCommandController extends CommandController
2424
{
25+
private const TEN_MINUTES_IN_SECONDS = 600;
26+
27+
protected $stopPollingAfter = self::TEN_MINUTES_IN_SECONDS;
28+
2529
/**
2630
* @var Scheduler
2731
*/
@@ -77,11 +81,10 @@ public function queueDueJobsCommand(string $groupName): void
7781
public function pollForIncomingJobsCommand(string $groupName): void
7882
{
7983
$startTime = time();
80-
$oneHourInSeconds = 3600;
81-
$endTime = $startTime + $oneHourInSeconds;
84+
$endTime = $startTime + $this->stopPollingAfter;
8285

8386
while (true) {
84-
$numberOfHandledJobs = $this->queueDueJobs($groupName);
87+
$numberOfHandledJobs = $this->queueDueJobs($groupName, $endTime);
8588
if (time() >= $endTime) {
8689
return;
8790
}
@@ -92,9 +95,12 @@ public function pollForIncomingJobsCommand(string $groupName): void
9295
}
9396

9497
/**
98+
* @param string $groupName Handle only jobs of this group
99+
* @param int $endTime Stop handling new jobs once this time is reached
95100
* @return int Number of handled jobs
101+
* @throws ConnectionLost
96102
*/
97-
protected function queueDueJobs(string $groupName): int
103+
protected function queueDueJobs(string $groupName, int $endTime): int
98104
{
99105
$numberOfHandledJobs = 0;
100106
$retry = new Retry($this->scheduler);
@@ -116,6 +122,9 @@ protected function queueDueJobs(string $groupName): int
116122
$this->throwableStorage->logThrowable($throwable);
117123
$retry->markJobForRescheduling($next);
118124
}
125+
if (time() >= $endTime) {
126+
return $numberOfHandledJobs;
127+
}
119128
}
120129

121130
$retry->scheduleAll();

0 commit comments

Comments
 (0)