use OCP\AutoloadNotAllowedException;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
+use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
->where($query->expr()->eq('id', $query->createNamedParameter($job->getId()), IQueryBuilder::PARAM_INT));
$query->executeStatement();
}
+
+ public function hasReservedJob(?string $className): bool {
+ $query = $this->connection->getQueryBuilder();
+ $query->select('*')
+ ->from('jobs')
+ ->where($query->expr()->neq('reserved_at', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
+
+ if ($className !== null) {
+ $query->andWhere($query->expr()->eq('class', $query->createNamedParameter($className)));
+ }
+
+ try {
+ return $query->executeQuery()->rowCount() > 0;
+ } catch (Exception $e) {
+ return false;
+ }
+ }
}
* @since 23.0.0
*/
public function resetBackgroundJob(IJob $job): void;
+
+ /**
+ * Checks whether a job of the passed class is reserved to run
+ *
+ * @param string|null $className
+ * @return bool
+ * @since 27.0.0
+ */
+ public function hasReservedJob(?string $className): bool;
}
protected int $lastRun = 0;
protected $argument;
protected ITimeFactory $time;
+ protected bool $allowParallelRuns = true;
/**
* @since 15.0.0
$jobList->setLastRun($this);
$logger = \OCP\Server::get(LoggerInterface::class);
+ if (!$this->getAllowParallelRuns() && $jobList->hasReservedJob(get_class($this))) {
+ $logger->debug('Skipping ' . get_class($this) . ' job with ID ' . $this->getId() . ' because another job with the same class is already running', ['app' => 'cron']);
+ return;
+ }
+
try {
$jobStartTime = $this->time->getTime();
$logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']);
return $this->argument;
}
+ /**
+ * Set this to false to prevent two Jobs from this class from running in parallel
+ *
+ * @param bool $allow
+ * @return void
+ * @since 27.0.0
+ */
+ public function setAllowParallelRuns(bool $allow) {
+ $this->allowParallelRuns = $allow;
+ }
+
+ /**
+ * @return bool
+ * @since 27.0.0
+ */
+ public function getAllowParallelRuns(): bool {
+ return $this->allowParallelRuns;
+ }
+
/**
* The actual function that is called to run the job
*