diff --git a/core/Command/Background/Queue/Delete.php b/core/Command/Background/Queue/Delete.php new file mode 100644 index 000000000000..fe3f5d834421 --- /dev/null +++ b/core/Command/Background/Queue/Delete.php @@ -0,0 +1,49 @@ +jobList = \OC::$server->getJobList(); + 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 + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $id = $input->getArgument('id'); + + $job = $this->jobList->getById($id); + if (is_null($job)) { + $output->writeln("Job with id <$id> is not known."); + return; + } + + $this->jobList->removeById($id); + $output->writeln("Job has been deleted."); + } +} diff --git a/core/Command/Background/Queue/Status.php b/core/Command/Background/Queue/Status.php new file mode 100644 index 000000000000..08cfbd68a7ef --- /dev/null +++ b/core/Command/Background/Queue/Status.php @@ -0,0 +1,44 @@ +jobList = \OC::$server->getJobList(); + parent::__construct(); + } + + protected function configure() { + $this + ->setName("background:queue:status") + ->setDescription("List queue status"); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + */ + protected function execute(InputInterface $input, OutputInterface $output) { + /** @var TableHelper $t */ + $t = $this->getHelper('table'); + $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($output); + } +} diff --git a/core/Command/Background/Worker.php b/core/Command/Background/Queue/Worker.php similarity index 94% rename from core/Command/Background/Worker.php rename to core/Command/Background/Queue/Worker.php index fa2106fb3265..7d9a2cf7fd9d 100644 --- a/core/Command/Background/Worker.php +++ b/core/Command/Background/Queue/Worker.php @@ -1,6 +1,6 @@ setName("background:worker") + ->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); } diff --git a/core/register_command.php b/core/register_command.php index 4c9a3af2305c..faa6684bae2a 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -71,7 +71,9 @@ $application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig())); - $application->add(new OC\Core\Command\Background\Worker()); + $application->add(new OC\Core\Command\Background\Queue\Worker()); + $application->add(new OC\Core\Command\Background\Queue\Status()); + $application->add(new OC\Core\Command\Background\Queue\Delete()); $application->add(new OC\Core\Command\Config\App\DeleteConfig(\OC::$server->getConfig())); $application->add(new OC\Core\Command\Config\App\GetConfig(\OC::$server->getConfig())); diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index 55ddbbe63f31..1ef90c83e405 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -108,7 +108,7 @@ public function remove($job, $argument = null) { /** * @param int $id */ - protected function removeById($id) { + public function removeById($id) { $query = $this->connection->getQueryBuilder(); $query->delete('jobs') ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); @@ -305,9 +305,7 @@ public function getLastJob() { } /** - * set the lastRun of $job to now - * - * @param IJob $job + * @inheritdoc */ public function setLastRun($job) { $query = $this->connection->getQueryBuilder(); @@ -324,4 +322,24 @@ public function setExecutionTime($job, $timeTaken) { ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); $query->execute(); } + + /** + * @inheritdoc + */ + public function listJobs(\Closure $callback) { + $query = $this->connection->getQueryBuilder(); + $query->select('*') + ->from('jobs'); + $result = $query->execute(); + + while ($row = $result->fetch()) { + $job = $this->buildJob($row); + if ($job) { + if ($callback($job) === false) { + break; + } + } + } + $result->closeCursor(); + } } diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index 9ca979cbf796..81998fbe501e 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -127,4 +127,20 @@ public function setLastRun($job); * @since 10.0.0 */ public function setExecutionTime($job, $timeTaken); + + /** + * iterate over all jobs in the queue + * + * @return void + * @since 10.0.3 + */ + public function listJobs(\Closure $callback); + + /** + * remove a specific job by id + * @return void + * @since 10.0.3 + */ + public function removeById($id); + }