-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding background:queue commands: worker, status and delete
- Loading branch information
1 parent
2038c0b
commit 3929f66
Showing
10 changed files
with
538 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace OC\Core\Command\Background\Queue; | ||
|
||
use OCP\BackgroundJob\IJobList; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Delete extends Command { | ||
|
||
/** @var \OCP\BackgroundJob\IJobList */ | ||
private $jobList; | ||
|
||
public function __construct(IJobList $jobList) { | ||
$this->jobList = $jobList; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() { | ||
$this | ||
->setName('background:queue:delete') | ||
->setDescription('Delete a job from the queue') | ||
->addArgument('id', InputArgument::REQUIRED, 'id of the job to be deleted'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @return void | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$id = $input->getArgument('id'); | ||
|
||
$job = $this->jobList->getById($id); | ||
if ($job === null) { | ||
$output->writeln("Job with id <$id> is not known."); | ||
return; | ||
} | ||
|
||
$this->jobList->removeById($id); | ||
$output->writeln('Job has been deleted.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace OC\Core\Command\Background\Queue; | ||
|
||
use OCP\BackgroundJob\IJob; | ||
use OCP\BackgroundJob\IJobList; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Status extends Command { | ||
|
||
/** @var \OCP\BackgroundJob\IJobList */ | ||
private $jobList; | ||
|
||
public function __construct(IJobList $jobList) { | ||
$this->jobList = $jobList; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() { | ||
$this | ||
->setName('background:queue:status') | ||
->setDescription('List queue status'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* @return void | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$t = new Table($output); | ||
$t->setHeaders(['Id', 'Job', 'Last run', 'Arguments']); | ||
$this->jobList->listJobs(function (IJob $job) use ($t) { | ||
$t->addRow([$job->getId(), \get_class($job), \date('c', $job->getLastRun()), $job->getArgument()]); | ||
}); | ||
$t->render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace OC\Core\Command\Background\Queue; | ||
|
||
use OC\Console\CommandLogger; | ||
use OC\Core\Command\Base; | ||
use OCP\ILogger; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class Worker extends Base { | ||
|
||
/** @var \OCP\BackgroundJob\IJobList */ | ||
private $jobList; | ||
/** @var ILogger */ | ||
private $logger; | ||
|
||
public function __construct() { | ||
$this->jobList = \OC::$server->getJobList(); | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure() { | ||
$this | ||
->setName("background:queue:worker") | ||
->setDescription("Listen to the background job queue and execute the jobs") | ||
->addOption('sleep', null, InputOption::VALUE_OPTIONAL, 'Number of seconds to sleep when no job is available', 3); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$this->logger = new CommandLogger($output); | ||
|
||
$waitTime = \max(1, $input->getOption('sleep')); | ||
while (true) { | ||
if ($this->hasBeenInterrupted()) { | ||
break; | ||
} | ||
if ($this->executeNext() === null) { | ||
\sleep($waitTime); | ||
} | ||
} | ||
} | ||
|
||
private function executeNext() { | ||
$job = $this->jobList->getNext(); | ||
if ($job === null) { | ||
return null; | ||
} | ||
$jobId = $job->getId(); | ||
$this->logger->debug('Run job with ID ' . $job->getId(), ['app' => 'cron']); | ||
$job->execute($this->jobList, $this->logger); | ||
$this->logger->debug('Finished job with ID ' . $job->getId(), ['app' => 'cron']); | ||
|
||
$this->jobList->setLastJob($job); | ||
unset($job); | ||
|
||
return $jobId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.