File tree Expand file tree Collapse file tree 3 files changed +87
-0
lines changed
Filter options
src/Symfony/Component/Scheduler Expand file tree Collapse file tree 3 files changed +87
-0
lines changed
Original file line number Diff line number Diff line change 14
14
use Symfony \Component \Scheduler \Exception \InvalidArgumentException ;
15
15
use Symfony \Component \Scheduler \Trigger \CronExpressionTrigger ;
16
16
use Symfony \Component \Scheduler \Trigger \DateIntervalTrigger ;
17
+ use Symfony \Component \Scheduler \Trigger \JitterTrigger ;
17
18
use Symfony \Component \Scheduler \Trigger \TriggerInterface ;
18
19
19
20
/**
@@ -59,6 +60,11 @@ public static function trigger(TriggerInterface $trigger, object $message): self
59
60
return new self ($ trigger , $ message );
60
61
}
61
62
63
+ public function withJitter (int $ maxSeconds = 60 ): self
64
+ {
65
+ return new self (new JitterTrigger ($ this ->trigger , $ maxSeconds ), $ this ->message );
66
+ }
67
+
62
68
public function getMessage (): object
63
69
{
64
70
return $ this ->message ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments