|
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
|
|
|
|