-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScheduler.php
113 lines (95 loc) · 3.44 KB
/
Scheduler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Scheduler;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Scheduler\Event\FailureEvent;
use Symfony\Component\Scheduler\Event\PostRunEvent;
use Symfony\Component\Scheduler\Event\PreRunEvent;
use Symfony\Component\Scheduler\Generator\MessageGenerator;
final class Scheduler
{
/**
* @var array<MessageGenerator>
*/
private array $generators = [];
private int $index = 0;
private bool $shouldStop = false;
/**
* @param iterable<Schedule> $schedules
*/
public function __construct(
private readonly array $handlers,
array $schedules,
private readonly ClockInterface $clock = new Clock(),
private readonly ?EventDispatcherInterface $dispatcher = null,
) {
foreach ($schedules as $schedule) {
$this->addSchedule($schedule);
}
}
public function addSchedule(Schedule $schedule): void
{
$this->addMessageGenerator(new MessageGenerator($schedule, 'schedule_'.$this->index++, $this->clock));
}
public function addMessageGenerator(MessageGenerator $generator): void
{
$this->generators[] = $generator;
}
/**
* Schedules messages.
*
* Valid options are:
* * sleep (default: 1000000): Time in microseconds to sleep after no messages are found
*/
public function run(array $options = []): void
{
$options += ['sleep' => 1e6];
while (!$this->shouldStop) {
$start = $this->clock->now();
$ran = false;
foreach ($this->generators as $generator) {
foreach ($generator->getMessages() as $context => $message) {
if (!$this->dispatcher) {
$this->handlers[$message::class]($message);
$ran = true;
continue;
}
$preRunEvent = new PreRunEvent($generator->getSchedule(), $context, $message);
$this->dispatcher->dispatch($preRunEvent);
if ($preRunEvent->shouldCancel()) {
continue;
}
try {
$this->handlers[$message::class]($message);
$ran = true;
$this->dispatcher->dispatch(new PostRunEvent($generator->getSchedule(), $context, $message));
} catch (\Throwable $error) {
$failureEvent = new FailureEvent($generator->getSchedule(), $context, $message, $error);
$this->dispatcher->dispatch($failureEvent);
if (!$failureEvent->shouldIgnore()) {
throw $error;
}
}
}
}
if (!$ran) {
if (0 < $sleep = (int) ($options['sleep'] - 1e6 * ($this->clock->now()->format('U.u') - $start->format('U.u')))) {
$this->clock->sleep($sleep / 1e6);
}
}
}
}
public function stop(): void
{
$this->shouldStop = true;
}
}