@@ -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; |
@@ -109,10 +109,10 @@ discard block |
||
| 109 | 109 | $lastExec = 0; |
| 110 | 110 | $hasError = false; |
| 111 | 111 | |
| 112 | - foreach($jobs as $cmd => $job) { |
|
| 112 | + foreach ($jobs as $cmd => $job) { |
|
| 113 | 113 | $execTime = $job['last_exec']; |
| 114 | - if($execTime > $lastExec) $lastExec = $execTime; |
|
| 115 | - if(!empty($job['error'])) { |
|
| 114 | + if ($execTime > $lastExec) $lastExec = $execTime; |
|
| 115 | + if (!empty($job['error'])) { |
|
| 116 | 116 | $hasError = true; |
| 117 | 117 | } |
| 118 | 118 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | 'Status', |
| 132 | 132 | ]; |
| 133 | 133 | |
| 134 | - if($hasError) { |
|
| 134 | + if ($hasError) { |
|
| 135 | 135 | $header[] = 'Error'; |
| 136 | 136 | } |
| 137 | 137 | |
@@ -141,15 +141,15 @@ discard block |
||
| 141 | 141 | ]); |
| 142 | 142 | |
| 143 | 143 | $cnt = 1; |
| 144 | - foreach($jobs as $cmd => $job) { |
|
| 145 | - if($cnt > 1) $table->addRow(new TableSeparator()); |
|
| 144 | + foreach ($jobs as $cmd => $job) { |
|
| 145 | + if ($cnt > 1) $table->addRow(new TableSeparator()); |
|
| 146 | 146 | $row = [ |
| 147 | 147 | $cmd, |
| 148 | 148 | $job['period'], |
| 149 | 149 | ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'), |
| 150 | 150 | $job['status'], |
| 151 | 151 | ]; |
| 152 | - if($hasError) { |
|
| 152 | + if ($hasError) { |
|
| 153 | 153 | $row[] = $job['error']; |
| 154 | 154 | } |
| 155 | 155 | $table->addRow($row); |
@@ -163,9 +163,9 @@ discard block |
||
| 163 | 163 | |
| 164 | 164 | $crontab = []; |
| 165 | 165 | |
| 166 | - if($command) { |
|
| 166 | + if ($command) { |
|
| 167 | 167 | $crontab = $this->getCronTab(); |
| 168 | - if($crontab === false) { |
|
| 168 | + if ($crontab === false) { |
|
| 169 | 169 | return false; |
| 170 | 170 | } |
| 171 | 171 | unset($crontab[$command]); |
@@ -184,11 +184,11 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
| 186 | 186 | |
| 187 | - if(!empty($jobs)) { |
|
| 187 | + if (!empty($jobs)) { |
|
| 188 | 188 | |
| 189 | - foreach($jobs as $cmd => $job) { |
|
| 189 | + foreach ($jobs as $cmd => $job) { |
|
| 190 | 190 | |
| 191 | - if($this->isActualJob($job)) { |
|
| 191 | + if ($this->isActualJob($job)) { |
|
| 192 | 192 | |
| 193 | 193 | $job['status'] = self::EXEC_STATUS_WORK; |
| 194 | 194 | $this->updaateJob($cmd, $job); |
@@ -200,12 +200,12 @@ discard block |
||
| 200 | 200 | $timeStart = microtime(true); |
| 201 | 201 | $returnCode = $command->run($cmdInput, $output); |
| 202 | 202 | |
| 203 | - if(!$returnCode) { |
|
| 203 | + if (!$returnCode) { |
|
| 204 | 204 | |
| 205 | 205 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
| 206 | 206 | |
| 207 | 207 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 208 | - if($this->logger) { |
|
| 208 | + if ($this->logger) { |
|
| 209 | 209 | $this->logger->alert($msg); |
| 210 | 210 | } |
| 211 | 211 | $output->writeln(PHP_EOL . $msg); |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | $job['error_code'] = $returnCode; |
| 217 | 217 | |
| 218 | 218 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 219 | - if($this->logger) { |
|
| 219 | + if ($this->logger) { |
|
| 220 | 220 | $this->logger->alert($msg); |
| 221 | 221 | } |
| 222 | 222 | $output->writeln(PHP_EOL . $msg); |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | $job['error'] = $e->getMessage(); |
| 229 | 229 | |
| 230 | 230 | |
| 231 | - if($this->logger) { |
|
| 231 | + if ($this->logger) { |
|
| 232 | 232 | $this->logger->error($e, ['command' => $cmd]); |
| 233 | 233 | } |
| 234 | 234 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
@@ -250,21 +250,21 @@ discard block |
||
| 250 | 250 | |
| 251 | 251 | protected function isActualJob(&$job) { |
| 252 | 252 | |
| 253 | - if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 253 | + if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 254 | 254 | return false; |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | $period = intval($job['period']); |
| 258 | 258 | |
| 259 | - if($period > 0) { |
|
| 260 | - if($period < $this->minAgentPeriod) { |
|
| 259 | + if ($period > 0) { |
|
| 260 | + if ($period < $this->minAgentPeriod) { |
|
| 261 | 261 | $job['orig_period'] = $period; |
| 262 | 262 | $period = $job['period'] = $this->minAgentPeriod; |
| 263 | 263 | } |
| 264 | - if(time() - $job['last_exec'] >= $period) { |
|
| 264 | + if (time() - $job['last_exec'] >= $period) { |
|
| 265 | 265 | return true; |
| 266 | 266 | } |
| 267 | - } else if(!empty($job['times'])) { |
|
| 267 | + } else if (!empty($job['times'])) { |
|
| 268 | 268 | //TODO: |
| 269 | 269 | } |
| 270 | 270 | |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | public function getCronJobs() { |
| 275 | 275 | |
| 276 | 276 | $crontab = $this->getCronTab(); |
| 277 | - if($crontab === false) { |
|
| 277 | + if ($crontab === false) { |
|
| 278 | 278 | return false; |
| 279 | 279 | } |
| 280 | 280 | |
@@ -284,9 +284,9 @@ discard block |
||
| 284 | 284 | $commands = $app->all(); |
| 285 | 285 | |
| 286 | 286 | $selfCommands = []; |
| 287 | - foreach($commands as $command) { |
|
| 287 | + foreach ($commands as $command) { |
|
| 288 | 288 | /** @var BxCommand $command */ |
| 289 | - if($command instanceof BxCommand) { |
|
| 289 | + if ($command instanceof BxCommand) { |
|
| 290 | 290 | $name = $command->getName(); |
| 291 | 291 | $selfCommands[$name] = [ |
| 292 | 292 | 'object' => $command, |
@@ -296,19 +296,19 @@ discard block |
||
| 296 | 296 | |
| 297 | 297 | $agents = []; |
| 298 | 298 | $reader = new AnnotationReader(); |
| 299 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 299 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 300 | 300 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 301 | 301 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 302 | 302 | |
| 303 | - foreach($annotations as $annotation) { |
|
| 304 | - if($annotation instanceof Agent) { |
|
| 303 | + foreach ($annotations as $annotation) { |
|
| 304 | + if ($annotation instanceof Agent) { |
|
| 305 | 305 | $agents[$cmd] = $annotation->toArray(); |
| 306 | 306 | } |
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | - foreach($crontab as $cmd => $job) { |
|
| 311 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 310 | + foreach ($crontab as $cmd => $job) { |
|
| 311 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 312 | 312 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 313 | 313 | } |
| 314 | 314 | } |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | |
| 328 | 328 | $crontab = $this->getCronTab(); |
| 329 | 329 | |
| 330 | - if($crontab === false) { |
|
| 330 | + if ($crontab === false) { |
|
| 331 | 331 | return false; |
| 332 | 332 | } else { |
| 333 | 333 | $crontab = array_merge($crontab, $changedAgents); |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | $fh = fopen($filename, 'c'); |
| 346 | 346 | if (flock($fh, LOCK_EX)) { |
| 347 | 347 | ftruncate($fh, 0); |
| 348 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 348 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 349 | 349 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 350 | 350 | } |
| 351 | 351 | } else { |
@@ -365,11 +365,11 @@ discard block |
||
| 365 | 365 | $filename = EnvHelper::getCrontabFile(); |
| 366 | 366 | |
| 367 | 367 | $fh = fopen($filename, 'r'); |
| 368 | - if(flock($fh, LOCK_SH)) { |
|
| 368 | + if (flock($fh, LOCK_SH)) { |
|
| 369 | 369 | $cronTab = []; |
| 370 | - if($data = fread($fh, filesize($filename))) { |
|
| 370 | + if ($data = fread($fh, filesize($filename))) { |
|
| 371 | 371 | $decoded = json_decode($data, true); |
| 372 | - if(is_array($decoded)) { |
|
| 372 | + if (is_array($decoded)) { |
|
| 373 | 373 | $cronTab = $decoded; |
| 374 | 374 | } |
| 375 | 375 | } |
@@ -384,14 +384,14 @@ discard block |
||
| 384 | 384 | |
| 385 | 385 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
| 386 | 386 | |
| 387 | - if($sort == self::SORT_TIME) { |
|
| 387 | + if ($sort == self::SORT_TIME) { |
|
| 388 | 388 | $sorting = []; |
| 389 | - foreach($crontab as $cmd => $data) { |
|
| 389 | + foreach ($crontab as $cmd => $data) { |
|
| 390 | 390 | $sorting[$cmd] = $data['last_exec']; |
| 391 | 391 | } |
| 392 | 392 | arsort($sorting, SORT_NUMERIC); |
| 393 | 393 | $sorted = []; |
| 394 | - foreach($sorting as $cmd => $time) { |
|
| 394 | + foreach ($sorting as $cmd => $time) { |
|
| 395 | 395 | $sorted[$cmd] = $crontab[$cmd]; |
| 396 | 396 | } |
| 397 | 397 | $crontab = $sorted; |