| Conditions | 10 |
| Paths | 20 |
| Total Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 74 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
| 75 | if (\OCP\Util::needUpgrade()) { |
||
| 76 | $output->writeln('Update required, skipping cron'); |
||
| 77 | return 1; |
||
| 78 | } |
||
| 79 | if ($this->config->getSystemValue('maintenance', false)) { |
||
| 80 | $output->writeln('We are in maintenance mode, skipping cron'); |
||
| 81 | return 1; |
||
| 82 | } |
||
| 83 | if ($this->config->getSystemValue('singleuser', false)) { |
||
| 84 | $output->writeln('We are in admin only mode, skipping cron'); |
||
| 85 | return 1; |
||
| 86 | } |
||
| 87 | |||
| 88 | // clean the temp folder |
||
| 89 | $this->tempManager->cleanOld(); |
||
| 90 | |||
| 91 | // Exit if background jobs are disabled! |
||
| 92 | $appMode = $this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax'); |
||
| 93 | if ($appMode === 'none') { |
||
| 94 | $output->writeln('Background Jobs are disabled!'); |
||
| 95 | return 1; |
||
| 96 | } |
||
| 97 | |||
| 98 | // We call ownCloud from the CLI (aka cron) |
||
| 99 | if ($appMode !== 'cron') { |
||
| 100 | $this->config->setAppValue('core', 'backgroundjobs_mode', 'cron'); |
||
| 101 | } |
||
| 102 | |||
| 103 | $progress = new ProgressBar($output); |
||
| 104 | |||
| 105 | // We only ask for jobs for 14 minutes, because after 15 minutes the next |
||
| 106 | // system cron task should spawn. |
||
| 107 | $endTime = \time() + 14 * 60; |
||
| 108 | |||
| 109 | $executedJobs = []; |
||
| 110 | while ($job = $this->jobList->getNext()) { |
||
| 111 | if (isset($executedJobs[$job->getId()])) { |
||
| 112 | $this->jobList->unlockJob($job); |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | $progress->advance(); |
||
| 116 | $jobName = \get_class($job); |
||
| 117 | $progress->setMessage("Executing: {$job->getId()} - {$jobName}"); |
||
| 118 | |||
| 119 | $job->execute($this->jobList, $this->logger); |
||
| 120 | |||
| 121 | // clean up after unclean jobs |
||
| 122 | \OC_Util::tearDownFS(); |
||
| 123 | |||
| 124 | $this->jobList->setLastJob($job); |
||
| 125 | $executedJobs[$job->getId()] = true; |
||
| 126 | unset($job); |
||
| 127 | |||
| 128 | if (\time() > $endTime) { |
||
| 129 | break; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | // Log the successful cron execution |
||
| 134 | if ($this->config->getSystemValue('cron_log', true)) { |
||
| 135 | $this->config->setAppValue('core', 'lastcron', \time()); |
||
| 136 | } |
||
| 137 | $output->writeln(''); |
||
| 138 | |||
| 139 | return 0; |
||
| 140 | } |
||
| 141 | } |
||
| 142 |