diff options
author | Joas Schilling <coding@schilljs.com> | 2021-05-31 18:15:26 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-10-14 10:57:16 +0300 |
commit | 9cd9f4b4bc87133cf446746295e9730c244b3a49 (patch) | |
tree | 04a08e521e533e61e76f8d2ff3534cf2a6b0db11 | |
parent | e2a7482b490b6b3e5143b4678d88867a6398b248 (diff) |
Move queries to the joblist
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | core/Command/Background/Job.php | 25 | ||||
-rw-r--r-- | core/register_command.php | 2 | ||||
-rw-r--r-- | lib/private/BackgroundJob/JobList.php | 33 | ||||
-rw-r--r-- | lib/public/BackgroundJob/IJobList.php | 15 | ||||
-rw-r--r-- | tests/lib/BackgroundJob/DummyJobList.php | 7 |
5 files changed, 54 insertions, 28 deletions
diff --git a/core/Command/Background/Job.php b/core/Command/Background/Job.php index 2a96744310b..62f2ea823dd 100644 --- a/core/Command/Background/Job.php +++ b/core/Command/Background/Job.php @@ -27,8 +27,6 @@ namespace OC\Core\Command\Background; use OCP\BackgroundJob\IJob; use OCP\BackgroundJob\IJobList; -use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\IDBConnection; use OCP\ILogger; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -39,17 +37,13 @@ use Symfony\Component\Console\Output\OutputInterface; class Job extends Command { /** @var IJobList */ protected $jobList; - /** @var IDBConnection */ - protected $connection; /** @var ILogger */ protected $logger; public function __construct(IJobList $jobList, - IDBConnection $connection, ILogger $logger) { parent::__construct(); $this->jobList = $jobList; - $this->connection = $connection; $this->logger = $logger; } @@ -86,14 +80,7 @@ class Job extends Command { $output->writeln(''); $output->writeln('<comment>Forcing execution of the job</comment>'); - $query = $this->connection->getQueryBuilder(); - $query->update('jobs') - ->set('last_run', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)) - ->set('reserved_at', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)) - ->where($query->expr()->eq('id', $query->createNamedParameter($jobId), IQueryBuilder::PARAM_INT)); - - $query->executeUpdate(); - + $this->jobList->resetBackgroundJob($job); $job = $this->jobList->getById($jobId); $job->execute($this->jobList, $this->logger); $this->jobList->setLastJob($job); @@ -110,15 +97,7 @@ class Job extends Command { } protected function printJobInfo(int $jobId, IJob $job, OutputInterface$output): void { - - $query = $this->connection->getQueryBuilder(); - $query->select('*') - ->from('jobs') - ->where($query->expr()->eq('id', $query->createNamedParameter($jobId), IQueryBuilder::PARAM_INT)); - - $result = $query->executeQuery(); - $row = $result->fetch(); - $result->closeCursor(); + $row = $this->jobList->getDetailsById($jobId); $lastRun = new \DateTime(); $lastRun->setTimestamp((int) $row['last_run']); diff --git a/core/register_command.php b/core/register_command.php index b27d92e81b6..3cff363e46f 100644 --- a/core/register_command.php +++ b/core/register_command.php @@ -90,7 +90,7 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) { $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\Job(\OC::$server->getJobList(), \OC::$server->getDatabaseConnection(), \OC::$server->getLogger())); + $application->add(new OC\Core\Command\Background\Job(\OC::$server->getJobList(), \OC::$server->getLogger())); $application->add(\OC::$server->query(\OC\Core\Command\Broadcast\Test::class)); diff --git a/lib/private/BackgroundJob/JobList.php b/lib/private/BackgroundJob/JobList.php index f3b96dcbb7b..fe8e94513e1 100644 --- a/lib/private/BackgroundJob/JobList.php +++ b/lib/private/BackgroundJob/JobList.php @@ -236,19 +236,29 @@ class JobList implements IJobList { * @return IJob|null */ public function getById($id) { + $row = $this->getDetailsById($id); + + if ($row) { + return $this->buildJob($row); + } + + return null; + } + + public function getDetailsById(int $id): ?array { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from('jobs') ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); - $result = $query->execute(); + $result = $query->executeQuery(); $row = $result->fetch(); $result->closeCursor(); if ($row) { - return $this->buildJob($row); - } else { - return null; + return $row; } + + return null; } /** @@ -330,4 +340,19 @@ class JobList implements IJobList { ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); $query->execute(); } + + /** + * Reset the $job so it executes on the next trigger + * + * @param IJob $job + * @since 22.0.0 + */ + public function resetBackgroundJob(IJob $job): void { + $query = $this->connection->getQueryBuilder(); + $query->update('jobs') + ->set('last_run', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)) + ->set('reserved_at', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)) + ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId()), IQueryBuilder::PARAM_INT)); + $query->executeStatement(); + } } diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index 299d6725229..b8d6b03c6ea 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -98,6 +98,13 @@ interface IJobList { public function getById($id); /** + * @param int $id + * @return array|null + * @since 22.0.0 + */ + public function getDetailsById(int $id): ?array; + + /** * set the job that was last ran to the current time * * @param \OCP\BackgroundJob\IJob $job @@ -129,4 +136,12 @@ interface IJobList { * @since 12.0.0 */ public function setExecutionTime(IJob $job, $timeTaken); + + /** + * Reset the $job so it executes on the next trigger + * + * @param IJob $job + * @since 22.0.0 + */ + public function resetBackgroundJob(IJob $job): void; } diff --git a/tests/lib/BackgroundJob/DummyJobList.php b/tests/lib/BackgroundJob/DummyJobList.php index 452b9bb98ed..97fc551d8f5 100644 --- a/tests/lib/BackgroundJob/DummyJobList.php +++ b/tests/lib/BackgroundJob/DummyJobList.php @@ -117,6 +117,10 @@ class DummyJobList extends \OC\BackgroundJob\JobList { return null; } + public function getDetailsById(int $id): ?array { + return null; + } + /** * set the lastRun of $job to now * @@ -128,4 +132,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList { public function setExecutionTime(IJob $job, $timeTaken) { } + + public function resetBackgroundJob(IJob $job): void { + } } |