@@ -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,7 +182,7 @@ discard block |
||
| 182 | 182 | $allTimeout = EnvHelper::getCrontabTimeout(); |
| 183 | 183 | $workTime = 0; |
| 184 | 184 | |
| 185 | - if(is_array($jobs) && !empty($jobs)) { |
|
| 185 | + if (is_array($jobs) && !empty($jobs)) { |
|
| 186 | 186 | |
| 187 | 187 | /* |
| 188 | 188 | * Минимально допустимый период выполнения одной задачи |
@@ -191,9 +191,9 @@ discard block |
||
| 191 | 191 | $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
| 192 | 192 | $this->logger->alert(sprintf("Minimal agent period: %d", $this->minAgentPeriod)); |
| 193 | 193 | |
| 194 | - foreach($jobs as $cmd => $job) { |
|
| 194 | + foreach ($jobs as $cmd => $job) { |
|
| 195 | 195 | |
| 196 | - if($this->isActualJob($job)) { |
|
| 196 | + if ($this->isActualJob($job)) { |
|
| 197 | 197 | |
| 198 | 198 | $job['status'] = self::EXEC_STATUS_WORK; |
| 199 | 199 | $this->updateJob($cmd, $job); |
@@ -207,12 +207,12 @@ discard block |
||
| 207 | 207 | $returnCode = $command->run($cmdInput, $output); |
| 208 | 208 | $execTime = microtime(true) - $timeStart; |
| 209 | 209 | |
| 210 | - if(!$returnCode) { |
|
| 210 | + if (!$returnCode) { |
|
| 211 | 211 | |
| 212 | 212 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
| 213 | 213 | |
| 214 | 214 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime); |
| 215 | - if($this->logger) { |
|
| 215 | + if ($this->logger) { |
|
| 216 | 216 | $this->logger->alert($msg); |
| 217 | 217 | } |
| 218 | 218 | $output->writeln(PHP_EOL . $msg); |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | $job['error_code'] = $returnCode; |
| 224 | 224 | |
| 225 | 225 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime); |
| 226 | - if($this->logger) { |
|
| 226 | + if ($this->logger) { |
|
| 227 | 227 | $this->logger->alert($msg); |
| 228 | 228 | } |
| 229 | 229 | $output->writeln(PHP_EOL . $msg); |
@@ -235,14 +235,14 @@ discard block |
||
| 235 | 235 | $job['error'] = $e->getMessage(); |
| 236 | 236 | |
| 237 | 237 | |
| 238 | - if($this->logger) { |
|
| 238 | + if ($this->logger) { |
|
| 239 | 239 | $this->logger->error($e, ['command' => $cmd]); |
| 240 | 240 | } |
| 241 | 241 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
| 242 | 242 | |
| 243 | 243 | } finally { |
| 244 | 244 | |
| 245 | - if(!$execTime) { |
|
| 245 | + if (!$execTime) { |
|
| 246 | 246 | $execTime = microtime(true) - $timeStart; |
| 247 | 247 | } |
| 248 | 248 | $job['last_exec'] = time(); |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | $this->updateJob($cmd, $job); |
| 253 | 253 | |
| 254 | 254 | $workTime += $execTime; |
| 255 | - if($workTime * 2 > $allTimeout) { |
|
| 255 | + if ($workTime * 2 > $allTimeout) { |
|
| 256 | 256 | break; |
| 257 | 257 | } |
| 258 | 258 | /* |
@@ -266,21 +266,21 @@ discard block |
||
| 266 | 266 | |
| 267 | 267 | protected function isActualJob(&$job) { |
| 268 | 268 | |
| 269 | - if($job['status'] == self::EXEC_STATUS_WORK) { |
|
| 269 | + if ($job['status'] == self::EXEC_STATUS_WORK) { |
|
| 270 | 270 | return false; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | $period = intval($job['period']); |
| 274 | 274 | |
| 275 | - if($period > 0) { |
|
| 276 | - if($period < $this->minAgentPeriod) { |
|
| 275 | + if ($period > 0) { |
|
| 276 | + if ($period < $this->minAgentPeriod) { |
|
| 277 | 277 | $job['orig_period'] = $period; |
| 278 | 278 | $period = $job['period'] = $this->minAgentPeriod; |
| 279 | 279 | } |
| 280 | - if(time() - $job['last_exec'] >= $period) { |
|
| 280 | + if (time() - $job['last_exec'] >= $period) { |
|
| 281 | 281 | return true; |
| 282 | 282 | } |
| 283 | - } else if(!empty($job['times'])) { |
|
| 283 | + } else if (!empty($job['times'])) { |
|
| 284 | 284 | //TODO: |
| 285 | 285 | } |
| 286 | 286 | |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | public function getCronJobs() { |
| 291 | 291 | |
| 292 | 292 | $crontab = $this->getCronTab(); |
| 293 | - if($crontab === false) { |
|
| 293 | + if ($crontab === false) { |
|
| 294 | 294 | return false; |
| 295 | 295 | } |
| 296 | 296 | |
@@ -300,9 +300,9 @@ discard block |
||
| 300 | 300 | $commands = $app->all(); |
| 301 | 301 | |
| 302 | 302 | $selfCommands = []; |
| 303 | - foreach($commands as $command) { |
|
| 303 | + foreach ($commands as $command) { |
|
| 304 | 304 | /** @var BxCommand $command */ |
| 305 | - if($command instanceof BxCommand) { |
|
| 305 | + if ($command instanceof BxCommand) { |
|
| 306 | 306 | $name = $command->getName(); |
| 307 | 307 | $selfCommands[$name] = [ |
| 308 | 308 | 'object' => $command, |
@@ -312,19 +312,19 @@ discard block |
||
| 312 | 312 | |
| 313 | 313 | $agents = []; |
| 314 | 314 | $reader = new AnnotationReader(); |
| 315 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 315 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 316 | 316 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 317 | 317 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 318 | 318 | |
| 319 | - foreach($annotations as $annotation) { |
|
| 320 | - if($annotation instanceof Agent) { |
|
| 319 | + foreach ($annotations as $annotation) { |
|
| 320 | + if ($annotation instanceof Agent) { |
|
| 321 | 321 | $agents[$cmd] = $annotation->toArray(); |
| 322 | 322 | } |
| 323 | 323 | } |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | - foreach($crontab as $cmd => $job) { |
|
| 327 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 326 | + foreach ($crontab as $cmd => $job) { |
|
| 327 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 328 | 328 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 329 | 329 | } |
| 330 | 330 | } |
@@ -343,7 +343,7 @@ discard block |
||
| 343 | 343 | |
| 344 | 344 | $crontab = $this->getCronTab(); |
| 345 | 345 | |
| 346 | - if($crontab === false) { |
|
| 346 | + if ($crontab === false) { |
|
| 347 | 347 | return false; |
| 348 | 348 | } else { |
| 349 | 349 | $crontab = array_merge($crontab, $changedAgents); |
@@ -361,7 +361,7 @@ discard block |
||
| 361 | 361 | $fh = fopen($filename, 'c'); |
| 362 | 362 | if (flock($fh, LOCK_EX)) { |
| 363 | 363 | ftruncate($fh, 0); |
| 364 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 364 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 365 | 365 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 366 | 366 | } |
| 367 | 367 | } else { |
@@ -381,16 +381,16 @@ discard block |
||
| 381 | 381 | $filename = EnvHelper::getCrontabFile(); |
| 382 | 382 | |
| 383 | 383 | $fh = fopen($filename, 'r'); |
| 384 | - if(!$fh) { |
|
| 384 | + if (!$fh) { |
|
| 385 | 385 | return false; |
| 386 | 386 | } |
| 387 | - if(flock($fh, LOCK_SH)) { |
|
| 387 | + if (flock($fh, LOCK_SH)) { |
|
| 388 | 388 | $cronTab = []; |
| 389 | 389 | clearstatcache(); |
| 390 | 390 | $filesize = (int) filesize($filename); |
| 391 | - if($filesize > 0 && $data = fread($fh, $filesize)) { |
|
| 391 | + if ($filesize > 0 && $data = fread($fh, $filesize)) { |
|
| 392 | 392 | $decoded = json_decode($data, true); |
| 393 | - if(is_array($decoded)) { |
|
| 393 | + if (is_array($decoded)) { |
|
| 394 | 394 | $cronTab = $decoded; |
| 395 | 395 | } else { |
| 396 | 396 | throw new \Exception("Unable to parse cronTab"); |
@@ -407,14 +407,14 @@ discard block |
||
| 407 | 407 | |
| 408 | 408 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
| 409 | 409 | |
| 410 | - if($sort == self::SORT_TIME) { |
|
| 410 | + if ($sort == self::SORT_TIME) { |
|
| 411 | 411 | $sorting = []; |
| 412 | - foreach($crontab as $cmd => $data) { |
|
| 412 | + foreach ($crontab as $cmd => $data) { |
|
| 413 | 413 | $sorting[$cmd] = $data['last_exec']; |
| 414 | 414 | } |
| 415 | 415 | arsort($sorting, SORT_NUMERIC); |
| 416 | 416 | $sorted = []; |
| 417 | - foreach($sorting as $cmd => $time) { |
|
| 417 | + foreach ($sorting as $cmd => $time) { |
|
| 418 | 418 | $sorted[$cmd] = $crontab[$cmd]; |
| 419 | 419 | } |
| 420 | 420 | $crontab = $sorted; |