Total Complexity | 79 |
Total Lines | 419 |
Duplicated Lines | 0 % |
Changes | 20 | ||
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 | protected function configure() { |
||
29 | |||
30 | $this->setName('system:cron') |
||
31 | ->setDescription('Job scheduler for application commands') |
||
32 | ->addOption('status', 's', InputOption::VALUE_NONE, 'Show BX_CRONTAB status table') |
||
33 | ->addOption('bytime', 't', InputOption::VALUE_NONE, 'Sort status table by exec time desc') |
||
34 | ->addOption('clean', 'c', InputOption::VALUE_REQUIRED, 'Command to be clean crontab data (status, last exec)') |
||
35 | ->addOption('all', 'a', InputOption::VALUE_NONE, 'Command to be clean all crontab data (status, last exec)'); |
||
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(!empty($jobs)) { |
||
186 | |||
187 | foreach($jobs as $cmd => $job) { |
||
188 | |||
189 | $job['cmd'] = $cmd; |
||
190 | if($this->isActualJob($job)) { |
||
191 | |||
192 | $job['status'] = self::EXEC_STATUS_WORK; |
||
193 | $job['start_time'] = time(); |
||
194 | $this->updateJob($cmd, $job); |
||
195 | |||
196 | $command = $this->getApplication()->find($cmd); |
||
197 | $cmdInput = new ArrayInput(['command' => $cmd]); |
||
198 | $timeStart = microtime(true); |
||
199 | $execTime = 0; |
||
200 | try { |
||
201 | |||
202 | $returnCode = $command->run($cmdInput, $output); |
||
203 | $execTime = microtime(true) - $timeStart; |
||
204 | |||
205 | if(!$returnCode) { |
||
206 | |||
207 | $job['status'] = self::EXEC_STATUS_SUCCESS; |
||
208 | |||
209 | $msg = sprintf("%s: SUCCESS [%.2f s]", $cmd, $execTime); |
||
210 | if($this->logger) { |
||
211 | $this->logger->alert($msg); |
||
212 | } |
||
213 | $output->writeln(PHP_EOL . $msg); |
||
214 | |||
215 | } else { |
||
216 | |||
217 | $job['status'] = self::EXEC_STATUS_ERROR; |
||
218 | $job['error_code'] = $returnCode; |
||
219 | |||
220 | $msg = sprintf("%s: ERROR [%.2f s]", $cmd, $execTime); |
||
221 | if($this->logger) { |
||
222 | $this->logger->alert($msg); |
||
223 | } |
||
224 | $output->writeln(PHP_EOL . $msg); |
||
225 | } |
||
226 | |||
227 | } catch (\Exception $e) { |
||
228 | |||
229 | $job['status'] = self::EXEC_STATUS_ERROR; |
||
230 | $job['error'] = $e->getMessage(); |
||
231 | |||
232 | |||
233 | if($this->logger) { |
||
234 | $this->logger->error($e, ['command' => $cmd]); |
||
235 | } |
||
236 | $output->writeln(PHP_EOL . 'ERROR: ' . $e->getMessage()); |
||
237 | |||
238 | } finally { |
||
239 | |||
240 | if(!$execTime) { |
||
241 | $execTime = microtime(true) - $timeStart; |
||
242 | } |
||
243 | $job['last_exec'] = time(); |
||
244 | $job['exec_time'] = round($execTime, 1); |
||
245 | } |
||
246 | |||
247 | $this->updateJob($cmd, $job); |
||
248 | |||
249 | $workTime += $execTime; |
||
250 | if($workTime * 2 > $allTimeout) { |
||
251 | break; |
||
252 | } |
||
253 | /* |
||
254 | * Let's do just one task |
||
255 | */ |
||
256 | //break; |
||
257 | } |
||
258 | } // foreach($jobs as $cmd => $job) |
||
259 | } // if(!empty($jobs)) |
||
260 | } |
||
261 | |||
262 | protected function isActualJob(&$job): bool |
||
299 | } |
||
300 | |||
301 | public function getCronJobs(): array |
||
302 | { |
||
303 | $crontab = $this->getCronTab(); |
||
304 | if($crontab === false) { |
||
305 | return []; |
||
306 | } |
||
307 | |||
308 | /** @var Application $app */ |
||
309 | $app = $this->getApplication(); |
||
310 | |||
311 | $commands = $app->all(); |
||
312 | |||
313 | $selfCommands = []; |
||
314 | foreach($commands as $command) { |
||
315 | /** @var BxCommand $command */ |
||
316 | if($command instanceof BxCommand) { |
||
317 | $name = $command->getName(); |
||
318 | $selfCommands[$name] = [ |
||
319 | 'object' => $command, |
||
320 | ]; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | $agents = []; |
||
325 | $reader = new AnnotationReader(); |
||
326 | foreach($selfCommands as $cmd => $selfCommand) { |
||
327 | $reflectionClass = new \ReflectionClass($selfCommand['object']); |
||
328 | $annotations = $reader->getClassAnnotations($reflectionClass); |
||
329 | |||
330 | foreach($annotations as $annotation) { |
||
331 | if($annotation instanceof Agent) { |
||
332 | $agents[$cmd] = $annotation->toArray(); |
||
333 | } |
||
334 | } |
||
335 | } |
||
336 | |||
337 | foreach($crontab as $cmd => $job) { |
||
338 | if(is_array($job) && isset($agents[$cmd])) { |
||
339 | $agents[$cmd] = array_merge($job, $agents[$cmd]); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | $this->setCronTab($agents); |
||
344 | |||
345 | return $agents; |
||
346 | } |
||
347 | |||
348 | protected function updateJob($cmd, $job) { |
||
349 | |||
350 | return $this->updateCronTab([$cmd => $job]); |
||
351 | } |
||
352 | |||
353 | protected function updateCronTab(array $changedAgents) { |
||
354 | |||
355 | $crontab = $this->getCronTab(); |
||
356 | |||
357 | if($crontab === false) { |
||
358 | return false; |
||
359 | } else { |
||
360 | $crontab = array_merge($crontab, $changedAgents); |
||
361 | return $this->setCronTab($crontab); |
||
362 | } |
||
363 | } |
||
364 | |||
365 | protected function setCronTab(array $agents) { |
||
366 | |||
367 | $isSuccess = true; |
||
368 | $this->sortCronTab($agents); |
||
369 | |||
370 | $filename = EnvHelper::getCrontabFile(); |
||
371 | |||
372 | $fh = fopen($filename, 'c'); |
||
373 | if (flock($fh, LOCK_EX)) { |
||
374 | ftruncate($fh, 0); |
||
375 | if(!fwrite($fh, json_encode($agents, JSON_PRETTY_PRINT))) { |
||
376 | throw new \Exception('Unable to write BX_CRONTAB : ' . $filename); |
||
377 | } |
||
378 | } else { |
||
379 | $isSuccess = false; |
||
380 | } |
||
381 | flock($fh, LOCK_UN); |
||
382 | fclose($fh); |
||
383 | |||
384 | return $isSuccess; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @return array|false|mixed |
||
389 | */ |
||
390 | protected function getCronTab() { |
||
417 | } |
||
418 | |||
419 | protected function sortCronTab(array &$crontab, $sort = self::SORT_NAME) { |
||
420 | |||
421 | if($sort == self::SORT_TIME) { |
||
434 | } |
||
435 | } |
||
436 | } |