|
1
|
|
|
<?php namespace Comodojo\Extender\Schedule; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
|
4
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
|
5
|
|
|
use \Comodojo\Foundation\Logging\LoggerTrait; |
|
6
|
|
|
use \Comodojo\Foundation\Events\EventsTrait; |
|
7
|
|
|
use \Comodojo\Extender\Components\Database; |
|
8
|
|
|
use \Comodojo\Foundation\Base\ConfigurationTrait; |
|
9
|
|
|
use \Comodojo\Extender\Traits\EntityManagerTrait; |
|
10
|
|
|
use \Comodojo\Extender\Orm\Entities\Schedule; |
|
11
|
|
|
use \Comodojo\Extender\Events\ScheduleEvent; |
|
12
|
|
|
use \Doctrine\ORM\EntityManager; |
|
13
|
|
|
use \Psr\Log\LoggerInterface; |
|
14
|
|
|
use \DateTime; |
|
15
|
|
|
use \InvalidArgumentException; |
|
16
|
|
|
use \Exception; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @package Comodojo Extender |
|
20
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
21
|
|
|
* @license MIT |
|
22
|
|
|
* |
|
23
|
|
|
* LICENSE: |
|
24
|
|
|
* |
|
25
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
26
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
27
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
28
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
29
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
30
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
31
|
|
|
* THE SOFTWARE. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
class Manager { |
|
35
|
|
|
|
|
36
|
|
|
use ConfigurationTrait; |
|
37
|
|
|
use LoggerTrait; |
|
38
|
|
|
use EventsTrait; |
|
39
|
|
|
use EntityManagerTrait; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* If true, $em will be destroyed at shutdown |
|
43
|
|
|
* |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private $destroy_em = true; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Class constructor |
|
50
|
|
|
* |
|
51
|
|
|
* @param Configuration $configuration |
|
52
|
|
|
* @param LoggerInterface $logger |
|
53
|
|
|
* @param TasksTable $tasks |
|
|
|
|
|
|
54
|
|
|
* @param EventsManager $events |
|
55
|
|
|
* @param EntityManager $em |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct( |
|
58
|
|
|
Configuration $configuration, |
|
59
|
|
|
LoggerInterface $logger, |
|
60
|
|
|
EventsManager $events, |
|
61
|
|
|
EntityManager $em = null |
|
62
|
|
|
) { |
|
63
|
|
|
|
|
64
|
|
|
$this->setConfiguration($configuration); |
|
65
|
|
|
$this->setLogger($logger); |
|
66
|
|
|
$this->setEvents($events); |
|
67
|
|
|
|
|
68
|
|
|
if ( is_null($em) ) { |
|
69
|
|
|
$this->setEntityManager( |
|
70
|
|
|
Database::init($configuration)->getEntityManager() |
|
71
|
|
|
); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->setEntityManager($em); |
|
74
|
|
|
$this->destroy_em = false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function __destruct() { |
|
80
|
|
|
|
|
81
|
|
|
if ($this->destroy_em) { |
|
82
|
|
|
$em = $this->getEntityManager(); |
|
83
|
|
|
$em->getConnection()->close(); |
|
84
|
|
|
$em->close(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function get($id) { |
|
90
|
|
|
|
|
91
|
|
|
$em = $this->getEntityManager(); |
|
92
|
|
|
|
|
93
|
|
|
$data = $em->find('Comodojo\Extender\Orm\Entities\Schedule', $id); |
|
94
|
|
|
|
|
95
|
|
|
return $data; |
|
96
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getByName($name) { |
|
100
|
|
|
|
|
101
|
|
|
$em = $this->getEntityManager(); |
|
102
|
|
|
|
|
103
|
|
|
$data = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findBy(["name" => $name]); |
|
104
|
|
|
|
|
105
|
|
|
if ( empty($data) ) return null; |
|
106
|
|
|
|
|
107
|
|
|
return $data[0]; |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getAll($ready = false) { |
|
112
|
|
|
|
|
113
|
|
|
$logger = $this->getLogger(); |
|
114
|
|
|
|
|
115
|
|
|
$time = new DateTime(); |
|
116
|
|
|
|
|
117
|
|
|
$em = $this->getEntityManager(); |
|
118
|
|
|
|
|
119
|
|
|
$standby = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findAll(); |
|
120
|
|
|
|
|
121
|
|
|
return $ready ? array_filter($standby, function($job) use ($time, $logger) { |
|
122
|
|
|
|
|
123
|
|
|
$name = $job->getName(); |
|
124
|
|
|
$id = $job->getId(); |
|
125
|
|
|
$enabled = $job->getEnabled(); |
|
126
|
|
|
$firstrun = $job->getFirstrun(); |
|
127
|
|
|
$lastrun = $job->getLastrun(); |
|
128
|
|
|
$expression = $job->getExpression(); |
|
129
|
|
|
|
|
130
|
|
|
if ( $lastrun !== null ) { |
|
131
|
|
|
$nextrun = $expression->getNextRunDate($lastrun); |
|
132
|
|
|
} else { |
|
133
|
|
|
$nextrun = $firstrun; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$shouldrun = $enabled === true ? ($nextrun <= $time) : false; |
|
137
|
|
|
|
|
138
|
|
|
$logger->debug("Job $name (id $id) will ".($shouldrun ? "" : "NOT ")."be executed", [ |
|
139
|
|
|
'ENABLED' => $enabled, |
|
140
|
|
|
'NEXTRUN' => $nextrun->format('r'), |
|
141
|
|
|
'SHOULDRUN' => $shouldrun |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
|
|
return $shouldrun; |
|
145
|
|
|
|
|
146
|
|
|
}) : $standby; |
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function add(Schedule $schedule) { |
|
151
|
|
|
|
|
152
|
|
|
$time = new DateTime(); |
|
153
|
|
|
|
|
154
|
|
|
$schedule->setFirstrun($schedule->getNextPlannedRun($time)); |
|
155
|
|
|
|
|
156
|
|
|
$this->getEvents()->emit( new ScheduleEvent('add', $schedule) ); |
|
157
|
|
|
|
|
158
|
|
|
$em = $this->getEntityManager(); |
|
159
|
|
|
|
|
160
|
|
|
$em->persist($schedule); |
|
161
|
|
|
|
|
162
|
|
|
$em->flush(); |
|
163
|
|
|
|
|
164
|
|
|
return $schedule->getId(); |
|
165
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function addBulk(array $schedules) { |
|
169
|
|
|
|
|
170
|
|
|
$time = new DateTime(); |
|
171
|
|
|
$records = []; |
|
172
|
|
|
$em = $this->getEntityManager(); |
|
173
|
|
|
|
|
174
|
|
|
foreach ($schedules as $key => $schedule) { |
|
175
|
|
|
|
|
176
|
|
|
try { |
|
177
|
|
|
|
|
178
|
|
|
$schedule->setFirstrun($schedule->getNextPlannedRun($time)); |
|
179
|
|
|
|
|
180
|
|
|
$this->getEvents()->emit( new ScheduleEvent('add', $schedule) ); |
|
181
|
|
|
|
|
182
|
|
|
$em->persist($schedule); |
|
183
|
|
|
|
|
184
|
|
|
$em->flush(); |
|
185
|
|
|
|
|
186
|
|
|
} catch (Exception $e) { |
|
187
|
|
|
|
|
188
|
|
|
$records[$key] = false; |
|
189
|
|
|
|
|
190
|
|
|
continue; |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$records[$key] = $schedule->getId(); |
|
195
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $records; |
|
199
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function edit(Schedule $schedule) { |
|
203
|
|
|
|
|
204
|
|
|
$em = $this->getEntityManager(); |
|
205
|
|
|
|
|
206
|
|
|
$id = $schedule->getId(); |
|
207
|
|
|
|
|
208
|
|
|
if ( empty($id) ) throw new InvalidArgumentException("Cannot edit scheule without id"); |
|
209
|
|
|
|
|
210
|
|
|
$old_schedule = $this->get($schedule->getId()); |
|
211
|
|
|
|
|
212
|
|
|
if ( empty($old_schedule) ) throw new InvalidArgumentException("Cannot find schedule with id $id"); |
|
213
|
|
|
|
|
214
|
|
|
$this->getEvents()->emit( new ScheduleEvent('edit', $schedule, $old_schedule) ); |
|
215
|
|
|
|
|
216
|
|
|
$old_schedule->merge($schedule); |
|
217
|
|
|
|
|
218
|
|
|
$em->persist($old_schedule); |
|
219
|
|
|
|
|
220
|
|
|
$em->flush(); |
|
221
|
|
|
|
|
222
|
|
|
return true; |
|
223
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function remove($id) { |
|
227
|
|
|
|
|
228
|
|
|
$em = $this->getEntityManager(); |
|
229
|
|
|
|
|
230
|
|
|
$schedule = $this->get($id); |
|
231
|
|
|
|
|
232
|
|
|
if ( is_null($schedule) ) throw new InvalidArgumentException("Cannot find scheule $id"); |
|
233
|
|
|
|
|
234
|
|
|
$this->getEvents()->emit( new ScheduleEvent('remove', $schedule) ); |
|
235
|
|
|
|
|
236
|
|
|
$em->remove($schedule); |
|
237
|
|
|
|
|
238
|
|
|
$em->flush(); |
|
239
|
|
|
|
|
240
|
|
|
return true; |
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function removeByName($name) { |
|
245
|
|
|
|
|
246
|
|
|
$em = $this->getEntityManager(); |
|
247
|
|
|
|
|
248
|
|
|
$schedule = $this->getByName($name); |
|
249
|
|
|
|
|
250
|
|
|
if ( is_null($schedule) ) throw new InvalidArgumentException("Cannot find scheule $name"); |
|
251
|
|
|
|
|
252
|
|
|
$this->getEvents()->emit( new ScheduleEvent('remove', $schedule) ); |
|
253
|
|
|
|
|
254
|
|
|
$em->remove($schedule); |
|
255
|
|
|
|
|
256
|
|
|
$em->flush(); |
|
257
|
|
|
|
|
258
|
|
|
return true; |
|
259
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function enable($id) { |
|
263
|
|
|
|
|
264
|
|
|
$em = $this->getEntityManager(); |
|
265
|
|
|
|
|
266
|
|
|
$schedule = $this->get($id); |
|
267
|
|
|
|
|
268
|
|
|
if ( is_null($schedule) ) throw new InvalidArgumentException("Cannot find scheule $id"); |
|
269
|
|
|
|
|
270
|
|
|
$this->getEvents()->emit( new ScheduleEvent('enable', $schedule) ); |
|
271
|
|
|
|
|
272
|
|
|
$schedule->setEnabled(true); |
|
273
|
|
|
|
|
274
|
|
|
$em->persist($schedule); |
|
275
|
|
|
|
|
276
|
|
|
$em->flush(); |
|
277
|
|
|
|
|
278
|
|
|
return true; |
|
279
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
public function enableByName($name) { |
|
283
|
|
|
|
|
284
|
|
|
$em = $this->getEntityManager(); |
|
285
|
|
|
|
|
286
|
|
|
$schedule = $this->getByName($name); |
|
287
|
|
|
|
|
288
|
|
|
if ( is_null($schedule) ) throw new InvalidArgumentException("Cannot find scheule $name"); |
|
289
|
|
|
|
|
290
|
|
|
$this->getEvents()->emit( new ScheduleEvent('enable', $schedule) ); |
|
291
|
|
|
|
|
292
|
|
|
$schedule->setEnabled(true); |
|
293
|
|
|
|
|
294
|
|
|
$em->persist($schedule); |
|
295
|
|
|
|
|
296
|
|
|
$em->flush(); |
|
297
|
|
|
|
|
298
|
|
|
return true; |
|
299
|
|
|
|
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
public function disable($id) { |
|
303
|
|
|
|
|
304
|
|
|
$em = $this->getEntityManager(); |
|
305
|
|
|
|
|
306
|
|
|
$schedule = $this->get($id); |
|
307
|
|
|
|
|
308
|
|
|
if ( is_null($schedule) ) throw new InvalidArgumentException("Cannot find scheule $id"); |
|
309
|
|
|
|
|
310
|
|
|
$this->getEvents()->emit( new ScheduleEvent('disable', $schedule) ); |
|
311
|
|
|
|
|
312
|
|
|
$schedule->setEnabled(false); |
|
313
|
|
|
|
|
314
|
|
|
$em->persist($schedule); |
|
315
|
|
|
|
|
316
|
|
|
$em->flush(); |
|
317
|
|
|
|
|
318
|
|
|
return true; |
|
319
|
|
|
|
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
public function disableByName($name) { |
|
323
|
|
|
|
|
324
|
|
|
$em = $this->getEntityManager(); |
|
325
|
|
|
|
|
326
|
|
|
$schedule = $this->getByName($name); |
|
327
|
|
|
|
|
328
|
|
|
if ( is_null($schedule) ) throw new InvalidArgumentException("Cannot find scheule $name"); |
|
329
|
|
|
|
|
330
|
|
|
$this->getEvents()->emit( new ScheduleEvent('disable', $schedule) ); |
|
331
|
|
|
|
|
332
|
|
|
$schedule->setEnabled(false); |
|
333
|
|
|
|
|
334
|
|
|
$em->persist($schedule); |
|
335
|
|
|
|
|
336
|
|
|
$em->flush(); |
|
337
|
|
|
|
|
338
|
|
|
return true; |
|
339
|
|
|
|
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
} |
|
343
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths