Skip to content

Commit

Permalink
Add command to query the queue status and delete a job
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Jul 19, 2017
1 parent 3538dd6 commit 887821d
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 7 deletions.
49 changes: 49 additions & 0 deletions core/Command/Background/Queue/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace OC\Core\Command\Background\Queue;

use OC\Console\CommandLogger;
use OCP\BackgroundJob\IJob;
use OCP\ILogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Delete extends Command {

/** @var \OCP\BackgroundJob\IJobList */
private $jobList;

public function __construct() {
$this->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.");
}
}
44 changes: 44 additions & 0 deletions core/Command/Background/Queue/Status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace OC\Core\Command\Background\Queue;

use OC\Console\CommandLogger;
use OCP\BackgroundJob\IJob;
use OCP\ILogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Status extends Command {

/** @var \OCP\BackgroundJob\IJobList */
private $jobList;

public function __construct() {
$this->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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace OC\Core\Command\Background;
namespace OC\Core\Command\Background\Queue;

use OC\Console\CommandLogger;
use OCP\ILogger;
Expand All @@ -23,7 +23,7 @@ public function __construct() {

protected function configure() {
$this
->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);
}
Expand Down
4 changes: 3 additions & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
26 changes: 22 additions & 4 deletions lib/private/BackgroundJob/JobList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
}
16 changes: 16 additions & 0 deletions lib/public/BackgroundJob/IJobList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

0 comments on commit 887821d

Please sign in to comment.