@@ -13,10 +13,10 @@ discard block |
||
| 13 | 13 | public static function loadEnv() { |
| 14 | 14 | |
| 15 | 15 | $envFile = realpath(__DIR__ . '/../../../../.env'); |
| 16 | - if(!is_file($envFile)) { |
|
| 16 | + if (!is_file($envFile)) { |
|
| 17 | 17 | $envFile = realpath(__DIR__ . '/../../../../../.env'); |
| 18 | 18 | } |
| 19 | - if(is_file($envFile)) { |
|
| 19 | + if (is_file($envFile)) { |
|
| 20 | 20 | try { |
| 21 | 21 | $env = new \Symfony\Component\Dotenv\Dotenv(); |
| 22 | 22 | $env->load($envFile); |
@@ -33,15 +33,15 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | $_SERVER['DOCUMENT_ROOT'] = realpath(__DIR__ . '/../../../../'); |
| 35 | 35 | |
| 36 | - if(isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) { |
|
| 36 | + if (isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) { |
|
| 37 | 37 | $_SERVER['DOCUMENT_ROOT'] = $_ENV['APP_DOCUMENT_ROOT']; |
| 38 | 38 | return $_SERVER['DOCUMENT_ROOT']; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | $composerFile = realpath(__DIR__ . '/../../../../composer.json'); |
| 42 | - if(is_file($composerFile)) { |
|
| 42 | + if (is_file($composerFile)) { |
|
| 43 | 43 | $composerConfig = json_decode(file_get_contents($composerFile), true); |
| 44 | - if(isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) { |
|
| 44 | + if (isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) { |
|
| 45 | 45 | $_SERVER['DOCUMENT_ROOT'] = $composerConfig['extra']['document-root']; |
| 46 | 46 | return $_SERVER['DOCUMENT_ROOT']; |
| 47 | 47 | } |
@@ -56,10 +56,10 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public static function getLogger($channel) { |
| 58 | 58 | |
| 59 | - if(isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) { |
|
| 59 | + if (isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) { |
|
| 60 | 60 | $logClass = $_ENV['APP_LOG_CLASS']; |
| 61 | 61 | $log = new $logClass($channel); |
| 62 | - if($log instanceof LoggerInterface) { |
|
| 62 | + if ($log instanceof LoggerInterface) { |
|
| 63 | 63 | return $log; |
| 64 | 64 | } |
| 65 | 65 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | public static function getCrontabFile() { |
| 74 | 74 | |
| 75 | - if(isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) { |
|
| 75 | + if (isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) { |
|
| 76 | 76 | return rtrim($_ENV['BX_CRONTAB_FOLDER'], "/") . '/bx_crontab.json'; |
| 77 | 77 | } |
| 78 | 78 | |
@@ -81,15 +81,15 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | public static function getSwitch($name, $state) { |
| 83 | 83 | |
| 84 | - if(isset($_ENV[$name])) { |
|
| 84 | + if (isset($_ENV[$name])) { |
|
| 85 | 85 | |
| 86 | 86 | $val = strtolower(trim($_ENV[$name])); |
| 87 | - if($state == self::SWITCH_STATE_ON) { |
|
| 88 | - if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') { |
|
| 87 | + if ($state == self::SWITCH_STATE_ON) { |
|
| 88 | + if ($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') { |
|
| 89 | 89 | return true; |
| 90 | 90 | } |
| 91 | - } else if($state == self::SWITCH_STATE_OFF) { |
|
| 92 | - if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') { |
|
| 91 | + } else if ($state == self::SWITCH_STATE_OFF) { |
|
| 92 | + if ($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') { |
|
| 93 | 93 | return true; |
| 94 | 94 | } |
| 95 | 95 | } |
@@ -38,12 +38,12 @@ 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 | |
| 45 | - if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 46 | - if($this->logger) { |
|
| 45 | + if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 46 | + if ($this->logger) { |
|
| 47 | 47 | $this->logger->alert('BxCron switch off'); |
| 48 | 48 | } |
| 49 | 49 | return 0; |
@@ -57,20 +57,20 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | $this->minAgentPeriod = (count($jobs) + 1) * self::BX_CRON_PERIOD; |
| 59 | 59 | |
| 60 | - if(!empty($jobs)) { |
|
| 60 | + if (!empty($jobs)) { |
|
| 61 | 61 | |
| 62 | 62 | $lockStore = new FlockStore(pathinfo(EnvHelper::getCrontabFile(), PATHINFO_DIRNAME)); |
| 63 | 63 | $lockFactory = new LockFactory($lockStore); |
| 64 | - if($this->logger) { |
|
| 64 | + if ($this->logger) { |
|
| 65 | 65 | $lockFactory->setLogger($this->logger); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - foreach($jobs as $cmd => $job) { |
|
| 68 | + foreach ($jobs as $cmd => $job) { |
|
| 69 | 69 | |
| 70 | - if($this->isActualJob($job)) { |
|
| 70 | + if ($this->isActualJob($job)) { |
|
| 71 | 71 | |
| 72 | 72 | $lock = $lockFactory->createLock($this->getLockName($cmd), self::EXEC_TIMEOUT); |
| 73 | - if($lock->acquire()) { |
|
| 73 | + if ($lock->acquire()) { |
|
| 74 | 74 | |
| 75 | 75 | $job['status'] = self::EXEC_STATUS_WORK; |
| 76 | 76 | $this->updaateJob($cmd, $job); |
@@ -82,12 +82,12 @@ discard block |
||
| 82 | 82 | $timeStart = microtime(true); |
| 83 | 83 | $returnCode = $command->run($cmdInput, $output); |
| 84 | 84 | |
| 85 | - if(!$returnCode) { |
|
| 85 | + if (!$returnCode) { |
|
| 86 | 86 | |
| 87 | 87 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
| 88 | 88 | |
| 89 | 89 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 90 | - if($this->logger) { |
|
| 90 | + if ($this->logger) { |
|
| 91 | 91 | $this->logger->alert($msg); |
| 92 | 92 | } |
| 93 | 93 | $output->writeln(PHP_EOL . $msg); |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $job['error_code'] = $returnCode; |
| 99 | 99 | |
| 100 | 100 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 101 | - if($this->logger) { |
|
| 101 | + if ($this->logger) { |
|
| 102 | 102 | $this->logger->alert($msg); |
| 103 | 103 | } |
| 104 | 104 | $output->writeln(PHP_EOL . $msg); |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | $job['error'] = $e->getMessage(); |
| 111 | 111 | |
| 112 | 112 | |
| 113 | - if($this->logger) { |
|
| 113 | + if ($this->logger) { |
|
| 114 | 114 | $this->logger->error($e, ['command' => $cmd]); |
| 115 | 115 | } |
| 116 | 116 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | break; |
| 131 | 131 | |
| 132 | 132 | } else { |
| 133 | - if($this->logger) { |
|
| 133 | + if ($this->logger) { |
|
| 134 | 134 | $this->logger->warning($cmd . " is locked"); |
| 135 | 135 | } |
| 136 | 136 | } |
@@ -146,21 +146,21 @@ discard block |
||
| 146 | 146 | |
| 147 | 147 | protected function isActualJob(&$job) { |
| 148 | 148 | |
| 149 | - if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 149 | + if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 150 | 150 | return false; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | $period = intval($job['period']); |
| 154 | 154 | |
| 155 | - if($period > 0) { |
|
| 156 | - if($period < $this->minAgentPeriod) { |
|
| 155 | + if ($period > 0) { |
|
| 156 | + if ($period < $this->minAgentPeriod) { |
|
| 157 | 157 | $job['orig_period'] = $period; |
| 158 | 158 | $period = $job['period'] = $this->minAgentPeriod; |
| 159 | 159 | } |
| 160 | - if(time() - $job['last_exec'] >= $period) { |
|
| 160 | + if (time() - $job['last_exec'] >= $period) { |
|
| 161 | 161 | return true; |
| 162 | 162 | } |
| 163 | - } else if(!empty($job['times'])) { |
|
| 163 | + } else if (!empty($job['times'])) { |
|
| 164 | 164 | //TODO: |
| 165 | 165 | } |
| 166 | 166 | |
@@ -175,9 +175,9 @@ discard block |
||
| 175 | 175 | $commands = $app->all(); |
| 176 | 176 | |
| 177 | 177 | $selfCommands = []; |
| 178 | - foreach($commands as $command) { |
|
| 178 | + foreach ($commands as $command) { |
|
| 179 | 179 | /** @var BxCommand $command */ |
| 180 | - if($command instanceof BxCommand) { |
|
| 180 | + if ($command instanceof BxCommand) { |
|
| 181 | 181 | $name = $command->getName(); |
| 182 | 182 | $selfCommands[$name] = [ |
| 183 | 183 | 'object' => $command, |
@@ -187,12 +187,12 @@ discard block |
||
| 187 | 187 | |
| 188 | 188 | $agents = []; |
| 189 | 189 | $reader = new AnnotationReader(); |
| 190 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 190 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 191 | 191 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 192 | 192 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 193 | 193 | |
| 194 | - foreach($annotations as $annotation) { |
|
| 195 | - if($annotation instanceof Agent) { |
|
| 194 | + foreach ($annotations as $annotation) { |
|
| 195 | + if ($annotation instanceof Agent) { |
|
| 196 | 196 | $agents[$cmd] = $annotation->toArray(); |
| 197 | 197 | } |
| 198 | 198 | } |
@@ -200,9 +200,9 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | $crontab = $this->getCronTab(); |
| 202 | 202 | |
| 203 | - if(is_array($crontab)) { |
|
| 204 | - foreach($crontab as $cmd => $job) { |
|
| 205 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 203 | + if (is_array($crontab)) { |
|
| 204 | + foreach ($crontab as $cmd => $job) { |
|
| 205 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 206 | 206 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 207 | 207 | } |
| 208 | 208 | } |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | $fh = fopen($filename, 'c'); |
| 236 | 236 | if (flock($fh, LOCK_EX)) { |
| 237 | 237 | ftruncate($fh, 0); |
| 238 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 238 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 239 | 239 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 240 | 240 | } |
| 241 | 241 | } |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | $filename = EnvHelper::getCrontabFile(); |
| 254 | 254 | |
| 255 | 255 | $fh = fopen($filename, 'r'); |
| 256 | - if(flock($fh, LOCK_SH)) { |
|
| 256 | + if (flock($fh, LOCK_SH)) { |
|
| 257 | 257 | $data = @fread($fh, filesize($filename)); |
| 258 | 258 | $cronTab = json_decode($data, true); |
| 259 | 259 | } |