@@ -18,10 +18,10 @@ discard block |
||
| 18 | 18 | public static function loadEnv() { |
| 19 | 19 | |
| 20 | 20 | $envFile = realpath(__DIR__ . '/../../../../.env'); |
| 21 | - if(!is_file($envFile)) { |
|
| 21 | + if (!is_file($envFile)) { |
|
| 22 | 22 | $envFile = realpath(__DIR__ . '/../../../../../.env'); |
| 23 | 23 | } |
| 24 | - if(is_file($envFile)) { |
|
| 24 | + if (is_file($envFile)) { |
|
| 25 | 25 | try { |
| 26 | 26 | $env = new \Symfony\Component\Dotenv\Dotenv(); |
| 27 | 27 | $env->load($envFile); |
@@ -36,19 +36,19 @@ discard block |
||
| 36 | 36 | */ |
| 37 | 37 | public static function getDocumentRoot() { |
| 38 | 38 | |
| 39 | - if(isset($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['DOCUMENT_ROOT'])) { |
|
| 39 | + if (isset($_SERVER['DOCUMENT_ROOT']) && !empty($_SERVER['DOCUMENT_ROOT'])) { |
|
| 40 | 40 | return $_SERVER['DOCUMENT_ROOT']; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - if(isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) { |
|
| 43 | + if (isset($_ENV['APP_DOCUMENT_ROOT']) && is_dir($_ENV['APP_DOCUMENT_ROOT'])) { |
|
| 44 | 44 | $_SERVER['DOCUMENT_ROOT'] = $_ENV['APP_DOCUMENT_ROOT']; |
| 45 | 45 | return $_SERVER['DOCUMENT_ROOT']; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | $composerFile = realpath(__DIR__ . '/../../../../composer.json'); |
| 49 | - if(is_file($composerFile)) { |
|
| 49 | + if (is_file($composerFile)) { |
|
| 50 | 50 | $composerConfig = json_decode(file_get_contents($composerFile), true); |
| 51 | - if(isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) { |
|
| 51 | + if (isset($composerConfig['extra']['document-root']) && is_dir($composerConfig['extra']['document-root'])) { |
|
| 52 | 52 | $_SERVER['DOCUMENT_ROOT'] = $composerConfig['extra']['document-root']; |
| 53 | 53 | return $_SERVER['DOCUMENT_ROOT']; |
| 54 | 54 | } |
@@ -65,10 +65,10 @@ discard block |
||
| 65 | 65 | */ |
| 66 | 66 | public static function getLogger($channel) { |
| 67 | 67 | |
| 68 | - if(isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) { |
|
| 68 | + if (isset($_ENV['APP_LOG_CLASS']) && class_exists($_ENV['APP_LOG_CLASS'])) { |
|
| 69 | 69 | $logClass = $_ENV['APP_LOG_CLASS']; |
| 70 | 70 | $log = new $logClass($channel); |
| 71 | - if($log instanceof LoggerInterface) { |
|
| 71 | + if ($log instanceof LoggerInterface) { |
|
| 72 | 72 | return $log; |
| 73 | 73 | } |
| 74 | 74 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | public static function getCrontabFile() { |
| 83 | 83 | |
| 84 | - if(isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) { |
|
| 84 | + if (isset($_ENV['BX_CRONTAB_FOLDER']) && $_ENV['BX_CRONTAB_FOLDER']) { |
|
| 85 | 85 | return rtrim($_ENV['BX_CRONTAB_FOLDER'], "/") . '/bx_crontab.json'; |
| 86 | 86 | } |
| 87 | 87 | |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | public static function getCrontabTimeout() { |
| 92 | 92 | |
| 93 | - if(isset($_ENV['BX_CRONTAB_TIMEOUT']) && is_numeric($_ENV['BX_CRONTAB_TIMEOUT'])) { |
|
| 93 | + if (isset($_ENV['BX_CRONTAB_TIMEOUT']) && is_numeric($_ENV['BX_CRONTAB_TIMEOUT'])) { |
|
| 94 | 94 | return (int) $_ENV['BX_CRONTAB_TIMEOUT']; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | |
| 100 | 100 | public static function getBxCrontabPeriod() { |
| 101 | 101 | |
| 102 | - if(isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) { |
|
| 102 | + if (isset($_ENV['BX_CRONTAB_PERIOD']) && is_numeric($_ENV['BX_CRONTAB_PERIOD'])) { |
|
| 103 | 103 | return (int) $_ENV['BX_CRONTAB_PERIOD']; |
| 104 | 104 | } |
| 105 | 105 | |
@@ -108,15 +108,15 @@ discard block |
||
| 108 | 108 | |
| 109 | 109 | public static function getSwitch($name, $state) { |
| 110 | 110 | |
| 111 | - if(isset($_ENV[$name])) { |
|
| 111 | + if (isset($_ENV[$name])) { |
|
| 112 | 112 | |
| 113 | 113 | $val = strtolower(trim($_ENV[$name])); |
| 114 | - if($state == self::SWITCH_STATE_ON) { |
|
| 115 | - if($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') { |
|
| 114 | + if ($state == self::SWITCH_STATE_ON) { |
|
| 115 | + if ($val === self::SWITCH_STATE_ON || $val === '1' || $val === 'true') { |
|
| 116 | 116 | return true; |
| 117 | 117 | } |
| 118 | - } else if($state == self::SWITCH_STATE_OFF) { |
|
| 119 | - if($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') { |
|
| 118 | + } else if ($state == self::SWITCH_STATE_OFF) { |
|
| 119 | + if ($val === self::SWITCH_STATE_OFF || $val === '0' || $val === 'false') { |
|
| 120 | 120 | return true; |
| 121 | 121 | } |
| 122 | 122 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | |
| 130 | 130 | $timeZone = self::BX_CRONTAB_TIMEZONE; |
| 131 | 131 | |
| 132 | - if(isset($_ENV['BX_CRONTAB_TIMEZONE']) && $_ENV['BX_CRONTAB_TIMEZONE']) { |
|
| 132 | + if (isset($_ENV['BX_CRONTAB_TIMEZONE']) && $_ENV['BX_CRONTAB_TIMEZONE']) { |
|
| 133 | 133 | $timeZone = trim($_ENV['BX_CRONTAB_TIMEZONE']); |
| 134 | 134 | } |
| 135 | 135 | |
@@ -138,17 +138,17 @@ discard block |
||
| 138 | 138 | |
| 139 | 139 | public static function checkSleepInterval() { |
| 140 | 140 | |
| 141 | - if(isset($_ENV['BX_CRONTAB_SLEEP_TIME']) && $_ENV['BX_CRONTAB_SLEEP_TIME']) { |
|
| 141 | + if (isset($_ENV['BX_CRONTAB_SLEEP_TIME']) && $_ENV['BX_CRONTAB_SLEEP_TIME']) { |
|
| 142 | 142 | $intervals = explode(',', $_ENV['BX_CRONTAB_SLEEP_TIME']); |
| 143 | - foreach($intervals as $interval) { |
|
| 143 | + foreach ($intervals as $interval) { |
|
| 144 | 144 | $times = explode('-', $interval); |
| 145 | - if(count($times) != 2) { |
|
| 145 | + if (count($times) != 2) { |
|
| 146 | 146 | continue; |
| 147 | 147 | } |
| 148 | 148 | $minTime = Time24::validateTimeString($times[0]); |
| 149 | 149 | $maxTime = Time24::validateTimeString($times[1]); |
| 150 | - if($minTime && $maxTime) { |
|
| 151 | - if(Time24::inInterval($minTime, $maxTime)) { |
|
| 150 | + if ($minTime && $maxTime) { |
|
| 151 | + if (Time24::inInterval($minTime, $maxTime)) { |
|
| 152 | 152 | return $interval; |
| 153 | 153 | } |
| 154 | 154 | } |
@@ -39,45 +39,45 @@ discard block |
||
| 39 | 39 | set_time_limit(EnvHelper::getCrontabTimeout()); |
| 40 | 40 | |
| 41 | 41 | $logger = EnvHelper::getLogger('bx_cron'); |
| 42 | - if($logger) { |
|
| 42 | + if ($logger) { |
|
| 43 | 43 | $this->setLogger($logger); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | $showStatus = $input->getOption('status'); |
| 47 | 47 | $byTime = $input->getOption('bytime'); |
| 48 | - if($showStatus) { |
|
| 48 | + if ($showStatus) { |
|
| 49 | 49 | $sort = ($byTime ? self::SORT_TIME : self::SORT_NAME); |
| 50 | 50 | $this->showStatus($output, $sort); |
| 51 | 51 | return 0; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - if(EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 55 | - if($this->logger) { |
|
| 54 | + if (EnvHelper::getSwitch('BX_CRONTAB_RUN', EnvHelper::SWITCH_STATE_OFF)) { |
|
| 55 | + if ($this->logger) { |
|
| 56 | 56 | $this->logger->alert('BxCron switch off'); |
| 57 | 57 | } |
| 58 | 58 | return 0; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - if(!$this->lock()) { |
|
| 61 | + if (!$this->lock()) { |
|
| 62 | 62 | $msg = 'The command is already running in another process.'; |
| 63 | 63 | $output->writeln($msg); |
| 64 | - if($this->logger) { |
|
| 64 | + if ($this->logger) { |
|
| 65 | 65 | $this->logger->warning($msg); |
| 66 | 66 | } |
| 67 | 67 | return 0; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - if($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
| 70 | + if ($sleepInterval = EnvHelper::checkSleepInterval()) { |
|
| 71 | 71 | $msg = sprintf("Sleep in interval %s", $sleepInterval); |
| 72 | 72 | $output->writeln($msg); |
| 73 | - if($this->logger) { |
|
| 73 | + if ($this->logger) { |
|
| 74 | 74 | $this->logger->warning($msg); |
| 75 | 75 | } |
| 76 | 76 | return 0; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | $clean = $input->getOption('clean'); |
| 80 | - if($clean) { |
|
| 80 | + if ($clean) { |
|
| 81 | 81 | $command = $this->getApplication()->find($clean); |
| 82 | 82 | $this->cleanJob($command->getName()); |
| 83 | 83 | $output->writeln($command->getName() . " will be executed now"); |
@@ -101,10 +101,10 @@ discard block |
||
| 101 | 101 | $lastExec = 0; |
| 102 | 102 | $hasError = false; |
| 103 | 103 | |
| 104 | - foreach($jobs as $cmd => $job) { |
|
| 104 | + foreach ($jobs as $cmd => $job) { |
|
| 105 | 105 | $execTime = $job['last_exec']; |
| 106 | - if($execTime > $lastExec) $lastExec = $execTime; |
|
| 107 | - if(!empty($job['error'])) { |
|
| 106 | + if ($execTime > $lastExec) $lastExec = $execTime; |
|
| 107 | + if (!empty($job['error'])) { |
|
| 108 | 108 | $hasError = true; |
| 109 | 109 | } |
| 110 | 110 | } |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | 'Status', |
| 124 | 124 | ]; |
| 125 | 125 | |
| 126 | - if($hasError) { |
|
| 126 | + if ($hasError) { |
|
| 127 | 127 | $header[] = 'Error'; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -133,15 +133,15 @@ discard block |
||
| 133 | 133 | ]); |
| 134 | 134 | |
| 135 | 135 | $cnt = 1; |
| 136 | - foreach($jobs as $cmd => $job) { |
|
| 137 | - if($cnt > 1) $table->addRow(new TableSeparator()); |
|
| 136 | + foreach ($jobs as $cmd => $job) { |
|
| 137 | + if ($cnt > 1) $table->addRow(new TableSeparator()); |
|
| 138 | 138 | $row = [ |
| 139 | 139 | $cmd, |
| 140 | 140 | $job['period'], |
| 141 | 141 | ($job['last_exec'] ? date("d.m.Y H:i:s", $job['last_exec']) : 'NONE'), |
| 142 | 142 | $job['status'], |
| 143 | 143 | ]; |
| 144 | - if($hasError) { |
|
| 144 | + if ($hasError) { |
|
| 145 | 145 | $row[] = $job['error']; |
| 146 | 146 | } |
| 147 | 147 | $table->addRow($row); |
@@ -170,11 +170,11 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
| 172 | 172 | |
| 173 | - if(!empty($jobs)) { |
|
| 173 | + if (!empty($jobs)) { |
|
| 174 | 174 | |
| 175 | - foreach($jobs as $cmd => $job) { |
|
| 175 | + foreach ($jobs as $cmd => $job) { |
|
| 176 | 176 | |
| 177 | - if($this->isActualJob($job)) { |
|
| 177 | + if ($this->isActualJob($job)) { |
|
| 178 | 178 | |
| 179 | 179 | $job['status'] = self::EXEC_STATUS_WORK; |
| 180 | 180 | $this->updaateJob($cmd, $job); |
@@ -186,12 +186,12 @@ discard block |
||
| 186 | 186 | $timeStart = microtime(true); |
| 187 | 187 | $returnCode = $command->run($cmdInput, $output); |
| 188 | 188 | |
| 189 | - if(!$returnCode) { |
|
| 189 | + if (!$returnCode) { |
|
| 190 | 190 | |
| 191 | 191 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
| 192 | 192 | |
| 193 | 193 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 194 | - if($this->logger) { |
|
| 194 | + if ($this->logger) { |
|
| 195 | 195 | $this->logger->alert($msg); |
| 196 | 196 | } |
| 197 | 197 | $output->writeln(PHP_EOL . $msg); |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | $job['error_code'] = $returnCode; |
| 203 | 203 | |
| 204 | 204 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, microtime(true) - $timeStart); |
| 205 | - if($this->logger) { |
|
| 205 | + if ($this->logger) { |
|
| 206 | 206 | $this->logger->alert($msg); |
| 207 | 207 | } |
| 208 | 208 | $output->writeln(PHP_EOL . $msg); |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | $job['error'] = $e->getMessage(); |
| 215 | 215 | |
| 216 | 216 | |
| 217 | - if($this->logger) { |
|
| 217 | + if ($this->logger) { |
|
| 218 | 218 | $this->logger->error($e, ['command' => $cmd]); |
| 219 | 219 | } |
| 220 | 220 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
@@ -236,21 +236,21 @@ discard block |
||
| 236 | 236 | |
| 237 | 237 | protected function isActualJob(&$job) { |
| 238 | 238 | |
| 239 | - if(isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 239 | + if (isset($job['status']) && $job['status'] !== self::EXEC_STATUS_SUCCESS) { |
|
| 240 | 240 | return false; |
| 241 | 241 | } |
| 242 | 242 | |
| 243 | 243 | $period = intval($job['period']); |
| 244 | 244 | |
| 245 | - if($period > 0) { |
|
| 246 | - if($period < $this->minAgentPeriod) { |
|
| 245 | + if ($period > 0) { |
|
| 246 | + if ($period < $this->minAgentPeriod) { |
|
| 247 | 247 | $job['orig_period'] = $period; |
| 248 | 248 | $period = $job['period'] = $this->minAgentPeriod; |
| 249 | 249 | } |
| 250 | - if(time() - $job['last_exec'] >= $period) { |
|
| 250 | + if (time() - $job['last_exec'] >= $period) { |
|
| 251 | 251 | return true; |
| 252 | 252 | } |
| 253 | - } else if(!empty($job['times'])) { |
|
| 253 | + } else if (!empty($job['times'])) { |
|
| 254 | 254 | //TODO: |
| 255 | 255 | } |
| 256 | 256 | |
@@ -265,9 +265,9 @@ discard block |
||
| 265 | 265 | $commands = $app->all(); |
| 266 | 266 | |
| 267 | 267 | $selfCommands = []; |
| 268 | - foreach($commands as $command) { |
|
| 268 | + foreach ($commands as $command) { |
|
| 269 | 269 | /** @var BxCommand $command */ |
| 270 | - if($command instanceof BxCommand) { |
|
| 270 | + if ($command instanceof BxCommand) { |
|
| 271 | 271 | $name = $command->getName(); |
| 272 | 272 | $selfCommands[$name] = [ |
| 273 | 273 | 'object' => $command, |
@@ -277,12 +277,12 @@ discard block |
||
| 277 | 277 | |
| 278 | 278 | $agents = []; |
| 279 | 279 | $reader = new AnnotationReader(); |
| 280 | - foreach($selfCommands as $cmd => $selfCommand) { |
|
| 280 | + foreach ($selfCommands as $cmd => $selfCommand) { |
|
| 281 | 281 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
| 282 | 282 | $annotations = $reader->getClassAnnotations($reflectionClass); |
| 283 | 283 | |
| 284 | - foreach($annotations as $annotation) { |
|
| 285 | - if($annotation instanceof Agent) { |
|
| 284 | + foreach ($annotations as $annotation) { |
|
| 285 | + if ($annotation instanceof Agent) { |
|
| 286 | 286 | $agents[$cmd] = $annotation->toArray(); |
| 287 | 287 | } |
| 288 | 288 | } |
@@ -290,8 +290,8 @@ discard block |
||
| 290 | 290 | |
| 291 | 291 | $crontab = $this->getCronTab(); |
| 292 | 292 | |
| 293 | - foreach($crontab as $cmd => $job) { |
|
| 294 | - if(is_array($job) && isset($agents[$cmd])) { |
|
| 293 | + foreach ($crontab as $cmd => $job) { |
|
| 294 | + if (is_array($job) && isset($agents[$cmd])) { |
|
| 295 | 295 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
| 296 | 296 | } |
| 297 | 297 | } |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | $fh = fopen($filename, 'c'); |
| 326 | 326 | if (flock($fh, LOCK_EX)) { |
| 327 | 327 | ftruncate($fh, 0); |
| 328 | - if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 328 | + if (!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
|
| 329 | 329 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
| 330 | 330 | } |
| 331 | 331 | } |
@@ -343,10 +343,10 @@ discard block |
||
| 343 | 343 | $filename = EnvHelper::getCrontabFile(); |
| 344 | 344 | |
| 345 | 345 | $fh = fopen($filename, 'r'); |
| 346 | - if(flock($fh, LOCK_SH)) { |
|
| 347 | - if($data = @fread($fh, filesize($filename))) { |
|
| 346 | + if (flock($fh, LOCK_SH)) { |
|
| 347 | + if ($data = @fread($fh, filesize($filename))) { |
|
| 348 | 348 | $decoded = json_decode($data, true); |
| 349 | - if(is_array($decoded)) { |
|
| 349 | + if (is_array($decoded)) { |
|
| 350 | 350 | $cronTab = $decoded; |
| 351 | 351 | } |
| 352 | 352 | } |
@@ -359,14 +359,14 @@ discard block |
||
| 359 | 359 | |
| 360 | 360 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
| 361 | 361 | |
| 362 | - if($sort == self::SORT_TIME) { |
|
| 362 | + if ($sort == self::SORT_TIME) { |
|
| 363 | 363 | $sorting = []; |
| 364 | - foreach($crontab as $cmd => $data) { |
|
| 364 | + foreach ($crontab as $cmd => $data) { |
|
| 365 | 365 | $sorting[$cmd] = $data['last_exec']; |
| 366 | 366 | } |
| 367 | 367 | arsort($sorting, SORT_NUMERIC); |
| 368 | 368 | $sorted = []; |
| 369 | - foreach($sorting as $cmd => $time) { |
|
| 369 | + foreach ($sorting as $cmd => $time) { |
|
| 370 | 370 | $sorted[$cmd] = $crontab[$cmd]; |
| 371 | 371 | } |
| 372 | 372 | $crontab = $sorted; |