Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3b29817

Browse filesBrowse files
committed
feature #50270 [Scheduler] add JitterTrigger (kbond)
This PR was merged into the 6.3 branch. Discussion ---------- [Scheduler] add `JitterTrigger` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #49806 | License | MIT | Doc PR | n/a I think this is what `@vtsykun` was trying to accomplish with #49806. FreeBSD has a `-jitter` option that does the same thing so I think there's precedent. From the (https://man.freebsd.org/cgi/man.cgi?cron): > -j jitter > Enable time jitter. Prior to executing commands, cron will sleep a random number of seconds in the range from 0 to jitter. ~This will not affect superuser jobs (see -J).~ A value for jitter must be between 0 and 60 inclusive. Default is 0, which effectively disables time jitter. > This option can help to smooth down system load spikes during mo- ments when a lot of jobs are likely to start at once, e.g., at the beginning of the first minute of each hour. Usage: ```php // defaults to 0-60 seconds $schedule->add(RecurringMessage::cron('`@daily`', $message1)->withJitter()); // customize the max seconds (0-30) $schedule->add(RecurringMessage::cron('`@daily`', $message1)->withJitter(30)); ``` Commits ------- b0d984b [Scheduler] add `JitterTrigger`
2 parents a0b1ddb + b0d984b commit 3b29817
Copy full SHA for 3b29817

File tree

Expand file treeCollapse file tree

3 files changed

+87
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+87
-0
lines changed

‎src/Symfony/Component/Scheduler/RecurringMessage.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Scheduler/RecurringMessage.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Scheduler\Exception\InvalidArgumentException;
1515
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;
1616
use Symfony\Component\Scheduler\Trigger\DateIntervalTrigger;
17+
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
1718
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
1819

1920
/**
@@ -59,6 +60,11 @@ public static function trigger(TriggerInterface $trigger, object $message): self
5960
return new self($trigger, $message);
6061
}
6162

63+
public function withJitter(int $maxSeconds = 60): self
64+
{
65+
return new self(new JitterTrigger($this->trigger, $maxSeconds), $this->message);
66+
}
67+
6268
public function getMessage(): object
6369
{
6470
return $this->message;
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Tests\Trigger;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
16+
use Symfony\Component\Scheduler\Trigger\TriggerInterface;
17+
18+
class JitterTriggerTest extends TestCase
19+
{
20+
public function testCanAddJitter()
21+
{
22+
$time = new \DateTimeImmutable();
23+
$inner = $this->createMock(TriggerInterface::class);
24+
$inner->method('getNextRunDate')->willReturn($time);
25+
26+
$trigger = new JitterTrigger($inner);
27+
28+
$values = array_map(
29+
fn () => (int) $trigger->getNextRunDate($time)?->getTimestamp(),
30+
array_fill(0, 100, null)
31+
);
32+
33+
foreach ($values as $value) {
34+
$this->assertGreaterThanOrEqual($time->getTimestamp(), $value);
35+
$this->assertLessThanOrEqual($time->getTimestamp() + 60, $value);
36+
}
37+
38+
$values = array_unique($values);
39+
40+
$this->assertGreaterThan(1, \count($values));
41+
}
42+
}
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Trigger;
13+
14+
/**
15+
* @author Kevin Bond <kevinbond@gmail.com>
16+
*/
17+
final class JitterTrigger implements TriggerInterface
18+
{
19+
/**
20+
* @param positive-int $maxSeconds
21+
*/
22+
public function __construct(private readonly TriggerInterface $trigger, private readonly int $maxSeconds = 60)
23+
{
24+
}
25+
26+
public function __toString(): string
27+
{
28+
return sprintf('%s with 0-%d second jitter', $this->trigger, $this->maxSeconds);
29+
}
30+
31+
public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
32+
{
33+
if (!$nextRun = $this->trigger->getNextRunDate($run)) {
34+
return null;
35+
}
36+
37+
return $nextRun->add(new \DateInterval(sprintf('PT%sS', random_int(0, $this->maxSeconds))));
38+
}
39+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.