1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule; |
4
|
|
|
|
5
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
6
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipSchedule; |
7
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\Result; |
8
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunContext; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Kevin Bond <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class ScheduleRunContext extends RunContext |
14
|
|
|
{ |
15
|
|
|
/** @var Schedule */ |
16
|
|
|
private $schedule; |
17
|
|
|
|
18
|
|
|
/** @var Task[] */ |
19
|
|
|
private $dueTasks; |
20
|
|
|
|
21
|
|
|
/** @var bool */ |
22
|
|
|
private $force; |
23
|
|
|
|
24
|
|
|
/** @var TaskRunContext[] */ |
25
|
|
|
private $taskRunContexts; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
88 |
|
private $skipReason; |
29
|
|
|
|
30
|
88 |
|
/** @var Result[]|null */ |
31
|
|
|
private $results; |
32
|
88 |
|
|
33
|
88 |
|
/** @var Result[]|null */ |
34
|
88 |
|
private $successful; |
35
|
88 |
|
|
36
|
|
|
/** @var Result[]|null */ |
37
|
1 |
|
private $failures; |
38
|
|
|
|
39
|
1 |
|
/** @var Result[]|null */ |
40
|
|
|
private $skipped; |
41
|
|
|
|
42
|
82 |
|
/** @var Result[]|null */ |
43
|
|
|
private $run; |
44
|
82 |
|
|
45
|
|
|
public function __construct(Schedule $schedule, Task ...$forcedTasks) |
46
|
|
|
{ |
47
|
|
|
parent::__construct(); |
48
|
|
|
|
49
|
|
|
$this->schedule = $schedule; |
50
|
75 |
|
$this->dueTasks = empty($forcedTasks) ? $schedule->due($this->getStartTime()) : $forcedTasks; |
51
|
|
|
$this->force = !empty($forcedTasks); |
52
|
75 |
|
} |
53
|
|
|
|
54
|
|
|
public function __toString(): string |
55
|
25 |
|
{ |
56
|
|
|
return 'The Schedule'; |
57
|
25 |
|
} |
58
|
|
|
|
59
|
|
|
public function getSchedule(): Schedule |
60
|
73 |
|
{ |
61
|
|
|
return $this->schedule; |
62
|
73 |
|
} |
63
|
73 |
|
|
64
|
|
|
/** |
65
|
73 |
|
* @return Task[] |
66
|
1 |
|
*/ |
67
|
|
|
public function dueTasks(): array |
68
|
|
|
{ |
69
|
72 |
|
return $this->dueTasks; |
70
|
|
|
} |
71
|
72 |
|
|
72
|
72 |
|
public function isForceRun(): bool |
73
|
|
|
{ |
74
|
7 |
|
return $this->force; |
75
|
|
|
} |
76
|
7 |
|
|
77
|
7 |
|
public function setTaskRunContexts(TaskRunContext ...$contexts): void |
78
|
|
|
{ |
79
|
7 |
|
$contextCount = \count($contexts); |
80
|
|
|
$dueCount = \count($this->dueTasks()); |
81
|
7 |
|
|
82
|
|
|
if ($contextCount !== $dueCount) { |
83
|
|
|
throw new \LogicException("The number of results ({$contextCount}) does not match the number of due tasks ({$dueCount})."); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->markAsRun(\memory_get_peak_usage(true)); |
87
|
|
|
|
88
|
|
|
$this->taskRunContexts = $contexts; |
89
|
73 |
|
} |
90
|
|
|
|
91
|
73 |
|
public function skip(SkipSchedule $exception): void |
92
|
|
|
{ |
93
|
72 |
|
$this->skipReason = $exception->getMessage(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getSkipReason(): ?string |
97
|
|
|
{ |
98
|
|
|
return $this->skipReason; |
99
|
|
|
} |
100
|
|
|
|
101
|
73 |
|
/** |
102
|
|
|
* @return TaskRunContext[] |
103
|
73 |
|
* |
104
|
42 |
|
* @throws \LogicException if has not yet run |
105
|
|
|
*/ |
106
|
|
|
public function getTaskRunContexts(): array |
107
|
73 |
|
{ |
108
|
|
|
$this->ensureHasRun(); |
109
|
73 |
|
|
110
|
63 |
|
return $this->taskRunContexts; |
111
|
|
|
} |
112
|
|
|
|
113
|
72 |
|
/** |
114
|
|
|
* @return Result[] |
115
|
|
|
* |
116
|
|
|
* @throws \LogicException if has not yet run |
117
|
|
|
*/ |
118
|
|
|
public function getResults(): array |
119
|
75 |
|
{ |
120
|
|
|
if (null !== $this->results) { |
121
|
75 |
|
return $this->results; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->results = []; |
125
|
|
|
|
126
|
|
|
foreach ($this->getTaskRunContexts() as $context) { |
127
|
72 |
|
$this->results[] = $context->getResult(); |
128
|
|
|
} |
129
|
72 |
|
|
130
|
|
|
return $this->results; |
131
|
|
|
} |
132
|
79 |
|
|
133
|
|
|
/** |
134
|
79 |
|
* @throws \LogicException if has not yet run and has not been marked as skipped |
135
|
|
|
*/ |
136
|
|
|
public function isSuccessful(): bool |
137
|
|
|
{ |
138
|
|
|
return $this->isSkipped() || 0 === \count($this->getFailures()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
21 |
|
* @throws \LogicException if has not yet run |
143
|
|
|
*/ |
144
|
21 |
|
public function isFailure(): bool |
145
|
|
|
{ |
146
|
|
|
return !$this->isSuccessful(); |
147
|
|
|
} |
148
|
21 |
|
|
149
|
|
|
public function isSkipped(): bool |
150
|
21 |
|
{ |
151
|
19 |
|
return null !== $this->skipReason; |
152
|
16 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return Result[] |
156
|
21 |
|
* |
157
|
|
|
* @throws \LogicException if has not yet run |
158
|
|
|
*/ |
159
|
|
|
public function getSuccessful(): array |
160
|
|
|
{ |
161
|
|
|
if (null !== $this->successful) { |
162
|
|
|
return $this->successful; |
163
|
|
|
} |
164
|
72 |
|
|
165
|
|
|
$this->successful = []; |
166
|
72 |
|
|
167
|
72 |
|
foreach ($this->getResults() as $result) { |
168
|
|
|
if ($result->isSuccessful()) { |
169
|
|
|
$this->successful[] = $result; |
170
|
72 |
|
} |
171
|
|
|
} |
172
|
72 |
|
|
173
|
63 |
|
return $this->successful; |
174
|
28 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return Result[] |
178
|
72 |
|
* |
179
|
|
|
* @throws \LogicException if has not yet run |
180
|
|
|
*/ |
181
|
|
|
public function getFailures(): array |
182
|
|
|
{ |
183
|
|
|
if (null !== $this->failures) { |
184
|
|
|
return $this->failures; |
185
|
|
|
} |
186
|
35 |
|
|
187
|
|
|
$this->failures = []; |
188
|
35 |
|
|
189
|
|
|
foreach ($this->getResults() as $result) { |
190
|
|
|
if ($result->isFailure()) { |
191
|
|
|
$this->failures[] = $result; |
192
|
35 |
|
} |
193
|
|
|
} |
194
|
35 |
|
|
195
|
33 |
|
return $this->failures; |
196
|
12 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return Result[] |
200
|
35 |
|
* |
201
|
|
|
* @throws \LogicException if has not yet run |
202
|
|
|
*/ |
203
|
|
|
public function getSkipped(): array |
204
|
|
|
{ |
205
|
|
|
if (null !== $this->skipped) { |
206
|
|
|
return $this->skipped; |
207
|
|
|
} |
208
|
35 |
|
|
209
|
|
|
$this->skipped = []; |
210
|
35 |
|
|
211
|
|
|
foreach ($this->getResults() as $result) { |
212
|
|
|
if ($result->isSkipped()) { |
213
|
|
|
$this->skipped[] = $result; |
214
|
35 |
|
} |
215
|
|
|
} |
216
|
35 |
|
|
217
|
33 |
|
return $this->skipped; |
218
|
22 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return Result[] |
222
|
35 |
|
* |
223
|
|
|
* @throws \LogicException if has not yet run |
224
|
|
|
*/ |
225
|
|
|
public function getRun(): array |
226
|
|
|
{ |
227
|
|
|
if (null !== $this->run) { |
228
|
|
|
return $this->run; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$this->run = []; |
232
|
|
|
|
233
|
|
|
foreach ($this->getResults() as $result) { |
234
|
|
|
if ($result->hasRun()) { |
235
|
|
|
$this->run[] = $result; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->run; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|