Total Complexity | 80 |
Total Lines | 434 |
Duplicated Lines | 0 % |
Changes | 19 | ||
Bugs | 5 | Features | 1 |
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 RESTART_TIME = 3600; |
||
24 | |||
25 | const SORT_NAME = 'name'; |
||
26 | const SORT_TIME = 'time'; |
||
27 | |||
28 | private $minAgentPeriod; |
||
29 | |||
30 | protected function configure() { |
||
31 | |||
32 | $this->setName('system:cron') |
||
33 | ->setDescription('Job scheduler for application commands') |
||
34 | ->addOption('status', 's', InputOption::VALUE_NONE, 'Show BX_CRONTAB status table') |
||
35 | ->addOption('bytime', 't', InputOption::VALUE_NONE, 'Sort status table by exec time desc') |
||
36 | ->addOption('clean', 'c', InputOption::VALUE_REQUIRED, 'Command to be clean crontab data (status, last exec)') |
||
37 | ->addOption('all', 'a', InputOption::VALUE_NONE, 'Command to be clean all crontab data (status, last exec)'); |
||
38 | } |
||
39 | |||
40 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
102 | } |
||
103 | |||
104 | protected function showStatus(OutputInterface $output, $sort) { |
||
164 | } |
||
165 | |||
166 | protected function cleanJob($command = false) { |
||
179 | } |
||
180 | |||
181 | protected function executeJobs(OutputInterface $output) { |
||
182 | |||
183 | $jobs = $this->getCronJobs(); |
||
184 | $allTimeout = EnvHelper::getCrontabTimeout(); |
||
185 | $workTime = 0; |
||
186 | |||
187 | if(!empty($jobs)) { |
||
188 | |||
189 | /* |
||
190 | * Минимально допустимый период выполнения одной задачи |
||
191 | * при котором гарантируется выполнение всех задач |
||
192 | */ |
||
193 | $this->minAgentPeriod = (count($jobs) + 1) * EnvHelper::getBxCrontabPeriod(); |
||
194 | $msg = sprintf("Minimal agent period: %d", $this->minAgentPeriod); |
||
195 | $this->logger->alert($msg); |
||
196 | $output->writeln($msg); |
||
197 | |||
198 | foreach($jobs as $cmd => $job) { |
||
199 | |||
200 | $job['cmd'] = $cmd; |
||
201 | if($this->isActualJob($job)) { |
||
202 | |||
203 | $job['status'] = self::EXEC_STATUS_WORK; |
||
204 | $job['start_time'] = time(); |
||
205 | $this->updateJob($cmd, $job); |
||
206 | |||
207 | $command = $this->getApplication()->find($cmd); |
||
208 | $cmdInput = new ArrayInput(['command' => $cmd]); |
||
209 | $timeStart = microtime(true); |
||
210 | $execTime = 0; |
||
211 | try { |
||
212 | |||
213 | $returnCode = $command->run($cmdInput, $output); |
||
214 | $execTime = microtime(true) - $timeStart; |
||
215 | |||
216 | if(!$returnCode) { |
||
217 | |||
218 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
||
219 | |||
220 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime); |
||
221 | if($this->logger) { |
||
222 | $this->logger->alert($msg); |
||
223 | } |
||
224 | $output->writeln(PHP_EOL . $msg); |
||
225 | |||
226 | } else { |
||
227 | |||
228 | $job['status'] = self::EXEC_STATUS_ERROR; |
||
229 | $job['error_code'] = $returnCode; |
||
230 | |||
231 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime); |
||
232 | if($this->logger) { |
||
233 | $this->logger->alert($msg); |
||
234 | } |
||
235 | $output->writeln(PHP_EOL . $msg); |
||
236 | } |
||
237 | |||
238 | } catch (\Exception $e) { |
||
239 | |||
240 | $job['status'] = self::EXEC_STATUS_ERROR; |
||
241 | $job['error'] = $e->getMessage(); |
||
242 | |||
243 | |||
244 | if($this->logger) { |
||
245 | $this->logger->error($e, ['command' => $cmd]); |
||
246 | } |
||
247 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
||
248 | |||
249 | } finally { |
||
250 | |||
251 | if(!$execTime) { |
||
252 | $execTime = microtime(true) - $timeStart; |
||
253 | } |
||
254 | $job['last_exec'] = time(); |
||
255 | $job['exec_time'] = round($execTime, 1); |
||
256 | } |
||
257 | |||
258 | $this->updateJob($cmd, $job); |
||
259 | |||
260 | $workTime += $execTime; |
||
261 | if($workTime * 2 > $allTimeout) { |
||
262 | break; |
||
263 | } |
||
264 | /* |
||
265 | * Let's do just one task |
||
266 | */ |
||
267 | //break; |
||
268 | } |
||
269 | } // foreach($jobs as $cmd => $job) |
||
270 | } // if(!empty($jobs)) |
||
271 | } |
||
272 | |||
273 | protected function isActualJob(&$job): bool |
||
314 | } |
||
315 | |||
316 | public function getCronJobs(): array |
||
317 | { |
||
318 | $crontab = $this->getCronTab(); |
||
319 | if($crontab === false) { |
||
320 | return []; |
||
321 | } |
||
322 | |||
323 | /** @var Application $app */ |
||
324 | $app = $this->getApplication(); |
||
325 | |||
326 | $commands = $app->all(); |
||
327 | |||
328 | $selfCommands = []; |
||
329 | foreach($commands as $command) { |
||
330 | /** @var BxCommand $command */ |
||
331 | if($command instanceof BxCommand) { |
||
332 | $name = $command->getName(); |
||
333 | $selfCommands[$name] = [ |
||
334 | 'object' => $command, |
||
335 | ]; |
||
336 | } |
||
337 | } |
||
338 | |||
339 | $agents = []; |
||
340 | $reader = new AnnotationReader(); |
||
341 | foreach($selfCommands as $cmd => $selfCommand) { |
||
342 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
||
343 | $annotations = $reader->getClassAnnotations($reflectionClass); |
||
344 | |||
345 | foreach($annotations as $annotation) { |
||
346 | if($annotation instanceof Agent) { |
||
347 | $agents[$cmd] = $annotation->toArray(); |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | |||
352 | foreach($crontab as $cmd => $job) { |
||
353 | if(is_array($job) && isset($agents[$cmd])) { |
||
354 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | $this->setCronTab($agents); |
||
359 | |||
360 | return $agents; |
||
361 | } |
||
362 | |||
363 | protected function updateJob($cmd, $job) { |
||
364 | |||
365 | return $this->updateCronTab([$cmd => $job]); |
||
366 | } |
||
367 | |||
368 | protected function updateCronTab(array $changedAgents) { |
||
369 | |||
370 | $crontab = $this->getCronTab(); |
||
371 | |||
372 | if($crontab === false) { |
||
373 | return false; |
||
374 | } else { |
||
375 | $crontab = array_merge($crontab, $changedAgents); |
||
376 | return $this->setCronTab($crontab); |
||
377 | } |
||
378 | } |
||
379 | |||
380 | protected function setCronTab(array $agents) { |
||
381 | |||
382 | $isSuccess = true; |
||
383 | $this->sortCronTab($agents); |
||
384 | |||
385 | $filename = EnvHelper::getCrontabFile(); |
||
386 | |||
387 | $fh = fopen($filename, 'c'); |
||
388 | if (flock($fh, LOCK_EX)) { |
||
389 | ftruncate($fh, 0); |
||
390 | if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
||
391 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
||
392 | } |
||
393 | } else { |
||
394 | $isSuccess = false; |
||
395 | } |
||
396 | flock($fh, LOCK_UN); |
||
397 | fclose($fh); |
||
398 | |||
399 | return $isSuccess; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @return array|false|mixed |
||
404 | */ |
||
405 | protected function getCronTab() { |
||
432 | } |
||
433 | |||
434 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
||
435 | |||
449 | } |
||
450 | } |
||
451 | } |