@@ -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]); |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | |
| 181 | 181 | $jobs = $this->getCronJobs(); |
| 182 | 182 | |
| 183 | - if(is_array($jobs) && !empty($jobs)) { |
|
| 183 | + if (is_array($jobs) && !empty($jobs)) { |
|
| 184 | 184 | |
| 185 | 185 | /* |
| 186 | 186 | * Минимально допустимый период выполнения одной задачи |
@@ -188,9 +188,9 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
| 190 | 190 | |
| 191 | - foreach($jobs as $cmd => $job) { |
|
| 191 | + foreach ($jobs as $cmd => $job) { |
|
| 192 | 192 | |
| 193 | - if($this->isActualJob($job)) { |
|
| 193 | + if ($this->isActualJob($job)) { |
|
| 194 | 194 | |
| 195 | 195 | $job['status'] = self::EXEC_STATUS_WORK; |
| 196 | 196 | $this->updaateJob($cmd, $job); |
@@ -202,12 +202,12 @@ discard block |
||
| 202 | 202 | $timeStart = microtime(true); |
| 203 | 203 | $returnCode = $command->run($cmdInput, $output); |
| 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, microtime(true) - $timeStart); |
| 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, microtime(true) - $timeStart); |
| 221 | - if($this->logger) { |
|
| 221 | + if ($this->logger) { |
|
| 222 | 222 | $this->logger->alert($msg); |
| 223 | 223 | } |
| 224 | 224 | $output->writeln(PHP_EOL . $msg); |
@@ -230,7 +230,7 @@ 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()); |
@@ -252,21 +252,21 @@ discard block |
||
| 252 | 252 | |
| 253 | 253 | protected function isActualJob(&$job) { |
| 254 | 254 | |
| 255 | - if($job['status'] == self::EXEC_STATUS_WORK) { |
|
| 255 | + if ($job['status'] == self::EXEC_STATUS_WORK) { |
|
| 256 | 256 | return false; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | $period = intval($job['period']); |
| 260 | 260 | |
| 261 | - if($period > 0) { |
|
| 262 | - if($period < $this->minAgentPeriod) { |
|
| 261 | + if ($period > 0) { |
|
| 262 | + if ($period < $this->minAgentPeriod) { |
|
| 263 | 263 | $job['orig_period'] = $period; |
| 264 | 264 | $period = $job['period'] = $this->minAgentPeriod; |
| 265 | 265 | } |
| 266 | - if(time() - $job['last_exec'] >= $period) { |
|
| 266 | + if (time() - $job['last_exec'] >= $period) { |
|
| 267 | 267 | return true; |
| 268 | 268 | } |
| 269 | - } else if(!empty($job['times'])) { |
|
| 269 | + } else if (!empty($job['times'])) { |
|
| 270 | 270 | //TODO: |
| 271 | 271 | } |
| 272 | 272 | |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | public function getCronJobs() { |
| 277 | 277 | |
| 278 | 278 | $crontab = $this->getCronTab(); |
| 279 | - if($crontab === false) { |
|
| 279 | + if ($crontab === false) { |
|
| 280 | 280 | return false; |
| 281 | 281 | } |
| 282 | 282 | |
@@ -286,9 +286,9 @@ discard block |
||
| 286 | 286 | $commands = $app->all(); |
| 287 | 287 | |
| 288 | 288 | $selfCommands = []; |
| 289 | - foreach($commands as $command) { |
|
| 289 | + foreach ($commands as $command) { |
|
| 290 | 290 | /** @var BxCommand $command */ |
| 291 | - if($command instanceof BxCommand) { |
|
| 291 | + if ($command instanceof BxCommand) { |
|
| 292 | 292 | $name = $command->getName(); |
| 293 | 293 | $selfCommands[$name] = [ |
| 294 | 294 | 'object' => $command, |
@@ -298,19 +298,19 @@ discard block |
||
| 298 | 298 | |
| 299 | 299 | $agents = []; |
| 300 | 300 | $reader = new AnnotationReader(); |
| 301 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 301 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 302 | 302 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 303 | 303 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 304 | 304 | |
| 305 | - foreach($annotations as $annotation) { |
|
| 306 | - if($annotation instanceof Agent) { |
|
| 305 | + foreach ($annotations as $annotation) { |
|
| 306 | + if ($annotation instanceof Agent) { |
|
| 307 | 307 | $agents[$cmd] = $annotation->toArray(); |
| 308 | 308 | } |
| 309 | 309 | } |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - foreach($crontab as $cmd => $job) { |
|
| 313 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 312 | + foreach ($crontab as $cmd => $job) { |
|
| 313 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 314 | 314 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 315 | 315 | } |
| 316 | 316 | } |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | |
| 330 | 330 | $crontab = $this->getCronTab(); |
| 331 | 331 | |
| 332 | - if($crontab === false) { |
|
| 332 | + if ($crontab === false) { |
|
| 333 | 333 | return false; |
| 334 | 334 | } else { |
| 335 | 335 | $crontab = array_merge($crontab, $changedAgents); |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | $fh = fopen($filename, 'c'); |
| 348 | 348 | if (flock($fh, LOCK_EX)) { |
| 349 | 349 | ftruncate($fh, 0); |
| 350 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 350 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 351 | 351 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 352 | 352 | } |
| 353 | 353 | } else { |
@@ -367,15 +367,15 @@ discard block |
||
| 367 | 367 | $filename = EnvHelper::getCrontabFile(); |
| 368 | 368 | |
| 369 | 369 | $fh = fopen($filename, 'r'); |
| 370 | - if(!$fh) { |
|
| 370 | + if (!$fh) { |
|
| 371 | 371 | return false; |
| 372 | 372 | } |
| 373 | - if(flock($fh, LOCK_SH)) { |
|
| 373 | + if (flock($fh, LOCK_SH)) { |
|
| 374 | 374 | $cronTab = []; |
| 375 | 375 | $filesize = (int) filesize($filename); |
| 376 | - if($filesize > 0 && $data = fread($fh, $filesize)) { |
|
| 376 | + if ($filesize > 0 && $data = fread($fh, $filesize)) { |
|
| 377 | 377 | $decoded = json_decode($data, true); |
| 378 | - if(is_array($decoded)) { |
|
| 378 | + if (is_array($decoded)) { |
|
| 379 | 379 | $cronTab = $decoded; |
| 380 | 380 | } |
| 381 | 381 | } |
@@ -390,14 +390,14 @@ discard block |
||
| 390 | 390 | |
| 391 | 391 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
| 392 | 392 | |
| 393 | - if($sort == self::SORT_TIME) { |
|
| 393 | + if ($sort == self::SORT_TIME) { |
|
| 394 | 394 | $sorting = []; |
| 395 | - foreach($crontab as $cmd => $data) { |
|
| 395 | + foreach ($crontab as $cmd => $data) { |
|
| 396 | 396 | $sorting[$cmd] = $data['last_exec']; |
| 397 | 397 | } |
| 398 | 398 | arsort($sorting, SORT_NUMERIC); |
| 399 | 399 | $sorted = []; |
| 400 | - foreach($sorting as $cmd => $time) { |
|
| 400 | + foreach ($sorting as $cmd => $time) { |
|
| 401 | 401 | $sorted[$cmd] = $crontab[$cmd]; |
| 402 | 402 | } |
| 403 | 403 | $crontab = $sorted; |