@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | protected function execute(InputInterface $input, OutputInterface $output) |
| 39 | 39 | { |
| 40 | 40 | $logger = EnvHelper::getLogger('bx_cron'); |
| 41 | - if($logger) { |
|
| 41 | + if ($logger) { |
|
| 42 | 42 | $this->setLogger($logger); |
| 43 | 43 | } |
| 44 | 44 | |
@@ -50,20 +50,20 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | $this->minAgentPeriod = (count($jobs) + 1) * self::BX_CRON_PERIOD; |
| 52 | 52 | |
| 53 | - if(!empty($jobs)) { |
|
| 53 | + if (!empty($jobs)) { |
|
| 54 | 54 | |
| 55 | 55 | $lockStore = new FlockStore(pathinfo(EnvHelper::getCrontabFile(), PATHINFO_DIRNAME)); |
| 56 | 56 | $lockFactory = new LockFactory($lockStore); |
| 57 | - if($this->logger) { |
|
| 57 | + if ($this->logger) { |
|
| 58 | 58 | $lockFactory->setLogger($this->logger); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - foreach($jobs as $cmd => $job) { |
|
| 61 | + foreach ($jobs as $cmd => $job) { |
|
| 62 | 62 | |
| 63 | - if($this->isActualJob($job)) { |
|
| 63 | + if ($this->isActualJob($job)) { |
|
| 64 | 64 | |
| 65 | 65 | $lock = $lockFactory->createLock($this->getLockName($cmd), self::EXEC_TIMEOUT); |
| 66 | - if($lock->acquire()) { |
|
| 66 | + if ($lock->acquire()) { |
|
| 67 | 67 | |
| 68 | 68 | $job['status'] = self::EXEC_STATUS_WORK; |
| 69 | 69 | $this->updaateJob($cmd, $job); |
@@ -75,12 +75,12 @@ discard block |
||
| 75 | 75 | $timeStart = microtime(true); |
| 76 | 76 | $returnCode = $command->run($cmdInput, $output); |
| 77 | 77 | |
| 78 | - if(!$returnCode) { |
|
| 78 | + if (!$returnCode) { |
|
| 79 | 79 | |
| 80 | 80 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
| 81 | 81 | |
| 82 | 82 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 83 | - if($this->logger) { |
|
| 83 | + if ($this->logger) { |
|
| 84 | 84 | $this->logger->alert($msg); |
| 85 | 85 | } |
| 86 | 86 | $output->writeln(PHP_EOL . $msg); |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | $job['error_code'] = $returnCode; |
| 92 | 92 | |
| 93 | 93 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 94 | - if($this->logger) { |
|
| 94 | + if ($this->logger) { |
|
| 95 | 95 | $this->logger->alert($msg); |
| 96 | 96 | } |
| 97 | 97 | $output->writeln(PHP_EOL . $msg); |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $job['error'] = $e->getMessage(); |
| 104 | 104 | |
| 105 | 105 | |
| 106 | - if($this->logger) { |
|
| 106 | + if ($this->logger) { |
|
| 107 | 107 | $this->logger->error($e, ['command' => $cmd]); |
| 108 | 108 | } |
| 109 | 109 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | break; |
| 124 | 124 | |
| 125 | 125 | } else { |
| 126 | - if($this->logger) { |
|
| 126 | + if ($this->logger) { |
|
| 127 | 127 | $this->logger->warning($cmd . " is locked"); |
| 128 | 128 | } |
| 129 | 129 | } |
@@ -139,21 +139,21 @@ discard block |
||
| 139 | 139 | |
| 140 | 140 | protected function isActualJob(&$job) { |
| 141 | 141 | |
| 142 | - if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 142 | + if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 143 | 143 | return false; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | $period = intval($job['period']); |
| 147 | 147 | |
| 148 | - if($period > 0) { |
|
| 149 | - if($period < $this->minAgentPeriod) { |
|
| 148 | + if ($period > 0) { |
|
| 149 | + if ($period < $this->minAgentPeriod) { |
|
| 150 | 150 | $job['orig_period'] = $period; |
| 151 | 151 | $period = $job['period'] = $this->minAgentPeriod; |
| 152 | 152 | } |
| 153 | - if(time() - $job['last_exec'] >= $period) { |
|
| 153 | + if (time() - $job['last_exec'] >= $period) { |
|
| 154 | 154 | return true; |
| 155 | 155 | } |
| 156 | - } else if(!empty($job['times'])) { |
|
| 156 | + } else if (!empty($job['times'])) { |
|
| 157 | 157 | //TODO: |
| 158 | 158 | } |
| 159 | 159 | |
@@ -168,9 +168,9 @@ discard block |
||
| 168 | 168 | $commands = $app->all(); |
| 169 | 169 | |
| 170 | 170 | $selfCommands = []; |
| 171 | - foreach($commands as $command) { |
|
| 171 | + foreach ($commands as $command) { |
|
| 172 | 172 | /** @var BxCommand $command */ |
| 173 | - if($command instanceof BxCommand) { |
|
| 173 | + if ($command instanceof BxCommand) { |
|
| 174 | 174 | $name = $command->getName(); |
| 175 | 175 | $selfCommands[$name] = [ |
| 176 | 176 | 'object' => $command, |
@@ -180,12 +180,12 @@ discard block |
||
| 180 | 180 | |
| 181 | 181 | $agents = []; |
| 182 | 182 | $reader = new AnnotationReader(); |
| 183 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 183 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 184 | 184 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 185 | 185 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 186 | 186 | |
| 187 | - foreach($annotations as $annotation) { |
|
| 188 | - if($annotation instanceof Agent) { |
|
| 187 | + foreach ($annotations as $annotation) { |
|
| 188 | + if ($annotation instanceof Agent) { |
|
| 189 | 189 | $agents[$cmd] = $annotation->toArray(); |
| 190 | 190 | } |
| 191 | 191 | } |
@@ -193,9 +193,9 @@ discard block |
||
| 193 | 193 | |
| 194 | 194 | $crontab = $this->getCronTab(); |
| 195 | 195 | |
| 196 | - if(is_array($crontab)) { |
|
| 197 | - foreach($crontab as $cmd => $job) { |
|
| 198 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 196 | + if (is_array($crontab)) { |
|
| 197 | + foreach ($crontab as $cmd => $job) { |
|
| 198 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 199 | 199 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 200 | 200 | } |
| 201 | 201 | } |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | $fh = fopen($filename, 'c'); |
| 229 | 229 | if (flock($fh, LOCK_EX)) { |
| 230 | 230 | ftruncate($fh, 0); |
| 231 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 231 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 232 | 232 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 233 | 233 | } |
| 234 | 234 | } |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $filename = EnvHelper::getCrontabFile(); |
| 247 | 247 | |
| 248 | 248 | $fh = fopen($filename, 'r'); |
| 249 | - if(flock($fh, LOCK_SH)) { |
|
| 249 | + if (flock($fh, LOCK_SH)) { |
|
| 250 | 250 | $data = @fread($fh, filesize($filename)); |
| 251 | 251 | $cronTab = json_decode($data, true); |
| 252 | 252 | } |