1
|
|
|
<?php namespace Comodojo\Extender\Schedule; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
4
|
|
|
use \Comodojo\Foundation\Events\Manager as EventsManager; |
5
|
|
|
use \Comodojo\Daemon\Traits\LoggerTrait; |
6
|
|
|
use \Comodojo\Daemon\Traits\EventsTrait; |
7
|
|
|
use \Comodojo\Extender\Traits\ConfigurationTrait; |
8
|
|
|
use \Comodojo\Extender\Traits\EntityManagerTrait; |
9
|
|
|
use \Comodojo\Extender\Orm\Entities\Schedule; |
10
|
|
|
use \Doctrine\ORM\EntityManager; |
11
|
|
|
use \Psr\Log\LoggerInterface; |
12
|
|
|
use \DateTime; |
13
|
|
|
use \Exception; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @package Comodojo Extender |
17
|
|
|
* @author Marco Giovinazzi <[email protected]> |
18
|
|
|
* @license MIT |
19
|
|
|
* |
20
|
|
|
* LICENSE: |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
28
|
|
|
* THE SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
class Manager { |
32
|
|
|
|
33
|
|
|
use ConfigurationTrait; |
34
|
|
|
use LoggerTrait; |
35
|
|
|
use EventsTrait; |
36
|
|
|
use EntityManagerTrait; |
37
|
|
|
|
38
|
|
|
// protected $locker; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class constructor |
42
|
|
|
* |
43
|
|
|
* @param string $manager_name |
|
|
|
|
44
|
|
|
* @param Configuration $configuration |
45
|
|
|
* @param LoggerInterface $logger |
46
|
|
|
* @param TasksTable $tasks |
|
|
|
|
47
|
|
|
* @param EventsManager $events |
48
|
|
|
* @param EntityManager $em |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
51
|
|
|
Configuration $configuration, |
52
|
|
|
LoggerInterface $logger, |
53
|
|
|
EventsManager $events, |
54
|
|
|
EntityManager $em = null |
55
|
|
|
) { |
56
|
|
|
|
57
|
|
|
$this->setConfiguration($configuration); |
58
|
|
|
$this->setLogger($logger); |
59
|
|
|
$this->setEvents($events); |
60
|
|
|
|
61
|
|
|
$em = is_null($em) ? Database::init($configuration)->getEntityManager() : $em; |
62
|
|
|
$this->setEntityManager($em); |
63
|
|
|
|
64
|
|
|
// $lock_path = $configuration->get('lockfiles-path'); |
|
|
|
|
65
|
|
|
// $this->locker = new Locker("$lock_path/schedule.lock"); |
|
|
|
|
66
|
|
|
// $this->locker->lock(0); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function get($id) { |
70
|
|
|
|
71
|
|
|
$em = $this->getEntityManager(); |
72
|
|
|
|
73
|
|
|
$data = $em->find('Comodojo\Extender\Orm\Entities\Schedule', $id); |
74
|
|
|
|
75
|
|
|
//if ( empty($data) ) return null; |
|
|
|
|
76
|
|
|
|
77
|
|
|
//return $data[0]; |
|
|
|
|
78
|
|
|
|
79
|
|
|
return $data; |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getByName($name) { |
84
|
|
|
|
85
|
|
|
$em = $this->getEntityManager(); |
86
|
|
|
|
87
|
|
|
$data = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findBy(["name" => $name]); |
88
|
|
|
|
89
|
|
|
if ( empty($data) ) return null; |
90
|
|
|
|
91
|
|
|
return $data[0]; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getAll($ready = false) { |
96
|
|
|
|
97
|
|
|
$logger = $this->getLogger(); |
98
|
|
|
|
99
|
|
|
$time = new DateTime(); |
100
|
|
|
|
101
|
|
|
$em = $this->getEntityManager(); |
102
|
|
|
|
103
|
|
|
$standby = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findAll(); |
104
|
|
|
|
105
|
|
|
return $ready ? array_filter($standby, function($job) use ($time, $logger) { |
106
|
|
|
|
107
|
|
|
$name = $job->getName(); |
108
|
|
|
$id = $job->getId(); |
109
|
|
|
$enabled = $job->getEnabled(); |
110
|
|
|
$firstrun = $job->getFirstrun(); |
111
|
|
|
$lastrun = $job->getLastrun(); |
112
|
|
|
$expression = $job->getExpression(); |
113
|
|
|
|
114
|
|
|
if ( $lastrun !== null ) { |
115
|
|
|
$nextrun = $expression->getNextRunDate($lastrun); |
116
|
|
|
} else { |
117
|
|
|
$nextrun = $expression->getNextRunDate($firstrun); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$shouldrun = $nextrun <= $time; |
121
|
|
|
|
122
|
|
|
$logger->debug("Job $name (id $id) will ".($shouldrun ? "" : "NOT ")."be executed", [ |
123
|
|
|
'ENABLED' => $enabled, |
124
|
|
|
'NEXTRUN' => $nextrun->format('r'), |
125
|
|
|
'SHOULDRUN' => $shouldrun |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
return $shouldrun; |
129
|
|
|
|
130
|
|
|
}) : $standby; |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getNextCycleTimestamp() { |
135
|
|
|
|
136
|
|
|
$items = $this->getAll(); |
137
|
|
|
|
138
|
|
|
$date = new DateTime(); |
139
|
|
|
$timestamps = []; |
140
|
|
|
|
141
|
|
|
foreach ($items as $schedule) { |
142
|
|
|
|
143
|
|
|
$timestamps[] = $schedule->getNextPlannedRun($date)->getTimestamp(); |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return empty($timestamps) ? 0 : min($timestamps); |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function updateFromResults(array $results) { |
152
|
|
|
|
153
|
|
|
$em = $this->getEntityManager(); |
154
|
|
|
|
155
|
|
|
foreach ($results as $result) { |
156
|
|
|
|
157
|
|
|
$id = $result->jid; |
158
|
|
|
|
159
|
|
|
if ( $id === null ) continue; |
160
|
|
|
|
161
|
|
|
$job = $this->get($id); |
162
|
|
|
|
163
|
|
|
if ( $job === null ) continue; |
164
|
|
|
|
165
|
|
|
if ( $job->getFirstrun() === null ) $job->setFirstrun($result->start); |
166
|
|
|
|
167
|
|
|
$job->setLastrun($result->start); |
168
|
|
|
|
169
|
|
|
$em->persist($job); |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$em->flush(); |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function add(Schedule $schedule) { |
178
|
|
|
|
179
|
|
|
$time = new DateTime(); |
180
|
|
|
|
181
|
|
|
$schedule->setFirstrun($schedule->getNextPlannedRun($time)); |
182
|
|
|
|
183
|
|
|
$em = $this->getEntityManager(); |
184
|
|
|
|
185
|
|
|
$em->persist($schedule); |
186
|
|
|
|
187
|
|
|
$em->flush(); |
188
|
|
|
|
189
|
|
|
return $schedule->getId(); |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function addBulk(array $schedules) { |
194
|
|
|
|
195
|
|
|
$em = $this->getEntityManager(); |
196
|
|
|
|
197
|
|
|
$records = []; |
198
|
|
|
|
199
|
|
|
foreach ($schedules as $key => $schedule) { |
200
|
|
|
|
201
|
|
|
try { |
202
|
|
|
|
203
|
|
|
$em->persist($schedule); |
204
|
|
|
|
205
|
|
|
$em->flush(); |
206
|
|
|
|
207
|
|
|
} catch (Exception $e) { |
208
|
|
|
|
209
|
|
|
$records[$key] = false; |
210
|
|
|
|
211
|
|
|
continue; |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$records[$key] = $scheudle->getId(); |
|
|
|
|
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $records; |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function remove(Schedule $schedule) { |
224
|
|
|
|
225
|
|
|
$em = $this->getEntityManager(); |
226
|
|
|
|
227
|
|
|
$em->remove($schedule); |
228
|
|
|
|
229
|
|
|
$em->flush(); |
230
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function edit(Schedule $schedule) { |
234
|
|
|
|
235
|
|
|
$em = $this->getEntityManager(); |
236
|
|
|
|
237
|
|
|
$id = $schedule->getId(); |
238
|
|
|
|
239
|
|
|
if ( empty($id) ) throw new Exception("Cannot edit scheule without id"); |
240
|
|
|
|
241
|
|
|
$old_schedule = $this->get($schedule->getId()); |
242
|
|
|
|
243
|
|
|
if ( empty($old_schedule) ) throw new Exception("Cannot find schedule with id $id"); |
244
|
|
|
|
245
|
|
|
$old_schedule->merge($schedule); |
246
|
|
|
|
247
|
|
|
$em->persist($old_schedule); |
248
|
|
|
|
249
|
|
|
$em->flush(); |
250
|
|
|
|
251
|
|
|
return true; |
252
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
View Code Duplication |
public function enable($name) { |
|
|
|
|
256
|
|
|
|
257
|
|
|
$em = $this->getEntityManager(); |
258
|
|
|
|
259
|
|
|
$schedule = $this->getByName($name); |
260
|
|
|
|
261
|
|
|
if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name"); |
262
|
|
|
|
263
|
|
|
$schedule->setEnabled(true); |
264
|
|
|
|
265
|
|
|
$em->persist($schedule); |
266
|
|
|
|
267
|
|
|
$em->flush(); |
268
|
|
|
|
269
|
|
|
return true; |
270
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
View Code Duplication |
public function disable($name) { |
|
|
|
|
274
|
|
|
|
275
|
|
|
$em = $this->getEntityManager(); |
276
|
|
|
|
277
|
|
|
$schedule = $this->getByName($name); |
278
|
|
|
|
279
|
|
|
if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name"); |
280
|
|
|
|
281
|
|
|
$schedule->setEnabled(false); |
282
|
|
|
|
283
|
|
|
$em->persist($schedule); |
284
|
|
|
|
285
|
|
|
$em->flush(); |
286
|
|
|
|
287
|
|
|
return true; |
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
} |
292
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.