|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AurimasNiekis\SchedulerBundle; |
|
6
|
|
|
|
|
7
|
|
|
use Cron\CronExpression; |
|
8
|
|
|
use DateTime; |
|
9
|
|
|
use DateTimeInterface; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Psr\Log\NullLogger; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Aurimas Niekis <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Scheduler |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ScheduledJobInterface[] */ |
|
20
|
|
|
private array $scheduledJobs; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
private LoggerInterface $logger; |
|
23
|
|
|
|
|
24
|
8 |
|
public function __construct(iterable $scheduledJobs = [], LoggerInterface $logger = null) |
|
25
|
|
|
{ |
|
26
|
8 |
|
$this->scheduledJobs = []; |
|
27
|
8 |
|
$this->logger = $logger ?? new NullLogger(); |
|
28
|
|
|
|
|
29
|
8 |
|
foreach ($scheduledJobs as $scheduledJob) { |
|
30
|
6 |
|
$this->addScheduledJob($scheduledJob); |
|
31
|
|
|
} |
|
32
|
8 |
|
} |
|
33
|
|
|
|
|
34
|
7 |
|
public function addScheduledJob(ScheduledJobInterface $scheduledJob): self |
|
35
|
|
|
{ |
|
36
|
7 |
|
$name = $this->getScheduledJobName($scheduledJob); |
|
37
|
|
|
|
|
38
|
7 |
|
if (false === is_callable($scheduledJob)) { |
|
39
|
1 |
|
throw new InvalidArgumentException('ScheduledJob "' . $name . '" must implement `__invoke` method'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
6 |
|
$this->scheduledJobs[$name] = $scheduledJob; |
|
43
|
|
|
|
|
44
|
6 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
7 |
|
private function getScheduledJobName(ScheduledJobInterface $scheduledJob): string |
|
48
|
|
|
{ |
|
49
|
7 |
|
if ($scheduledJob instanceof NamedScheduledJobInterface) { |
|
50
|
4 |
|
return $scheduledJob->getName(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
return get_class($scheduledJob); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function getScheduledJob(string $name): ScheduledJobInterface |
|
57
|
|
|
{ |
|
58
|
2 |
|
if (false === isset($this->scheduledJobs[$name])) { |
|
59
|
1 |
|
throw new InvalidArgumentException('ScheduledJob with name "' . $name . '" does not exist!'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $this->scheduledJobs[$name]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function execute(DateTimeInterface $at = null): void |
|
66
|
|
|
{ |
|
67
|
1 |
|
$at = $at ?? new DateTime(); |
|
68
|
|
|
|
|
69
|
1 |
|
$this->logger->debug('Executing Scheduler', ['at' => $at]); |
|
70
|
|
|
|
|
71
|
1 |
|
$dueJobs = $this->getDueJobs($at); |
|
72
|
|
|
|
|
73
|
1 |
|
$this->logger->debug( |
|
74
|
1 |
|
'Scheduled Events found to execute', |
|
75
|
1 |
|
['count' => count($dueJobs), 'at' => $at] |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
1 |
|
foreach ($dueJobs as $dueJob) { |
|
79
|
1 |
|
$this->executeJob($dueJob); |
|
80
|
|
|
} |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
public function getDueJobs(DateTimeInterface $at): array |
|
84
|
|
|
{ |
|
85
|
3 |
|
$dueJobs = []; |
|
86
|
|
|
|
|
87
|
3 |
|
foreach ($this->getScheduledJobs() as $name => $scheduledJob) { |
|
88
|
3 |
|
$cronExpression = CronExpression::factory($scheduledJob->getSchedulerExpresion()); |
|
89
|
|
|
|
|
90
|
2 |
|
if ($cronExpression->isDue($at)) { |
|
91
|
2 |
|
$dueJobs[$name] = $scheduledJob; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
return $dueJobs; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return ScheduledJobInterface[] |
|
100
|
|
|
*/ |
|
101
|
4 |
|
public function getScheduledJobs(): array |
|
102
|
|
|
{ |
|
103
|
4 |
|
return $this->scheduledJobs; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param ScheduledJobInterface|callable $scheduledJob |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function executeJob(ScheduledJobInterface $scheduledJob): void |
|
110
|
|
|
{ |
|
111
|
2 |
|
$this->logger->debug('Executing scheduled job', ['scheduledJob' => $scheduledJob]); |
|
112
|
|
|
|
|
113
|
2 |
|
$scheduledJob(); |
|
114
|
|
|
|
|
115
|
2 |
|
$this->logger->debug('Executing scheduled job finished', ['scheduledJob' => $scheduledJob]); |
|
116
|
2 |
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|