| Total Complexity | 71 |
| Total Lines | 405 |
| Duplicated Lines | 0 % |
| Changes | 15 | ||
| Bugs | 4 | Features | 0 |
Complex classes like Cron often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cron, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Cron extends BxCommand { |
||
| 16 | |||
| 17 | use LockableTrait; |
||
| 18 | |||
| 19 | const EXEC_STATUS_SUCCESS = 'SUCCESS'; |
||
| 20 | const EXEC_STATUS_ERROR = 'ERROR'; |
||
| 21 | const EXEC_STATUS_WORK = 'WORK'; |
||
| 22 | |||
| 23 | const SORT_NAME = 'name'; |
||
| 24 | const SORT_TIME = 'time'; |
||
| 25 | |||
| 26 | private $minAgentPeriod; |
||
| 27 | |||
| 28 | protected function configure() { |
||
| 36 | } |
||
| 37 | |||
| 38 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 100 | } |
||
| 101 | |||
| 102 | protected function showStatus(OutputInterface $output, $sort) { |
||
| 162 | } |
||
| 163 | |||
| 164 | protected function cleanJob($command = false) { |
||
| 177 | } |
||
| 178 | |||
| 179 | protected function executeJobs(OutputInterface $output) { |
||
| 180 | |||
| 181 | $jobs = $this->getCronJobs(); |
||
| 182 | $allTimeout = EnvHelper::getCrontabTimeout(); |
||
| 183 | $workTime = 0; |
||
| 184 | |||
| 185 | if(is_array($jobs) && !empty($jobs)) { |
||
| 186 | |||
| 187 | /* |
||
| 188 | * Минимально допустимый период выполнения одной задачи |
||
| 189 | * при котором гарантируется выполнение всех задач |
||
| 190 | */ |
||
| 191 | $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
||
| 192 | $this->logger->alert(sprintf("Minimal agent period: %d", $this->minAgentPeriod)); |
||
| 193 | |||
| 194 | foreach($jobs as $cmd => $job) { |
||
| 195 | |||
| 196 | if($this->isActualJob($job)) { |
||
| 197 | |||
| 198 | $job['status'] = self::EXEC_STATUS_WORK; |
||
| 199 | $this->updateJob($cmd, $job); |
||
| 200 | |||
| 201 | $command = $this->getApplication()->find($cmd); |
||
| 202 | $cmdInput = new ArrayInput(['command' => $cmd]); |
||
| 203 | $timeStart = microtime(true); |
||
| 204 | $execTime = 0; |
||
| 205 | try { |
||
| 206 | |||
| 207 | $returnCode = $command->run($cmdInput, $output); |
||
| 208 | $execTime = microtime(true) - $timeStart; |
||
| 209 | |||
| 210 | if(!$returnCode) { |
||
| 211 | |||
| 212 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
||
| 213 | |||
| 214 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime); |
||
| 215 | if($this->logger) { |
||
| 216 | $this->logger->alert($msg); |
||
| 217 | } |
||
| 218 | $output->writeln(PHP_EOL . $msg); |
||
| 219 | |||
| 220 | } else { |
||
| 221 | |||
| 222 | $job['status'] = self::EXEC_STATUS_ERROR; |
||
| 223 | $job['error_code'] = $returnCode; |
||
| 224 | |||
| 225 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime); |
||
| 226 | if($this->logger) { |
||
| 227 | $this->logger->alert($msg); |
||
| 228 | } |
||
| 229 | $output->writeln(PHP_EOL . $msg); |
||
| 230 | } |
||
| 231 | |||
| 232 | } catch (\Exception $e) { |
||
| 233 | |||
| 234 | $job['status'] = self::EXEC_STATUS_ERROR; |
||
| 235 | $job['error'] = $e->getMessage(); |
||
| 236 | |||
| 237 | |||
| 238 | if($this->logger) { |
||
| 239 | $this->logger->error($e, ['command' => $cmd]); |
||
| 240 | } |
||
| 241 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
||
| 242 | |||
| 243 | } finally { |
||
| 244 | |||
| 245 | if(!$execTime) { |
||
| 246 | $execTime = microtime(true) - $timeStart; |
||
| 247 | } |
||
| 248 | $job['last_exec'] = time(); |
||
| 249 | $job['exec_time'] = round($execTime, 1); |
||
| 250 | } |
||
| 251 | |||
| 252 | $this->updateJob($cmd, $job); |
||
| 253 | |||
| 254 | // $workTime += $execTime; |
||
| 255 | // if($workTime * 2 > $allTimeout) { |
||
| 256 | // break; |
||
| 257 | // } |
||
| 258 | /* |
||
| 259 | * Let's do just one task |
||
| 260 | */ |
||
| 261 | break; |
||
| 262 | } |
||
| 263 | } // foreach($jobs as $cmd => $job) |
||
| 264 | } // if(!empty($jobs)) |
||
| 265 | } |
||
| 266 | |||
| 267 | protected function isActualJob(&$job) { |
||
| 288 | } |
||
| 289 | |||
| 290 | public function getCronJobs() { |
||
| 291 | |||
| 292 | $crontab = $this->getCronTab(); |
||
| 293 | if($crontab === false) { |
||
| 294 | return false; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** @var Application $app */ |
||
| 298 | $app = $this->getApplication(); |
||
| 299 | |||
| 300 | $commands = $app->all(); |
||
| 301 | |||
| 302 | $selfCommands = []; |
||
| 303 | foreach($commands as $command) { |
||
| 304 | /** @var BxCommand $command */ |
||
| 305 | if($command instanceof BxCommand) { |
||
| 306 | $name = $command->getName(); |
||
| 307 | $selfCommands[$name] = [ |
||
| 308 | 'object' => $command, |
||
| 309 | ]; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | $agents = []; |
||
| 314 | $reader = new AnnotationReader(); |
||
| 315 | foreach($selfCommands as $cmd => $selfCommand) { |
||
| 316 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
||
| 317 | $annotations = $reader->getClassAnnotations($reflectionClass); |
||
| 318 | |||
| 319 | foreach($annotations as $annotation) { |
||
| 320 | if($annotation instanceof Agent) { |
||
| 321 | $agents[$cmd] = $annotation->toArray(); |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | foreach($crontab as $cmd => $job) { |
||
| 327 | if(is_array($job) && isset($agents[$cmd])) { |
||
| 328 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | $this->setCronTab($agents); |
||
| 333 | |||
| 334 | return $agents; |
||
| 335 | } |
||
| 336 | |||
| 337 | protected function updateJob($cmd, $job) { |
||
| 338 | |||
| 339 | return $this->updateCronTab([$cmd => $job]); |
||
| 340 | } |
||
| 341 | |||
| 342 | protected function updateCronTab(array $changedAgents) { |
||
| 343 | |||
| 344 | $crontab = $this->getCronTab(); |
||
| 345 | |||
| 346 | if($crontab === false) { |
||
| 347 | return false; |
||
| 348 | } else { |
||
| 349 | $crontab = array_merge($crontab, $changedAgents); |
||
| 350 | return $this->setCronTab($crontab); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | protected function setCronTab(array $agents) { |
||
| 355 | |||
| 356 | $isSuccess = true; |
||
| 357 | $this->sortCronTab($agents); |
||
| 358 | |||
| 359 | $filename = EnvHelper::getCrontabFile(); |
||
| 360 | |||
| 361 | $fh = fopen($filename, 'c'); |
||
| 362 | if (flock($fh, LOCK_EX)) { |
||
| 363 | ftruncate($fh, 0); |
||
| 364 | if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
||
| 365 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
||
| 366 | } |
||
| 367 | } else { |
||
| 368 | $isSuccess = false; |
||
| 369 | } |
||
| 370 | flock($fh, LOCK_UN); |
||
| 371 | fclose($fh); |
||
| 372 | |||
| 373 | return $isSuccess; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return array|false|mixed |
||
| 378 | */ |
||
| 379 | protected function getCronTab() { |
||
| 403 | } |
||
| 404 | |||
| 405 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
||
| 406 | |||
| 420 | } |
||
| 421 | } |
||
| 422 | } |