@@ -42,45 +42,45 @@ discard block |
||
| 42 | 42 | set_time_limit(EnvHelper::getCrontabTimeout()); |
| 43 | 43 | |
| 44 | 44 | $logger = EnvHelper::getLogger('bx_cron'); |
| 45 | - if($logger) { |
|
| 45 | + if ($logger) { |
|
| 46 | 46 | $this->setLogger($logger); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $showStatus = $input->getOption('status'); |
| 50 | 50 | $byTime = $input->getOption('bytime'); |
| 51 | - if($showStatus) { |
|
| 51 | + if ($showStatus) { |
|
| 52 | 52 | $sort = ($byTime ? self::SORT_TIME : self::SORT_NAME); |
| 53 | 53 | $this->showStatus($output, $sort); |
| 54 | 54 | return 0; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 58 | - if($this->logger) { |
|
| 57 | + if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 58 | + if ($this->logger) { |
|
| 59 | 59 | $this->logger->alert('BxCron switch off'); |
| 60 | 60 | } |
| 61 | 61 | return 0; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - if(!$this->lock()) { |
|
| 64 | + if (!$this->lock()) { |
|
| 65 | 65 | $msg = 'The command is already running in another process.'; |
| 66 | 66 | $output->writeln($msg); |
| 67 | - if($this->logger) { |
|
| 67 | + if ($this->logger) { |
|
| 68 | 68 | $this->logger->warning($msg); |
| 69 | 69 | } |
| 70 | 70 | return 0; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - if($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
| 73 | + if ($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
| 74 | 74 | $msg = sprintf("Sleep in interval %s", $sleepInterval); |
| 75 | 75 | $output->writeln($msg); |
| 76 | - if($this->logger) { |
|
| 76 | + if ($this->logger) { |
|
| 77 | 77 | $this->logger->warning($msg); |
| 78 | 78 | } |
| 79 | 79 | return 0; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | $clean = $input->getOption('clean'); |
| 83 | - if($clean) { |
|
| 83 | + if ($clean) { |
|
| 84 | 84 | $command = $this->getApplication()->find($clean); |
| 85 | 85 | $this->cleanJob($command->getName()); |
| 86 | 86 | $output->writeln($command->getName() . " will be executed now"); |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | $cleanAll = $input->getOption('all'); |
| 91 | - if($cleanAll) { |
|
| 91 | + if ($cleanAll) { |
|
| 92 | 92 | $this->cleanJob(); |
| 93 | 93 | $output->writeln("All commands will be executed now"); |
| 94 | 94 | return 0; |
@@ -113,10 +113,10 @@ discard block |
||
| 113 | 113 | $lastExec = 0; |
| 114 | 114 | $hasError = false; |
| 115 | 115 | |
| 116 | - foreach($jobs as $cmd => $job) { |
|
| 116 | + foreach ($jobs as $cmd => $job) { |
|
| 117 | 117 | $execTime = $job['last_exec']; |
| 118 | - if($execTime > $lastExec) $lastExec = $execTime; |
|
| 119 | - if(!empty($job['error'])) { |
|
| 118 | + if ($execTime > $lastExec) $lastExec = $execTime; |
|
| 119 | + if (!empty($job['error'])) { |
|
| 120 | 120 | $hasError = true; |
| 121 | 121 | } |
| 122 | 122 | } |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | 'Status', |
| 136 | 136 | ]; |
| 137 | 137 | |
| 138 | - if($hasError) { |
|
| 138 | + if ($hasError) { |
|
| 139 | 139 | $header[] = 'Error'; |
| 140 | 140 | } |
| 141 | 141 | |
@@ -145,15 +145,15 @@ discard block |
||
| 145 | 145 | ]); |
| 146 | 146 | |
| 147 | 147 | $cnt = 1; |
| 148 | - foreach($jobs as $cmd => $job) { |
|
| 149 | - if($cnt > 1) $table->addRow(new TableSeparator()); |
|
| 148 | + foreach ($jobs as $cmd => $job) { |
|
| 149 | + if ($cnt > 1) $table->addRow(new TableSeparator()); |
|
| 150 | 150 | $row = [ |
| 151 | 151 | $cmd, |
| 152 | 152 | $job['period'], |
| 153 | 153 | ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'), |
| 154 | 154 | $job['status'], |
| 155 | 155 | ]; |
| 156 | - if($hasError) { |
|
| 156 | + if ($hasError) { |
|
| 157 | 157 | $row[] = $job['error']; |
| 158 | 158 | } |
| 159 | 159 | $table->addRow($row); |
@@ -167,9 +167,9 @@ discard block |
||
| 167 | 167 | |
| 168 | 168 | $crontab = []; |
| 169 | 169 | |
| 170 | - if($command) { |
|
| 170 | + if ($command) { |
|
| 171 | 171 | $crontab = $this->getCronTab(); |
| 172 | - if($crontab === false) { |
|
| 172 | + if ($crontab === false) { |
|
| 173 | 173 | return false; |
| 174 | 174 | } |
| 175 | 175 | unset($crontab[$command]); |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | $allTimeout = EnvHelper::getCrontabTimeout(); |
| 185 | 185 | $workTime = 0; |
| 186 | 186 | |
| 187 | - if(!empty($jobs)) { |
|
| 187 | + if (!empty($jobs)) { |
|
| 188 | 188 | |
| 189 | 189 | /* |
| 190 | 190 | * Минимально допустимый период выполнения одной задачи |
@@ -195,9 +195,9 @@ discard block |
||
| 195 | 195 | $this->logger->alert($msg); |
| 196 | 196 | $output->writeln($msg); |
| 197 | 197 | |
| 198 | - foreach($jobs as $cmd => $job) { |
|
| 198 | + foreach ($jobs as $cmd => $job) { |
|
| 199 | 199 | |
| 200 | - if($this->isActualJob($job)) { |
|
| 200 | + if ($this->isActualJob($job)) { |
|
| 201 | 201 | |
| 202 | 202 | $job['status'] = self::EXEC_STATUS_WORK; |
| 203 | 203 | $job['start_time'] = time(); |
@@ -212,12 +212,12 @@ discard block |
||
| 212 | 212 | $returnCode = $command->run($cmdInput, $output); |
| 213 | 213 | $execTime = microtime(true) - $timeStart; |
| 214 | 214 | |
| 215 | - if(!$returnCode) { |
|
| 215 | + if (!$returnCode) { |
|
| 216 | 216 | |
| 217 | 217 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
| 218 | 218 | |
| 219 | 219 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime); |
| 220 | - if($this->logger) { |
|
| 220 | + if ($this->logger) { |
|
| 221 | 221 | $this->logger->alert($msg); |
| 222 | 222 | } |
| 223 | 223 | $output->writeln(PHP_EOL . $msg); |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | $job['error_code'] = $returnCode; |
| 229 | 229 | |
| 230 | 230 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime); |
| 231 | - if($this->logger) { |
|
| 231 | + if ($this->logger) { |
|
| 232 | 232 | $this->logger->alert($msg); |
| 233 | 233 | } |
| 234 | 234 | $output->writeln(PHP_EOL . $msg); |
@@ -240,14 +240,14 @@ discard block |
||
| 240 | 240 | $job['error'] = $e->getMessage(); |
| 241 | 241 | |
| 242 | 242 | |
| 243 | - if($this->logger) { |
|
| 243 | + if ($this->logger) { |
|
| 244 | 244 | $this->logger->error($e, ['command' => $cmd]); |
| 245 | 245 | } |
| 246 | 246 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
| 247 | 247 | |
| 248 | 248 | } finally { |
| 249 | 249 | |
| 250 | - if(!$execTime) { |
|
| 250 | + if (!$execTime) { |
|
| 251 | 251 | $execTime = microtime(true) - $timeStart; |
| 252 | 252 | } |
| 253 | 253 | $job['last_exec'] = time(); |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $this->updateJob($cmd, $job); |
| 258 | 258 | |
| 259 | 259 | $workTime += $execTime; |
| 260 | - if($workTime * 2 > $allTimeout) { |
|
| 260 | + if ($workTime * 2 > $allTimeout) { |
|
| 261 | 261 | break; |
| 262 | 262 | } |
| 263 | 263 | /* |
@@ -271,8 +271,8 @@ discard block |
||
| 271 | 271 | |
| 272 | 272 | protected function isActualJob(&$job) { |
| 273 | 273 | |
| 274 | - if($job['status'] == self::EXEC_STATUS_WORK) { |
|
| 275 | - if($job['start_time'] && $job['start_time'] < (time() - self::RESTART_TIME)) { |
|
| 274 | + if ($job['status'] == self::EXEC_STATUS_WORK) { |
|
| 275 | + if ($job['start_time'] && $job['start_time'] < (time() - self::RESTART_TIME)) { |
|
| 276 | 276 | return true; |
| 277 | 277 | } |
| 278 | 278 | return false; |
@@ -280,15 +280,15 @@ discard block |
||
| 280 | 280 | |
| 281 | 281 | $period = intval($job['period']); |
| 282 | 282 | |
| 283 | - if($period > 0) { |
|
| 284 | - if($period < $this->minAgentPeriod) { |
|
| 283 | + if ($period > 0) { |
|
| 284 | + if ($period < $this->minAgentPeriod) { |
|
| 285 | 285 | $job['orig_period'] = $period; |
| 286 | 286 | $period = $job['period'] = $this->minAgentPeriod; |
| 287 | 287 | } |
| 288 | - if(time() - $job['last_exec'] >= $period) { |
|
| 288 | + if (time() - $job['last_exec'] >= $period) { |
|
| 289 | 289 | return true; |
| 290 | 290 | } |
| 291 | - } else if(!empty($job['times'])) { |
|
| 291 | + } else if (!empty($job['times'])) { |
|
| 292 | 292 | //TODO: |
| 293 | 293 | } |
| 294 | 294 | |
@@ -298,7 +298,7 @@ discard block |
||
| 298 | 298 | public function getCronJobs() { |
| 299 | 299 | |
| 300 | 300 | $crontab = $this->getCronTab(); |
| 301 | - if($crontab === false) { |
|
| 301 | + if ($crontab === false) { |
|
| 302 | 302 | return false; |
| 303 | 303 | } |
| 304 | 304 | |
@@ -308,9 +308,9 @@ discard block |
||
| 308 | 308 | $commands = $app->all(); |
| 309 | 309 | |
| 310 | 310 | $selfCommands = []; |
| 311 | - foreach($commands as $command) { |
|
| 311 | + foreach ($commands as $command) { |
|
| 312 | 312 | /** @var BxCommand $command */ |
| 313 | - if($command instanceof BxCommand) { |
|
| 313 | + if ($command instanceof BxCommand) { |
|
| 314 | 314 | $name = $command->getName(); |
| 315 | 315 | $selfCommands[$name] = [ |
| 316 | 316 | 'object' => $command, |
@@ -320,19 +320,19 @@ discard block |
||
| 320 | 320 | |
| 321 | 321 | $agents = []; |
| 322 | 322 | $reader = new AnnotationReader(); |
| 323 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 323 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 324 | 324 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 325 | 325 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 326 | 326 | |
| 327 | - foreach($annotations as $annotation) { |
|
| 328 | - if($annotation instanceof Agent) { |
|
| 327 | + foreach ($annotations as $annotation) { |
|
| 328 | + if ($annotation instanceof Agent) { |
|
| 329 | 329 | $agents[$cmd] = $annotation->toArray(); |
| 330 | 330 | } |
| 331 | 331 | } |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - foreach($crontab as $cmd => $job) { |
|
| 335 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 334 | + foreach ($crontab as $cmd => $job) { |
|
| 335 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 336 | 336 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 337 | 337 | } |
| 338 | 338 | } |
@@ -351,7 +351,7 @@ discard block |
||
| 351 | 351 | |
| 352 | 352 | $crontab = $this->getCronTab(); |
| 353 | 353 | |
| 354 | - if($crontab === false) { |
|
| 354 | + if ($crontab === false) { |
|
| 355 | 355 | return false; |
| 356 | 356 | } else { |
| 357 | 357 | $crontab = array_merge($crontab, $changedAgents); |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | $fh = fopen($filename, 'c'); |
| 370 | 370 | if (flock($fh, LOCK_EX)) { |
| 371 | 371 | ftruncate($fh, 0); |
| 372 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 372 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 373 | 373 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 374 | 374 | } |
| 375 | 375 | } else { |
@@ -389,16 +389,16 @@ discard block |
||
| 389 | 389 | $filename = EnvHelper::getCrontabFile(); |
| 390 | 390 | |
| 391 | 391 | $fh = fopen($filename, 'r'); |
| 392 | - if(!$fh) { |
|
| 392 | + if (!$fh) { |
|
| 393 | 393 | return false; |
| 394 | 394 | } |
| 395 | - if(flock($fh, LOCK_SH)) { |
|
| 395 | + if (flock($fh, LOCK_SH)) { |
|
| 396 | 396 | $cronTab = []; |
| 397 | 397 | clearstatcache(); |
| 398 | 398 | $filesize = (int) filesize($filename); |
| 399 | - if($filesize > 0 && $data = fread($fh, $filesize)) { |
|
| 399 | + if ($filesize > 0 && $data = fread($fh, $filesize)) { |
|
| 400 | 400 | $decoded = json_decode($data, true); |
| 401 | - if(is_array($decoded)) { |
|
| 401 | + if (is_array($decoded)) { |
|
| 402 | 402 | $cronTab = $decoded; |
| 403 | 403 | } else { |
| 404 | 404 | throw new \Exception("Unable to parse cronTab"); |
@@ -415,14 +415,14 @@ discard block |
||
| 415 | 415 | |
| 416 | 416 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
| 417 | 417 | |
| 418 | - if($sort == self::SORT_TIME) { |
|
| 418 | + if ($sort == self::SORT_TIME) { |
|
| 419 | 419 | $sorting = []; |
| 420 | - foreach($crontab as $cmd => $data) { |
|
| 420 | + foreach ($crontab as $cmd => $data) { |
|
| 421 | 421 | $sorting[$cmd] = $data['last_exec']; |
| 422 | 422 | } |
| 423 | 423 | arsort($sorting, SORT_NUMERIC); |
| 424 | 424 | $sorted = []; |
| 425 | - foreach($sorting as $cmd => $time) { |
|
| 425 | + foreach ($sorting as $cmd => $time) { |
|
| 426 | 426 | $sorted[$cmd] = $crontab[$cmd]; |
| 427 | 427 | } |
| 428 | 428 | $crontab = $sorted; |