@@ -29,37 +29,37 @@ discard block |
||
| 29 | 29 | protected function execute(InputInterface $input, OutputInterface $output) |
| 30 | 30 | { |
| 31 | 31 | $logger = EnvHelper::getLogger('bx_cron'); |
| 32 | - if($logger) { |
|
| 32 | + if ($logger) { |
|
| 33 | 33 | $this->setLogger($logger); |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 37 | - if($this->logger) { |
|
| 36 | + if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 37 | + if ($this->logger) { |
|
| 38 | 38 | $this->logger->alert('BxCron switch off'); |
| 39 | 39 | } |
| 40 | 40 | return 0; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - if(!$this->lock()) { |
|
| 43 | + if (!$this->lock()) { |
|
| 44 | 44 | $msg = 'The command is already running in another process.'; |
| 45 | 45 | $output->writeln($msg); |
| 46 | - if($this->logger) { |
|
| 46 | + if ($this->logger) { |
|
| 47 | 47 | $this->logger->warning($msg); |
| 48 | 48 | } |
| 49 | 49 | return 0; |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - if($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
| 52 | + if ($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
| 53 | 53 | $msg = sprintf("Sleep in interval %s", $sleepInterval); |
| 54 | 54 | $output->writeln($msg); |
| 55 | - if($this->logger) { |
|
| 55 | + if ($this->logger) { |
|
| 56 | 56 | $this->logger->warning($msg); |
| 57 | 57 | } |
| 58 | 58 | return 0; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | $clean = $input->getOption('clean'); |
| 62 | - if($clean) { |
|
| 62 | + if ($clean) { |
|
| 63 | 63 | $command = $this->getApplication()->find($clean); |
| 64 | 64 | $this->cleanJob($command->getName()); |
| 65 | 65 | $output->writeln($command->getName() . " will be executed now"); |
@@ -90,11 +90,11 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
| 92 | 92 | |
| 93 | - if(!empty($jobs)) { |
|
| 93 | + if (!empty($jobs)) { |
|
| 94 | 94 | |
| 95 | - foreach($jobs as $cmd => $job) { |
|
| 95 | + foreach ($jobs as $cmd => $job) { |
|
| 96 | 96 | |
| 97 | - if($this->isActualJob($job)) { |
|
| 97 | + if ($this->isActualJob($job)) { |
|
| 98 | 98 | |
| 99 | 99 | $job['status'] = self::EXEC_STATUS_WORK; |
| 100 | 100 | $this->updaateJob($cmd, $job); |
@@ -106,12 +106,12 @@ discard block |
||
| 106 | 106 | $timeStart = microtime(true); |
| 107 | 107 | $returnCode = $command->run($cmdInput, $output); |
| 108 | 108 | |
| 109 | - if(!$returnCode) { |
|
| 109 | + if (!$returnCode) { |
|
| 110 | 110 | |
| 111 | 111 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
| 112 | 112 | |
| 113 | 113 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 114 | - if($this->logger) { |
|
| 114 | + if ($this->logger) { |
|
| 115 | 115 | $this->logger->alert($msg); |
| 116 | 116 | } |
| 117 | 117 | $output->writeln(PHP_EOL . $msg); |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | $job['error_code'] = $returnCode; |
| 123 | 123 | |
| 124 | 124 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 125 | - if($this->logger) { |
|
| 125 | + if ($this->logger) { |
|
| 126 | 126 | $this->logger->alert($msg); |
| 127 | 127 | } |
| 128 | 128 | $output->writeln(PHP_EOL . $msg); |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | $job['error'] = $e->getMessage(); |
| 135 | 135 | |
| 136 | 136 | |
| 137 | - if($this->logger) { |
|
| 137 | + if ($this->logger) { |
|
| 138 | 138 | $this->logger->error($e, ['command' => $cmd]); |
| 139 | 139 | } |
| 140 | 140 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
@@ -158,21 +158,21 @@ discard block |
||
| 158 | 158 | |
| 159 | 159 | protected function isActualJob(&$job) { |
| 160 | 160 | |
| 161 | - if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 161 | + if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 162 | 162 | return false; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | $period = intval($job['period']); |
| 166 | 166 | |
| 167 | - if($period > 0) { |
|
| 168 | - if($period < $this->minAgentPeriod) { |
|
| 167 | + if ($period > 0) { |
|
| 168 | + if ($period < $this->minAgentPeriod) { |
|
| 169 | 169 | $job['orig_period'] = $period; |
| 170 | 170 | $period = $job['period'] = $this->minAgentPeriod; |
| 171 | 171 | } |
| 172 | - if(time() - $job['last_exec'] >= $period) { |
|
| 172 | + if (time() - $job['last_exec'] >= $period) { |
|
| 173 | 173 | return true; |
| 174 | 174 | } |
| 175 | - } else if(!empty($job['times'])) { |
|
| 175 | + } else if (!empty($job['times'])) { |
|
| 176 | 176 | //TODO: |
| 177 | 177 | } |
| 178 | 178 | |
@@ -187,9 +187,9 @@ discard block |
||
| 187 | 187 | $commands = $app->all(); |
| 188 | 188 | |
| 189 | 189 | $selfCommands = []; |
| 190 | - foreach($commands as $command) { |
|
| 190 | + foreach ($commands as $command) { |
|
| 191 | 191 | /** @var BxCommand $command */ |
| 192 | - if($command instanceof BxCommand) { |
|
| 192 | + if ($command instanceof BxCommand) { |
|
| 193 | 193 | $name = $command->getName(); |
| 194 | 194 | $selfCommands[$name] = [ |
| 195 | 195 | 'object' => $command, |
@@ -199,12 +199,12 @@ discard block |
||
| 199 | 199 | |
| 200 | 200 | $agents = []; |
| 201 | 201 | $reader = new AnnotationReader(); |
| 202 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 202 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 203 | 203 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 204 | 204 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 205 | 205 | |
| 206 | - foreach($annotations as $annotation) { |
|
| 207 | - if($annotation instanceof Agent) { |
|
| 206 | + foreach ($annotations as $annotation) { |
|
| 207 | + if ($annotation instanceof Agent) { |
|
| 208 | 208 | $agents[$cmd] = $annotation->toArray(); |
| 209 | 209 | } |
| 210 | 210 | } |
@@ -212,8 +212,8 @@ discard block |
||
| 212 | 212 | |
| 213 | 213 | $crontab = $this->getCronTab(); |
| 214 | 214 | |
| 215 | - foreach($crontab as $cmd => $job) { |
|
| 216 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 215 | + foreach ($crontab as $cmd => $job) { |
|
| 216 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 217 | 217 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 218 | 218 | } |
| 219 | 219 | } |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | $fh = fopen($filename, 'c'); |
| 246 | 246 | if (flock($fh, LOCK_EX)) { |
| 247 | 247 | ftruncate($fh, 0); |
| 248 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 248 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 249 | 249 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 250 | 250 | } |
| 251 | 251 | } |
@@ -263,10 +263,10 @@ discard block |
||
| 263 | 263 | $filename = EnvHelper::getCrontabFile(); |
| 264 | 264 | |
| 265 | 265 | $fh = fopen($filename, 'r'); |
| 266 | - if(flock($fh, LOCK_SH)) { |
|
| 267 | - if($data = @fread($fh, filesize($filename))) { |
|
| 266 | + if (flock($fh, LOCK_SH)) { |
|
| 267 | + if ($data = @fread($fh, filesize($filename))) { |
|
| 268 | 268 | $decoded = json_decode($data, true); |
| 269 | - if(is_array($decoded)) { |
|
| 269 | + if (is_array($decoded)) { |
|
| 270 | 270 | $cronTab = $decoded; |
| 271 | 271 | } |
| 272 | 272 | } |