Move queries to the joblist

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2021-05-31 17:15:26 +02:00
parent d92bbfee57
commit 27ee8a7632
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
4 changed files with 53 additions and 27 deletions

View File

@ -27,8 +27,6 @@ namespace OC\Core\Command\Background;
use OCP\BackgroundJob\IJob; use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\ILogger; use OCP\ILogger;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
@ -39,17 +37,13 @@ use Symfony\Component\Console\Output\OutputInterface;
class Job extends Command { class Job extends Command {
/** @var IJobList */ /** @var IJobList */
protected $jobList; protected $jobList;
/** @var IDBConnection */
protected $connection;
/** @var ILogger */ /** @var ILogger */
protected $logger; protected $logger;
public function __construct(IJobList $jobList, public function __construct(IJobList $jobList,
IDBConnection $connection,
ILogger $logger) { ILogger $logger) {
parent::__construct(); parent::__construct();
$this->jobList = $jobList; $this->jobList = $jobList;
$this->connection = $connection;
$this->logger = $logger; $this->logger = $logger;
} }
@ -86,14 +80,7 @@ class Job extends Command {
$output->writeln(''); $output->writeln('');
$output->writeln('<comment>Forcing execution of the job</comment>'); $output->writeln('<comment>Forcing execution of the job</comment>');
$query = $this->connection->getQueryBuilder(); $this->jobList->resetBackgroundJob($job);
$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();
$job = $this->jobList->getById($jobId); $job = $this->jobList->getById($jobId);
$job->execute($this->jobList, $this->logger); $job->execute($this->jobList, $this->logger);
$this->jobList->setLastJob($job); $this->jobList->setLastJob($job);
@ -110,15 +97,7 @@ class Job extends Command {
} }
protected function printJobInfo(int $jobId, IJob $job, OutputInterface$output): void { protected function printJobInfo(int $jobId, IJob $job, OutputInterface$output): void {
$row = $this->jobList->getDetailsById($jobId);
$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();
$lastRun = new \DateTime(); $lastRun = new \DateTime();
$lastRun->setTimestamp((int) $row['last_run']); $lastRun->setTimestamp((int) $row['last_run']);

View File

@ -237,19 +237,29 @@ class JobList implements IJobList {
* @return IJob|null * @return IJob|null
*/ */
public function getById($id) { 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 = $this->connection->getQueryBuilder();
$query->select('*') $query->select('*')
->from('jobs') ->from('jobs')
->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$result = $query->execute(); $result = $query->executeQuery();
$row = $result->fetch(); $row = $result->fetch();
$result->closeCursor(); $result->closeCursor();
if ($row) { if ($row) {
return $this->buildJob($row); return $row;
} else {
return null;
} }
return null;
} }
/** /**
@ -331,4 +341,19 @@ class JobList implements IJobList {
->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT))); ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
$query->execute(); $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();
}
} }

View File

@ -98,6 +98,13 @@ interface IJobList {
*/ */
public function getById($id); 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 * set the job that was last ran to the current time
* *
@ -130,4 +137,12 @@ interface IJobList {
* @since 12.0.0 * @since 12.0.0
*/ */
public function setExecutionTime(IJob $job, $timeTaken); 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;
} }

View File

@ -117,6 +117,10 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
return null; return null;
} }
public function getDetailsById(int $id): ?array {
return null;
}
/** /**
* set the lastRun of $job to now * 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 setExecutionTime(IJob $job, $timeTaken) {
} }
public function resetBackgroundJob(IJob $job): void {
}
} }