File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed
Filter options
src/Symfony/Component/Scheduler/Trigger Expand file tree Collapse file tree 1 file changed +21
-1
lines changed
Original file line number Diff line number Diff line change 14
14
use Cron \CronExpression ;
15
15
use Random \Engine \Xoshiro256StarStar ;
16
16
use Random \Randomizer ;
17
+ use Symfony \Component \Scheduler \Exception \InvalidArgumentException ;
17
18
use Symfony \Component \Scheduler \Exception \LogicException ;
18
19
19
20
/**
@@ -46,6 +47,8 @@ final class CronExpressionTrigger implements TriggerInterface
46
47
[0 , 6 ],
47
48
];
48
49
50
+ private int $ jitter = 0 ;
51
+
49
52
public function __construct (
50
53
private readonly CronExpression $ expression = new CronExpression ('* * * * * ' ),
51
54
) {
@@ -73,9 +76,26 @@ public static function fromSpec(string $expression = '* * * * *', string $contex
73
76
return new self (new CronExpression (self ::parseHashed ($ expression , $ context )));
74
77
}
75
78
79
+ public function jitter (int $ value = 60 ): self
80
+ {
81
+ if ($ value < 0 || $ value > 60 ) {
82
+ throw new InvalidArgumentException ('The jitter value must be between 0 and 60. ' );
83
+ }
84
+
85
+ $ this ->jitter = $ value ;
86
+
87
+ return $ this ;
88
+ }
89
+
76
90
public function getNextRunDate (\DateTimeImmutable $ run ): ?\DateTimeImmutable
77
91
{
78
- return \DateTimeImmutable::createFromMutable ($ this ->expression ->getNextRunDate ($ run ));
92
+ $ nextRun = \DateTimeImmutable::createFromMutable ($ this ->expression ->getNextRunDate ($ run ));
93
+
94
+ if ($ this ->jitter > 0 ) {
95
+ $ nextRun = $ nextRun ->add (new \DateInterval (sprintf ("PT%sS " , random_int (0 , $ this ->jitter ))));
96
+ }
97
+
98
+ return $ nextRun ;
79
99
}
80
100
81
101
private static function parseHashed (string $ expression , string $ context ): string
You can’t perform that action at this time.
0 commit comments