Skip to content

[Scheduler] add JitterTrigger #50270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Scheduler/RecurringMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Scheduler\Exception\InvalidArgumentException;
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;
use Symfony\Component\Scheduler\Trigger\DateIntervalTrigger;
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

/**
Expand Down Expand Up @@ -59,6 +60,11 @@ public static function trigger(TriggerInterface $trigger, object $message): self
return new self($trigger, $message);
}

public function withJitter(int $maxSeconds = 60): self
{
return new self(new JitterTrigger($this->trigger, $maxSeconds), $this->message);
}

public function getMessage(): object
{
return $this->message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Scheduler\Tests\Trigger;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class JitterTriggerTest extends TestCase
{
public function testCanAddJitter()
{
$time = new \DateTimeImmutable();
$inner = $this->createMock(TriggerInterface::class);
$inner->method('getNextRunDate')->willReturn($time);

$trigger = new JitterTrigger($inner);

$values = array_map(
fn () => (int) $trigger->getNextRunDate($time)?->getTimestamp(),
array_fill(0, 100, null)
);

foreach ($values as $value) {
$this->assertGreaterThanOrEqual($time->getTimestamp(), $value);
$this->assertLessThanOrEqual($time->getTimestamp() + 60, $value);
}

$values = array_unique($values);

$this->assertGreaterThan(1, \count($values));
}
}
39 changes: 39 additions & 0 deletions src/Symfony/Component/Scheduler/Trigger/JitterTrigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Scheduler\Trigger;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class JitterTrigger implements TriggerInterface
{
/**
* @param positive-int $maxSeconds
*/
public function __construct(private readonly TriggerInterface $trigger, private readonly int $maxSeconds = 60)
{
}

public function __toString(): string
{
return sprintf('%s with 0-%d second jitter', $this->trigger, $this->maxSeconds);
}

public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
{
if (!$nextRun = $this->trigger->getNextRunDate($run)) {
return null;
}

return $nextRun->add(new \DateInterval(sprintf('PT%sS', random_int(0, $this->maxSeconds))));
}
}