-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWorker.php
381 lines (313 loc) · 13.3 KB
/
Worker.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
declare(strict_types=1);
namespace SchedulerBundle\Worker;
use DateTimeImmutable;
use Exception;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SchedulerBundle\Event\TaskExecutedEvent;
use SchedulerBundle\Event\TaskExecutingEvent;
use SchedulerBundle\Event\TaskFailedEvent;
use SchedulerBundle\Event\WorkerForkedEvent;
use SchedulerBundle\Event\WorkerPausedEvent;
use SchedulerBundle\Event\WorkerRestartedEvent;
use SchedulerBundle\Event\WorkerRunningEvent;
use SchedulerBundle\Event\WorkerSleepingEvent;
use SchedulerBundle\Event\WorkerStartedEvent;
use SchedulerBundle\Event\WorkerStoppedEvent;
use SchedulerBundle\Exception\LogicException;
use SchedulerBundle\Exception\UndefinedRunnerException;
use SchedulerBundle\Middleware\TaskLockBagMiddleware;
use SchedulerBundle\Middleware\WorkerMiddlewareStack;
use SchedulerBundle\Runner\RunnerRegistryInterface;
use SchedulerBundle\SchedulerInterface;
use SchedulerBundle\Task\FailedTask;
use SchedulerBundle\Task\Output;
use SchedulerBundle\Task\TaskExecutionTrackerInterface;
use SchedulerBundle\Task\TaskInterface;
use SchedulerBundle\Task\TaskList;
use SchedulerBundle\Task\TaskListInterface;
use SchedulerBundle\TaskBag\AccessLockBag;
use SchedulerBundle\Worker\ExecutionPolicy\ExecutionPolicyRegistryInterface;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\LockFactory;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Throwable;
use function in_array;
/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class Worker implements WorkerInterface
{
private TaskListInterface $failedTasks;
private LoggerInterface $logger;
private WorkerConfiguration $configuration;
public function __construct(
private SchedulerInterface $scheduler,
private RunnerRegistryInterface $runnerRegistry,
private ExecutionPolicyRegistryInterface $executionPolicyRegistry,
private TaskExecutionTrackerInterface $taskExecutionTracker,
private WorkerMiddlewareStack $middlewareStack,
private EventDispatcherInterface $eventDispatcher,
private LockFactory $lockFactory,
?LoggerInterface $logger = null
) {
$this->configuration = WorkerConfiguration::create();
$this->logger = $logger ?? new NullLogger();
$this->failedTasks = new TaskList();
}
/**
* {@inheritdoc}
*/
public function execute(WorkerConfiguration $configuration, TaskInterface ...$tasks): void
{
if ($this->configuration->shouldStop()) {
return;
}
if (0 === $this->runnerRegistry->count()) {
throw new UndefinedRunnerException(message: 'No runner found');
}
$this->configuration = $configuration;
$this->configuration->setExecutedTasksCount(executedTasksCount: 0);
$this->eventDispatcher->dispatch(event: new WorkerStartedEvent(worker: $this));
try {
$executionPolicy = $this->executionPolicyRegistry->find(policy: $configuration->getExecutionPolicy());
} catch (Throwable $throwable) {
$this->logger->critical(sprintf('The tasks cannot be executed, an error occurred while retrieving the execution policy: %s', $throwable->getMessage()));
return;
}
while (!$this->configuration->shouldStop()) {
$toExecuteTasks = $this->getTasks(tasks: $tasks);
if (0 === $toExecuteTasks->count() && !$this->configuration->isSleepingUntilNextMinute()) {
$this->stop();
}
$executionPolicy->execute(toExecuteTasks: $toExecuteTasks, handleTaskFunc: function (TaskInterface $task, TaskListInterface $taskList): void {
$this->handleTask(task: $task, taskList: $taskList);
});
if ($this->shouldStop(taskList: $toExecuteTasks)) {
break;
}
if ($this->configuration->isSleepingUntilNextMinute()) {
$this->sleep();
$this->stop();
$this->execute($this->configuration, ...$tasks);
}
}
$this->eventDispatcher->dispatch(event: new WorkerStoppedEvent(worker: $this));
}
/**
* {@inheritdoc}
*/
public function preempt(TaskListInterface $preemptTaskList, TaskListInterface $toPreemptTasksList): void
{
$nonExecutedTasks = $toPreemptTasksList->slice(...$preemptTaskList->map(func: static fn (TaskInterface $task): string => $task->getName(), keepKeys: false));
$nonExecutedTasks->walk(func: function (TaskInterface $task): void {
$accessLockBag = $task->getAccessLockBag();
$lock = $this->lockFactory->createLockFromKey(
key: $accessLockBag instanceof AccessLockBag && $accessLockBag->getKey() instanceof Key
? $accessLockBag->getKey()
: TaskLockBagMiddleware::createKey($task)
);
$lock->release();
});
$forkWorker = $this->fork();
$forkWorker->execute($forkWorker->getConfiguration(), ...$nonExecutedTasks->toArray(keepKeys: false));
$forkWorker->stop();
}
/**
* {@inheritdoc}
*/
public function fork(): WorkerInterface
{
$fork = clone $this;
$fork->configuration = WorkerConfiguration::create();
$fork->configuration->fork();
$fork->configuration->setForkedFrom(forkedFrom: $this);
$this->eventDispatcher->dispatch(event: new WorkerForkedEvent(forkedWorker: $this, newWorker: $fork));
return $fork;
}
/**
* {@inheritdoc}
*/
public function pause(): WorkerInterface
{
$this->configuration->run(isRunning: false);
$this->eventDispatcher->dispatch(event: new WorkerPausedEvent(worker: $this));
return $this;
}
/**
* {@inheritdoc}
*/
public function restart(): void
{
$this->configuration->stop();
$this->configuration->run(isRunning: false);
$this->failedTasks = new TaskList();
$this->eventDispatcher->dispatch(event: new WorkerRestartedEvent(worker: $this));
}
/**
* {@inheritdoc}
*/
public function sleep(): void
{
$sleepDuration = $this->getSleepDuration();
$this->eventDispatcher->dispatch(event: new WorkerSleepingEvent(sleepDuration: $sleepDuration, worker: $this));
sleep(seconds: $sleepDuration);
}
/**
* {@inheritdoc}
*/
public function stop(): void
{
$this->configuration->mustSleepUntilNextMinute(sleepUntilNextMinute: false);
$this->configuration->mustStrictlyCheckDate(mustStrictlyCheckDate: false);
$this->configuration->stop();
}
/**
* {@inheritdoc}
*/
public function isRunning(): bool
{
return $this->configuration->isRunning();
}
/**
* {@inheritdoc}
*/
public function getFailedTasks(): TaskListInterface
{
return $this->failedTasks;
}
/**
* {@inheritdoc}
*/
public function getLastExecutedTask(): ?TaskInterface
{
return $this->configuration->getLastExecutedTask();
}
/**
* {@inheritdoc}
*/
public function getRunners(): RunnerRegistryInterface
{
return $this->runnerRegistry;
}
/**
* {@inheritdoc}
*/
public function getExecutionPolicyRegistry(): ExecutionPolicyRegistryInterface
{
return $this->executionPolicyRegistry;
}
/**
* {@inheritdoc}
*/
public function getConfiguration(): WorkerConfiguration
{
return $this->configuration;
}
/**
* @param array<int|string, TaskInterface> $tasks
*
* @throws Throwable {@see SchedulerInterface::getDueTasks()}
*/
protected function getTasks(array $tasks): TaskListInterface
{
$tasks = [] !== $tasks ? new TaskList(tasks: $tasks) : $this->scheduler->getDueTasks(
lazy: $this->configuration->shouldRetrieveTasksLazily(),
strict: $this->configuration->isStrictlyCheckingDate()
);
$lockedTasks = $tasks->filter(filter: function (TaskInterface $task): bool {
$key = TaskLockBagMiddleware::createKey(task: $task);
$task->setAccessLockBag(bag: new AccessLockBag(key: $key));
$lock = $this->lockFactory->createLockFromKey(key: $key, ttl: null, autoRelease: false);
if (!$lock->acquire()) {
$this->logger->info(sprintf('The lock related to the task "%s" cannot be acquired', $task->getName()));
return false;
}
return true;
});
return $lockedTasks->filter(filter: fn (TaskInterface $task): bool => $this->checkTaskState(task: $task));
}
protected function handleTask(TaskInterface $task, TaskListInterface $taskList): void
{
if ($this->configuration->shouldStop()) {
return;
}
$this->eventDispatcher->dispatch(event: new WorkerRunningEvent(worker: $this));
try {
$runner = $this->runnerRegistry->find(task: $task);
if (!$this->configuration->isRunning()) {
$this->configuration->run(isRunning: true);
$this->middlewareStack->runPreExecutionMiddleware(task: $task);
$this->configuration->setCurrentlyExecutedTask(task: $task);
$this->eventDispatcher->dispatch(event: new WorkerRunningEvent(worker: $this));
$this->eventDispatcher->dispatch(event: new TaskExecutingEvent(task: $task, worker: $this, currentTasks: $taskList));
$task->setArrivalTime(dateTimeImmutable: new DateTimeImmutable());
$task->setExecutionStartTime(dateTimeImmutable: new DateTimeImmutable());
$this->taskExecutionTracker->startTracking(task: $task);
$output = $runner->run(task: $task, worker: $this);
$this->taskExecutionTracker->endTracking(task: $task);
$task->setExecutionEndTime(dateTimeImmutable: new DateTimeImmutable());
$task->setLastExecution(dateTimeImmutable: new DateTimeImmutable());
$this->defineTaskExecutionState(task: $task, output: $output);
$this->middlewareStack->runPostExecutionMiddleware(task: $task, worker: $this);
$this->eventDispatcher->dispatch(new TaskExecutedEvent(task: $task, output: $output));
$this->configuration->setLastExecutedTask(lastExecutedTask: $task);
$executedTasksCount = $this->configuration->getExecutedTasksCount();
$this->configuration->setExecutedTasksCount(executedTasksCount: ++$executedTasksCount);
}
} catch (Throwable $throwable) {
$failedTask = new FailedTask(task: $task, reason: $throwable->getMessage());
$this->getFailedTasks()->add(task: $failedTask);
$this->eventDispatcher->dispatch(event: new TaskFailedEvent(task: $failedTask));
} finally {
$this->configuration->setCurrentlyExecutedTask(task: null);
$this->configuration->run(isRunning: false);
$this->eventDispatcher->dispatch(event: new WorkerRunningEvent(worker: $this, isIdle: true));
}
}
protected function shouldStop(TaskListInterface $taskList): bool
{
if ($this->configuration->shouldStop()) {
return true;
}
if ($this->configuration->isSleepingUntilNextMinute()) {
return false;
}
if (0 === $this->configuration->getExecutedTasksCount()) {
return true;
}
return $this->configuration->getExecutedTasksCount() === $taskList->count();
}
private function checkTaskState(TaskInterface $task): bool
{
if (TaskInterface::UNDEFINED === $task->getState()) {
throw new LogicException(message: 'The task state must be defined in order to be executed!');
}
if (in_array(needle: $task->getState(), haystack: [TaskInterface::PAUSED, TaskInterface::DISABLED], strict: true)) {
$this->logger->info(sprintf('The following task "%s" is paused|disabled, consider enable it if it should be executed!', $task->getName()), [
'name' => $task->getName(),
'expression' => $task->getExpression(),
'state' => $task->getState(),
]);
return false;
}
return true;
}
/**
* @throws Exception {@see DateTimeImmutable::__construct()}
*/
private function getSleepDuration(): int
{
$nextMinute = new DateTimeImmutable(datetime: '+ 1 minute', timezone: $this->scheduler->getTimezone());
$updatedNextExecutionDate = $nextMinute->setTime(hour: (int) $nextMinute->format('H'), minute: (int) $nextMinute->format('i'));
return (new DateTimeImmutable(datetime: 'now', timezone: $this->scheduler->getTimezone()))->diff(targetObject: $updatedNextExecutionDate)->s + $this->configuration->getSleepDurationDelay();
}
private function defineTaskExecutionState(TaskInterface $task, Output $output): void
{
if (in_array(needle: $task->getExecutionState(), haystack: [TaskInterface::INCOMPLETE, TaskInterface::TO_RETRY], strict: true)) {
return;
}
$task->setExecutionState(executionState: Output::ERROR === $output->getType() ? TaskInterface::ERRORED : TaskInterface::SUCCEED);
}
}