@@ -40,45 +40,45 @@ discard block |
||
40 | 40 | set_time_limit(EnvHelper::getCrontabTimeout()); |
41 | 41 | |
42 | 42 | $logger = EnvHelper::getLogger('bx_cron'); |
43 | - if($logger) { |
|
43 | + if ($logger) { |
|
44 | 44 | $this->setLogger($logger); |
45 | 45 | } |
46 | 46 | |
47 | 47 | $showStatus = $input->getOption('status'); |
48 | 48 | $byTime = $input->getOption('bytime'); |
49 | - if($showStatus) { |
|
49 | + if ($showStatus) { |
|
50 | 50 | $sort = ($byTime ? self::SORT_TIME : self::SORT_NAME); |
51 | 51 | $this->showStatus($output, $sort); |
52 | 52 | return 0; |
53 | 53 | } |
54 | 54 | |
55 | - if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
56 | - if($this->logger) { |
|
55 | + if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
56 | + if ($this->logger) { |
|
57 | 57 | $this->logger->alert('BxCron switch off'); |
58 | 58 | } |
59 | 59 | return 0; |
60 | 60 | } |
61 | 61 | |
62 | - if(!$this->lock()) { |
|
62 | + if (!$this->lock()) { |
|
63 | 63 | $msg = 'The command is already running in another process.'; |
64 | 64 | $output->writeln($msg); |
65 | - if($this->logger) { |
|
65 | + if ($this->logger) { |
|
66 | 66 | $this->logger->warning($msg); |
67 | 67 | } |
68 | 68 | return 0; |
69 | 69 | } |
70 | 70 | |
71 | - if($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
71 | + if ($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
72 | 72 | $msg = sprintf("Sleep in interval %s", $sleepInterval); |
73 | 73 | $output->writeln($msg); |
74 | - if($this->logger) { |
|
74 | + if ($this->logger) { |
|
75 | 75 | $this->logger->warning($msg); |
76 | 76 | } |
77 | 77 | return 0; |
78 | 78 | } |
79 | 79 | |
80 | 80 | $clean = $input->getOption('clean'); |
81 | - if($clean) { |
|
81 | + if ($clean) { |
|
82 | 82 | $command = $this->getApplication()->find($clean); |
83 | 83 | $this->cleanJob($command->getName()); |
84 | 84 | $output->writeln($command->getName() . " will be executed now"); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | } |
87 | 87 | |
88 | 88 | $cleanAll = $input->getOption('all'); |
89 | - if($cleanAll) { |
|
89 | + if ($cleanAll) { |
|
90 | 90 | $this->cleanJob(); |
91 | 91 | $output->writeln("All commands will be executed now"); |
92 | 92 | return 0; |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | $lastExec = 0; |
112 | 112 | $hasError = false; |
113 | 113 | |
114 | - foreach($jobs as $cmd => $job) { |
|
114 | + foreach ($jobs as $cmd => $job) { |
|
115 | 115 | $execTime = $job['last_exec']; |
116 | - if($execTime > $lastExec) $lastExec = $execTime; |
|
117 | - if(!empty($job['error'])) { |
|
116 | + if ($execTime > $lastExec) $lastExec = $execTime; |
|
117 | + if (!empty($job['error'])) { |
|
118 | 118 | $hasError = true; |
119 | 119 | } |
120 | 120 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | 'Status', |
134 | 134 | ]; |
135 | 135 | |
136 | - if($hasError) { |
|
136 | + if ($hasError) { |
|
137 | 137 | $header[] = 'Error'; |
138 | 138 | } |
139 | 139 | |
@@ -143,15 +143,15 @@ discard block |
||
143 | 143 | ]); |
144 | 144 | |
145 | 145 | $cnt = 1; |
146 | - foreach($jobs as $cmd => $job) { |
|
147 | - if($cnt > 1) $table->addRow(new TableSeparator()); |
|
146 | + foreach ($jobs as $cmd => $job) { |
|
147 | + if ($cnt > 1) $table->addRow(new TableSeparator()); |
|
148 | 148 | $row = [ |
149 | 149 | $cmd, |
150 | 150 | $job['period'], |
151 | 151 | ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'), |
152 | 152 | $job['status'], |
153 | 153 | ]; |
154 | - if($hasError) { |
|
154 | + if ($hasError) { |
|
155 | 155 | $row[] = $job['error']; |
156 | 156 | } |
157 | 157 | $table->addRow($row); |
@@ -165,9 +165,9 @@ discard block |
||
165 | 165 | |
166 | 166 | $crontab = []; |
167 | 167 | |
168 | - if($command) { |
|
168 | + if ($command) { |
|
169 | 169 | $crontab = $this->getCronTab(); |
170 | - if($crontab === false) { |
|
170 | + if ($crontab === false) { |
|
171 | 171 | return false; |
172 | 172 | } |
173 | 173 | unset($crontab[$command]); |
@@ -182,12 +182,12 @@ discard block |
||
182 | 182 | $allTimeout = EnvHelper::getCrontabTimeout(); |
183 | 183 | $workTime = 0; |
184 | 184 | |
185 | - if(!empty($jobs)) { |
|
185 | + if (!empty($jobs)) { |
|
186 | 186 | |
187 | - foreach($jobs as $cmd => $job) { |
|
187 | + foreach ($jobs as $cmd => $job) { |
|
188 | 188 | |
189 | 189 | $job['cmd'] = $cmd; |
190 | - if($this->isActualJob($job)) { |
|
190 | + if ($this->isActualJob($job)) { |
|
191 | 191 | |
192 | 192 | $job['status'] = self::EXEC_STATUS_WORK; |
193 | 193 | $job['start_time'] = time(); |
@@ -202,12 +202,12 @@ discard block |
||
202 | 202 | $returnCode = $command->run($cmdInput, $output); |
203 | 203 | $execTime = microtime(true) - $timeStart; |
204 | 204 | |
205 | - if(!$returnCode) { |
|
205 | + if (!$returnCode) { |
|
206 | 206 | |
207 | 207 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
208 | 208 | |
209 | 209 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime); |
210 | - if($this->logger) { |
|
210 | + if ($this->logger) { |
|
211 | 211 | $this->logger->alert($msg); |
212 | 212 | } |
213 | 213 | $output->writeln(PHP_EOL . $msg); |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | $job['error_code'] = $returnCode; |
219 | 219 | |
220 | 220 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime); |
221 | - if($this->logger) { |
|
221 | + if ($this->logger) { |
|
222 | 222 | $this->logger->alert($msg); |
223 | 223 | } |
224 | 224 | $output->writeln(PHP_EOL . $msg); |
@@ -230,14 +230,14 @@ discard block |
||
230 | 230 | $job['error'] = $e->getMessage(); |
231 | 231 | |
232 | 232 | |
233 | - if($this->logger) { |
|
233 | + if ($this->logger) { |
|
234 | 234 | $this->logger->error($e, ['command' => $cmd]); |
235 | 235 | } |
236 | 236 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
237 | 237 | |
238 | 238 | } finally { |
239 | 239 | |
240 | - if(!$execTime) { |
|
240 | + if (!$execTime) { |
|
241 | 241 | $execTime = microtime(true) - $timeStart; |
242 | 242 | } |
243 | 243 | $job['last_exec'] = time(); |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | $this->updateJob($cmd, $job); |
248 | 248 | |
249 | 249 | $workTime += $execTime; |
250 | - if($workTime * 2 > $allTimeout) { |
|
250 | + if ($workTime * 2 > $allTimeout) { |
|
251 | 251 | break; |
252 | 252 | } |
253 | 253 | /* |
@@ -263,31 +263,31 @@ discard block |
||
263 | 263 | { |
264 | 264 | $actual = false; |
265 | 265 | |
266 | - if($job['status'] == self::EXEC_STATUS_WORK) { |
|
267 | - if($job['start_time'] && $job['start_time'] < (time() - self::RESTART_TIME)) { |
|
266 | + if ($job['status'] == self::EXEC_STATUS_WORK) { |
|
267 | + if ($job['start_time'] && $job['start_time'] < (time() - self::RESTART_TIME)) { |
|
268 | 268 | $actual = true; |
269 | 269 | } |
270 | 270 | } |
271 | 271 | |
272 | 272 | $period = intval($job['period']); |
273 | 273 | |
274 | - if($period > 0) { |
|
275 | - if(time() - $job['last_exec'] >= $period) { |
|
274 | + if ($period > 0) { |
|
275 | + if (time() - $job['last_exec'] >= $period) { |
|
276 | 276 | $actual = true; |
277 | 277 | } |
278 | - } else if(!empty($job['times'])) { |
|
278 | + } else if (!empty($job['times'])) { |
|
279 | 279 | //TODO: |
280 | 280 | } |
281 | 281 | |
282 | - if($actual && !empty($job['interval'])) { |
|
282 | + if ($actual && !empty($job['interval'])) { |
|
283 | 283 | $times = explode('-', $job['interval']); |
284 | - if(count($times) == 2) { |
|
284 | + if (count($times) == 2) { |
|
285 | 285 | $minTime = Time24::validateTimeString($times[0]); |
286 | 286 | $maxTime = Time24::validateTimeString($times[1]); |
287 | - if($minTime && $maxTime) { |
|
288 | - if(!Time24::inInterval($minTime, $maxTime)) { |
|
287 | + if ($minTime && $maxTime) { |
|
288 | + if (!Time24::inInterval($minTime, $maxTime)) { |
|
289 | 289 | $msg = sprintf("%s not in interval %s", $job['cmd'], $job['interval']); |
290 | - if($this->logger) { |
|
290 | + if ($this->logger) { |
|
291 | 291 | $this->logger->alert($msg); |
292 | 292 | } |
293 | 293 | return false; |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | public function getCronJobs(): array |
302 | 302 | { |
303 | 303 | $crontab = $this->getCronTab(); |
304 | - if($crontab === false) { |
|
304 | + if ($crontab === false) { |
|
305 | 305 | return []; |
306 | 306 | } |
307 | 307 | |
@@ -311,9 +311,9 @@ discard block |
||
311 | 311 | $commands = $app->all(); |
312 | 312 | |
313 | 313 | $selfCommands = []; |
314 | - foreach($commands as $command) { |
|
314 | + foreach ($commands as $command) { |
|
315 | 315 | /** @var BxCommand $command */ |
316 | - if($command instanceof BxCommand) { |
|
316 | + if ($command instanceof BxCommand) { |
|
317 | 317 | $name = $command->getName(); |
318 | 318 | $selfCommands[$name] = [ |
319 | 319 | 'object' => $command, |
@@ -323,19 +323,19 @@ discard block |
||
323 | 323 | |
324 | 324 | $agents = []; |
325 | 325 | $reader = new AnnotationReader(); |
326 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
326 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
327 | 327 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
328 | 328 | $annotations = $reader->getClassAnnotations($reflectionClass); |
329 | 329 | |
330 | - foreach($annotations as $annotation) { |
|
331 | - if($annotation instanceof Agent) { |
|
330 | + foreach ($annotations as $annotation) { |
|
331 | + if ($annotation instanceof Agent) { |
|
332 | 332 | $agents[$cmd] = $annotation->toArray(); |
333 | 333 | } |
334 | 334 | } |
335 | 335 | } |
336 | 336 | |
337 | - foreach($crontab as $cmd => $job) { |
|
338 | - if(is_array($job) && isset($agents[$cmd])) { |
|
337 | + foreach ($crontab as $cmd => $job) { |
|
338 | + if (is_array($job) && isset($agents[$cmd])) { |
|
339 | 339 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
340 | 340 | } |
341 | 341 | } |
@@ -354,7 +354,7 @@ discard block |
||
354 | 354 | |
355 | 355 | $crontab = $this->getCronTab(); |
356 | 356 | |
357 | - if($crontab === false) { |
|
357 | + if ($crontab === false) { |
|
358 | 358 | return false; |
359 | 359 | } else { |
360 | 360 | $crontab = array_merge($crontab, $changedAgents); |
@@ -372,7 +372,7 @@ discard block |
||
372 | 372 | $fh = fopen($filename, 'c'); |
373 | 373 | if (flock($fh, LOCK_EX)) { |
374 | 374 | ftruncate($fh, 0); |
375 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
375 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
376 | 376 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
377 | 377 | } |
378 | 378 | } else { |
@@ -392,16 +392,16 @@ discard block |
||
392 | 392 | $filename = EnvHelper::getCrontabFile(); |
393 | 393 | |
394 | 394 | $fh = fopen($filename, 'r'); |
395 | - if(!$fh) { |
|
395 | + if (!$fh) { |
|
396 | 396 | return false; |
397 | 397 | } |
398 | - if(flock($fh, LOCK_SH)) { |
|
398 | + if (flock($fh, LOCK_SH)) { |
|
399 | 399 | $cronTab = []; |
400 | 400 | clearstatcache(); |
401 | 401 | $filesize = (int) filesize($filename); |
402 | - if($filesize > 0 && $data = fread($fh, $filesize)) { |
|
402 | + if ($filesize > 0 && $data = fread($fh, $filesize)) { |
|
403 | 403 | $decoded = json_decode($data, true); |
404 | - if(is_array($decoded)) { |
|
404 | + if (is_array($decoded)) { |
|
405 | 405 | $cronTab = $decoded; |
406 | 406 | } else { |
407 | 407 | throw new \Exception("Unable to parse cronTab"); |
@@ -418,14 +418,14 @@ discard block |
||
418 | 418 | |
419 | 419 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
420 | 420 | |
421 | - if($sort == self::SORT_TIME) { |
|
421 | + if ($sort == self::SORT_TIME) { |
|
422 | 422 | $sorting = []; |
423 | - foreach($crontab as $cmd => $data) { |
|
423 | + foreach ($crontab as $cmd => $data) { |
|
424 | 424 | $sorting[$cmd] = $data['last_exec']; |
425 | 425 | } |
426 | 426 | arsort($sorting, SORT_NUMERIC); |
427 | 427 | $sorted = []; |
428 | - foreach($sorting as $cmd => $time) { |
|
428 | + foreach ($sorting as $cmd => $time) { |
|
429 | 429 | $sorted[$cmd] = $crontab[$cmd]; |
430 | 430 | } |
431 | 431 | $crontab = $sorted; |