1
0

JobList.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Noveen Sachdeva <noveen.sachdeva@research.iiit.ac.in>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\BackgroundJob;
  31. use Doctrine\DBAL\Platforms\MySQLPlatform;
  32. use OCP\AppFramework\QueryException;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\AutoloadNotAllowedException;
  35. use OCP\BackgroundJob\IJob;
  36. use OCP\BackgroundJob\IJobList;
  37. use OCP\BackgroundJob\IParallelAwareJob;
  38. use OCP\DB\Exception;
  39. use OCP\DB\QueryBuilder\IQueryBuilder;
  40. use OCP\IConfig;
  41. use OCP\IDBConnection;
  42. use Psr\Log\LoggerInterface;
  43. use function get_class;
  44. use function json_encode;
  45. use function md5;
  46. use function strlen;
  47. class JobList implements IJobList {
  48. protected IDBConnection $connection;
  49. protected IConfig $config;
  50. protected ITimeFactory $timeFactory;
  51. protected LoggerInterface $logger;
  52. public function __construct(IDBConnection $connection, IConfig $config, ITimeFactory $timeFactory, LoggerInterface $logger) {
  53. $this->connection = $connection;
  54. $this->config = $config;
  55. $this->timeFactory = $timeFactory;
  56. $this->logger = $logger;
  57. }
  58. public function add($job, $argument = null, int $firstCheck = null): void {
  59. if ($firstCheck === null) {
  60. $firstCheck = $this->timeFactory->getTime();
  61. }
  62. if ($job instanceof IJob) {
  63. $class = get_class($job);
  64. } else {
  65. $class = $job;
  66. }
  67. $argumentJson = json_encode($argument);
  68. if (strlen($argumentJson) > 4000) {
  69. throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)');
  70. }
  71. $query = $this->connection->getQueryBuilder();
  72. if (!$this->has($job, $argument)) {
  73. $query->insert('jobs')
  74. ->values([
  75. 'class' => $query->createNamedParameter($class),
  76. 'argument' => $query->createNamedParameter($argumentJson),
  77. 'argument_hash' => $query->createNamedParameter(md5($argumentJson)),
  78. 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  79. 'last_checked' => $query->createNamedParameter($firstCheck, IQueryBuilder::PARAM_INT),
  80. ]);
  81. } else {
  82. $query->update('jobs')
  83. ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
  84. ->set('last_checked', $query->createNamedParameter($firstCheck, IQueryBuilder::PARAM_INT))
  85. ->set('last_run', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))
  86. ->where($query->expr()->eq('class', $query->createNamedParameter($class)))
  87. ->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argumentJson))));
  88. }
  89. $query->executeStatement();
  90. }
  91. public function scheduleAfter(string $job, int $runAfter, $argument = null): void {
  92. $this->add($job, $argument, $runAfter);
  93. }
  94. /**
  95. * @param IJob|string $job
  96. * @param mixed $argument
  97. */
  98. public function remove($job, $argument = null): void {
  99. if ($job instanceof IJob) {
  100. $class = get_class($job);
  101. } else {
  102. $class = $job;
  103. }
  104. $query = $this->connection->getQueryBuilder();
  105. $query->delete('jobs')
  106. ->where($query->expr()->eq('class', $query->createNamedParameter($class)));
  107. if (!is_null($argument)) {
  108. $argumentJson = json_encode($argument);
  109. $query->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argumentJson))));
  110. }
  111. // Add galera safe delete chunking if using mysql
  112. // Stops us hitting wsrep_max_ws_rows when large row counts are deleted
  113. if ($this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
  114. // Then use chunked delete
  115. $max = IQueryBuilder::MAX_ROW_DELETION;
  116. $query->setMaxResults($max);
  117. do {
  118. $deleted = $query->execute();
  119. } while ($deleted === $max);
  120. } else {
  121. // Dont use chunked delete - let the DB handle the large row count natively
  122. $query->execute();
  123. }
  124. }
  125. protected function removeById(int $id): void {
  126. $query = $this->connection->getQueryBuilder();
  127. $query->delete('jobs')
  128. ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  129. $query->executeStatement();
  130. }
  131. /**
  132. * check if a job is in the list
  133. *
  134. * @param IJob|class-string<IJob> $job
  135. * @param mixed $argument
  136. */
  137. public function has($job, $argument): bool {
  138. if ($job instanceof IJob) {
  139. $class = get_class($job);
  140. } else {
  141. $class = $job;
  142. }
  143. $argument = json_encode($argument);
  144. $query = $this->connection->getQueryBuilder();
  145. $query->select('id')
  146. ->from('jobs')
  147. ->where($query->expr()->eq('class', $query->createNamedParameter($class)))
  148. ->andWhere($query->expr()->eq('argument_hash', $query->createNamedParameter(md5($argument))))
  149. ->setMaxResults(1);
  150. $result = $query->executeQuery();
  151. $row = $result->fetch();
  152. $result->closeCursor();
  153. return (bool) $row;
  154. }
  155. public function getJobs($job, ?int $limit, int $offset): array {
  156. $iterable = $this->getJobsIterator($job, $limit, $offset);
  157. if (is_array($iterable)) {
  158. return $iterable;
  159. } else {
  160. return iterator_to_array($iterable);
  161. }
  162. }
  163. /**
  164. * @param IJob|class-string<IJob>|null $job
  165. * @return iterable<IJob> Avoid to store these objects as they may share a Singleton instance. You should instead use these IJobs instances while looping on the iterable.
  166. */
  167. public function getJobsIterator($job, ?int $limit, int $offset): iterable {
  168. $query = $this->connection->getQueryBuilder();
  169. $query->select('*')
  170. ->from('jobs')
  171. ->setMaxResults($limit)
  172. ->setFirstResult($offset);
  173. if ($job !== null) {
  174. if ($job instanceof IJob) {
  175. $class = get_class($job);
  176. } else {
  177. $class = $job;
  178. }
  179. $query->where($query->expr()->eq('class', $query->createNamedParameter($class)));
  180. }
  181. $result = $query->executeQuery();
  182. while ($row = $result->fetch()) {
  183. $job = $this->buildJob($row);
  184. if ($job) {
  185. yield $job;
  186. }
  187. }
  188. $result->closeCursor();
  189. }
  190. /**
  191. * Get the next job in the list
  192. * @return ?IJob the next job to run. Beware that this object may be a singleton and may be modified by the next call to buildJob.
  193. */
  194. public function getNext(bool $onlyTimeSensitive = false): ?IJob {
  195. $query = $this->connection->getQueryBuilder();
  196. $query->select('*')
  197. ->from('jobs')
  198. ->where($query->expr()->lte('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 12 * 3600, IQueryBuilder::PARAM_INT)))
  199. ->andWhere($query->expr()->lte('last_checked', $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT)))
  200. ->orderBy('last_checked', 'ASC')
  201. ->setMaxResults(1);
  202. if ($onlyTimeSensitive) {
  203. $query->andWhere($query->expr()->eq('time_sensitive', $query->createNamedParameter(IJob::TIME_SENSITIVE, IQueryBuilder::PARAM_INT)));
  204. }
  205. $result = $query->executeQuery();
  206. $row = $result->fetch();
  207. $result->closeCursor();
  208. if ($row) {
  209. $job = $this->buildJob($row);
  210. if ($job instanceof IParallelAwareJob && !$job->getAllowParallelRuns() && $this->hasReservedJob(get_class($job))) {
  211. $this->logger->debug('Skipping ' . get_class($job) . ' job with ID ' . $job->getId() . ' because another job with the same class is already running', ['app' => 'cron']);
  212. $update = $this->connection->getQueryBuilder();
  213. $update->update('jobs')
  214. ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime() + 1))
  215. ->where($update->expr()->eq('id', $update->createParameter('jobid')));
  216. $update->setParameter('jobid', $row['id']);
  217. $update->executeStatement();
  218. return $this->getNext($onlyTimeSensitive);
  219. }
  220. $update = $this->connection->getQueryBuilder();
  221. $update->update('jobs')
  222. ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
  223. ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime()))
  224. ->where($update->expr()->eq('id', $update->createParameter('jobid')))
  225. ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at')))
  226. ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked')));
  227. $update->setParameter('jobid', $row['id']);
  228. $update->setParameter('reserved_at', $row['reserved_at']);
  229. $update->setParameter('last_checked', $row['last_checked']);
  230. $count = $update->executeStatement();
  231. if ($count === 0) {
  232. // Background job already executed elsewhere, try again.
  233. return $this->getNext($onlyTimeSensitive);
  234. }
  235. if ($job === null) {
  236. // set the last_checked to 12h in the future to not check failing jobs all over again
  237. $reset = $this->connection->getQueryBuilder();
  238. $reset->update('jobs')
  239. ->set('reserved_at', $reset->expr()->literal(0, IQueryBuilder::PARAM_INT))
  240. ->set('last_checked', $reset->createNamedParameter($this->timeFactory->getTime() + 12 * 3600, IQueryBuilder::PARAM_INT))
  241. ->where($reset->expr()->eq('id', $reset->createNamedParameter($row['id'], IQueryBuilder::PARAM_INT)));
  242. $reset->executeStatement();
  243. // Background job from disabled app, try again.
  244. return $this->getNext($onlyTimeSensitive);
  245. }
  246. return $job;
  247. } else {
  248. return null;
  249. }
  250. }
  251. /**
  252. * @return ?IJob The job matching the id. Beware that this object may be a singleton and may be modified by the next call to buildJob.
  253. */
  254. public function getById(int $id): ?IJob {
  255. $row = $this->getDetailsById($id);
  256. if ($row) {
  257. return $this->buildJob($row);
  258. }
  259. return null;
  260. }
  261. public function getDetailsById(int $id): ?array {
  262. $query = $this->connection->getQueryBuilder();
  263. $query->select('*')
  264. ->from('jobs')
  265. ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  266. $result = $query->executeQuery();
  267. $row = $result->fetch();
  268. $result->closeCursor();
  269. if ($row) {
  270. return $row;
  271. }
  272. return null;
  273. }
  274. /**
  275. * get the job object from a row in the db
  276. *
  277. * @param array{class:class-string<IJob>, id:mixed, last_run:mixed, argument:string} $row
  278. * @return ?IJob the next job to run. Beware that this object may be a singleton and may be modified by the next call to buildJob.
  279. */
  280. private function buildJob(array $row): ?IJob {
  281. try {
  282. try {
  283. // Try to load the job as a service
  284. /** @var IJob $job */
  285. $job = \OCP\Server::get($row['class']);
  286. } catch (QueryException $e) {
  287. if (class_exists($row['class'])) {
  288. $class = $row['class'];
  289. $job = new $class();
  290. } else {
  291. // Remove job from disabled app or old version of an app
  292. $this->removeById($row['id']);
  293. return null;
  294. }
  295. }
  296. if (!($job instanceof IJob)) {
  297. // This most likely means an invalid job was enqueued. We can ignore it.
  298. return null;
  299. }
  300. $job->setId((int) $row['id']);
  301. $job->setLastRun((int) $row['last_run']);
  302. $job->setArgument(json_decode($row['argument'], true));
  303. return $job;
  304. } catch (AutoloadNotAllowedException $e) {
  305. // job is from a disabled app, ignore
  306. return null;
  307. }
  308. }
  309. /**
  310. * set the job that was last ran
  311. */
  312. public function setLastJob(IJob $job): void {
  313. $this->unlockJob($job);
  314. $this->config->setAppValue('backgroundjob', 'lastjob', (string)$job->getId());
  315. }
  316. /**
  317. * Remove the reservation for a job
  318. */
  319. public function unlockJob(IJob $job): void {
  320. $query = $this->connection->getQueryBuilder();
  321. $query->update('jobs')
  322. ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
  323. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  324. $query->executeStatement();
  325. }
  326. /**
  327. * set the lastRun of $job to now
  328. */
  329. public function setLastRun(IJob $job): void {
  330. $query = $this->connection->getQueryBuilder();
  331. $query->update('jobs')
  332. ->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
  333. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  334. if ($job instanceof \OCP\BackgroundJob\TimedJob
  335. && !$job->isTimeSensitive()) {
  336. $query->set('time_sensitive', $query->createNamedParameter(IJob::TIME_INSENSITIVE));
  337. }
  338. $query->executeStatement();
  339. }
  340. /**
  341. * @param int $timeTaken
  342. */
  343. public function setExecutionTime(IJob $job, $timeTaken): void {
  344. $query = $this->connection->getQueryBuilder();
  345. $query->update('jobs')
  346. ->set('execution_duration', $query->createNamedParameter($timeTaken, IQueryBuilder::PARAM_INT))
  347. ->set('reserved_at', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))
  348. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  349. $query->executeStatement();
  350. }
  351. /**
  352. * Reset the $job so it executes on the next trigger
  353. *
  354. * @since 23.0.0
  355. */
  356. public function resetBackgroundJob(IJob $job): void {
  357. $query = $this->connection->getQueryBuilder();
  358. $query->update('jobs')
  359. ->set('last_run', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))
  360. ->set('reserved_at', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))
  361. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId()), IQueryBuilder::PARAM_INT));
  362. $query->executeStatement();
  363. }
  364. public function hasReservedJob(?string $className = null): bool {
  365. $query = $this->connection->getQueryBuilder();
  366. $query->select('*')
  367. ->from('jobs')
  368. ->where($query->expr()->gt('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 6 * 3600, IQueryBuilder::PARAM_INT)))
  369. ->setMaxResults(1);
  370. if ($className !== null) {
  371. $query->andWhere($query->expr()->eq('class', $query->createNamedParameter($className)));
  372. }
  373. try {
  374. $result = $query->executeQuery();
  375. $hasReservedJobs = $result->fetch() !== false;
  376. $result->closeCursor();
  377. return $hasReservedJobs;
  378. } catch (Exception $e) {
  379. $this->logger->debug('Querying reserved jobs failed', ['exception' => $e]);
  380. return false;
  381. }
  382. }
  383. }