Completed
Pull Request — master (#24551)
by Thomas
19:32 queued 48s
created

Worker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OC\Core\Command\Background\Queue;
4
5
use OC\Console\CommandLogger;
6
use OC\Core\Command\Base;
7
use OCP\ILogger;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class Worker extends Base {
14
15
	/** @var \OCP\BackgroundJob\IJobList */
16
	private $jobList;
17
	/** @var ILogger */
18
	private $logger;
19
20
	public function __construct() {
21
		$this->jobList = \OC::$server->getJobList();
22
		parent::__construct();
23
	}
24
25
	protected function configure() {
26
		$this
27
			->setName("background:queue:worker")
28
			->setDescription("Listen to the background job queue and execute the jobs")
29
			->addOption('sleep', null, InputOption::VALUE_OPTIONAL, 'Number of seconds to sleep when no job is available', 3);
30
	}
31
32
	/**
33
	* @param InputInterface $input
34
	* @param OutputInterface $output
35
	*/
36
	protected function execute(InputInterface $input, OutputInterface $output) {
37
		$this->logger = new CommandLogger($output);
38
39
		$waitTime = \max(1, $input->getOption('sleep'));
40
		while (true) {
41
			if ($this->hasBeenInterrupted()) {
42
				break;
43
			}
44
			if ($this->executeNext() === null) {
45
				\sleep($waitTime);
46
			}
47
		}
48
	}
49
50
	private function executeNext() {
51
		$job = $this->jobList->getNext();
52
		if ($job === null) {
53
			return null;
54
		}
55
		$jobId = $job->getId();
56
		$this->logger->debug('Run job with ID ' . $job->getId(), ['app' => 'cron']);
57
		$job->execute($this->jobList, $this->logger);
58
		$this->logger->debug('Finished job with ID ' . $job->getId(), ['app' => 'cron']);
59
60
		$this->jobList->setLastJob($job);
61
		unset($job);
62
63
		return $jobId;
64
	}
65
}
66