JobList.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\BackgroundJob;
  26. use OCP\AppFramework\QueryException;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\BackgroundJob\IJob;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\AutoloadNotAllowedException;
  31. use OCP\DB\QueryBuilder\IQueryBuilder;
  32. use OCP\IConfig;
  33. use OCP\IDBConnection;
  34. class JobList implements IJobList {
  35. /** @var IDBConnection */
  36. protected $connection;
  37. /**@var IConfig */
  38. protected $config;
  39. /**@var ITimeFactory */
  40. protected $timeFactory;
  41. /**
  42. * @param IDBConnection $connection
  43. * @param IConfig $config
  44. * @param ITimeFactory $timeFactory
  45. */
  46. public function __construct(IDBConnection $connection, IConfig $config, ITimeFactory $timeFactory) {
  47. $this->connection = $connection;
  48. $this->config = $config;
  49. $this->timeFactory = $timeFactory;
  50. }
  51. /**
  52. * @param IJob|string $job
  53. * @param mixed $argument
  54. */
  55. public function add($job, $argument = null) {
  56. if (!$this->has($job, $argument)) {
  57. if ($job instanceof IJob) {
  58. $class = get_class($job);
  59. } else {
  60. $class = $job;
  61. }
  62. $argument = json_encode($argument);
  63. if (strlen($argument) > 4000) {
  64. throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)');
  65. }
  66. $query = $this->connection->getQueryBuilder();
  67. $query->insert('jobs')
  68. ->values([
  69. 'class' => $query->createNamedParameter($class),
  70. 'argument' => $query->createNamedParameter($argument),
  71. 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  72. 'last_checked' => $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT),
  73. ]);
  74. $query->execute();
  75. }
  76. }
  77. /**
  78. * @param IJob|string $job
  79. * @param mixed $argument
  80. */
  81. public function remove($job, $argument = null) {
  82. if ($job instanceof IJob) {
  83. $class = get_class($job);
  84. } else {
  85. $class = $job;
  86. }
  87. $query = $this->connection->getQueryBuilder();
  88. $query->delete('jobs')
  89. ->where($query->expr()->eq('class', $query->createNamedParameter($class)));
  90. if (!is_null($argument)) {
  91. $argument = json_encode($argument);
  92. $query->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)));
  93. }
  94. $query->execute();
  95. }
  96. /**
  97. * @param int $id
  98. */
  99. protected function removeById($id) {
  100. $query = $this->connection->getQueryBuilder();
  101. $query->delete('jobs')
  102. ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  103. $query->execute();
  104. }
  105. /**
  106. * check if a job is in the list
  107. *
  108. * @param IJob|string $job
  109. * @param mixed $argument
  110. * @return bool
  111. */
  112. public function has($job, $argument) {
  113. if ($job instanceof IJob) {
  114. $class = get_class($job);
  115. } else {
  116. $class = $job;
  117. }
  118. $argument = json_encode($argument);
  119. $query = $this->connection->getQueryBuilder();
  120. $query->select('id')
  121. ->from('jobs')
  122. ->where($query->expr()->eq('class', $query->createNamedParameter($class)))
  123. ->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)))
  124. ->setMaxResults(1);
  125. $result = $query->execute();
  126. $row = $result->fetch();
  127. $result->closeCursor();
  128. return (bool) $row;
  129. }
  130. /**
  131. * get all jobs in the list
  132. *
  133. * @return IJob[]
  134. * @deprecated 9.0.0 - This method is dangerous since it can cause load and
  135. * memory problems when creating too many instances.
  136. */
  137. public function getAll() {
  138. $query = $this->connection->getQueryBuilder();
  139. $query->select('*')
  140. ->from('jobs');
  141. $result = $query->execute();
  142. $jobs = [];
  143. while ($row = $result->fetch()) {
  144. $job = $this->buildJob($row);
  145. if ($job) {
  146. $jobs[] = $job;
  147. }
  148. }
  149. $result->closeCursor();
  150. return $jobs;
  151. }
  152. /**
  153. * get the next job in the list
  154. *
  155. * @return IJob|null
  156. */
  157. public function getNext() {
  158. $query = $this->connection->getQueryBuilder();
  159. $query->select('*')
  160. ->from('jobs')
  161. ->where($query->expr()->lte('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 12 * 3600, IQueryBuilder::PARAM_INT)))
  162. ->orderBy('last_checked', 'ASC')
  163. ->setMaxResults(1);
  164. $update = $this->connection->getQueryBuilder();
  165. $update->update('jobs')
  166. ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
  167. ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime()))
  168. ->where($update->expr()->eq('id', $update->createParameter('jobid')));
  169. $this->connection->lockTable('jobs');
  170. $result = $query->execute();
  171. $row = $result->fetch();
  172. $result->closeCursor();
  173. if ($row) {
  174. $update->setParameter('jobid', $row['id']);
  175. $update->execute();
  176. $this->connection->unlockTable();
  177. $job = $this->buildJob($row);
  178. if ($job === null) {
  179. // Background job from disabled app, try again.
  180. return $this->getNext();
  181. }
  182. return $job;
  183. } else {
  184. $this->connection->unlockTable();
  185. return null;
  186. }
  187. }
  188. /**
  189. * @param int $id
  190. * @return IJob|null
  191. */
  192. public function getById($id) {
  193. $query = $this->connection->getQueryBuilder();
  194. $query->select('*')
  195. ->from('jobs')
  196. ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  197. $result = $query->execute();
  198. $row = $result->fetch();
  199. $result->closeCursor();
  200. if ($row) {
  201. return $this->buildJob($row);
  202. } else {
  203. return null;
  204. }
  205. }
  206. /**
  207. * get the job object from a row in the db
  208. *
  209. * @param array $row
  210. * @return IJob|null
  211. */
  212. private function buildJob($row) {
  213. try {
  214. try {
  215. // Try to load the job as a service
  216. /** @var IJob $job */
  217. $job = \OC::$server->query($row['class']);
  218. } catch (QueryException $e) {
  219. if (class_exists($row['class'])) {
  220. $class = $row['class'];
  221. $job = new $class();
  222. } else {
  223. // job from disabled app or old version of an app, no need to do anything
  224. return null;
  225. }
  226. }
  227. $job->setId($row['id']);
  228. $job->setLastRun($row['last_run']);
  229. $job->setArgument(json_decode($row['argument'], true));
  230. return $job;
  231. } catch (AutoloadNotAllowedException $e) {
  232. // job is from a disabled app, ignore
  233. return null;
  234. }
  235. }
  236. /**
  237. * set the job that was last ran
  238. *
  239. * @param IJob $job
  240. */
  241. public function setLastJob($job) {
  242. $this->unlockJob($job);
  243. $this->config->setAppValue('backgroundjob', 'lastjob', $job->getId());
  244. }
  245. /**
  246. * Remove the reservation for a job
  247. *
  248. * @param IJob $job
  249. */
  250. public function unlockJob($job) {
  251. $query = $this->connection->getQueryBuilder();
  252. $query->update('jobs')
  253. ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
  254. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  255. $query->execute();
  256. }
  257. /**
  258. * get the id of the last ran job
  259. *
  260. * @return int
  261. * @deprecated 9.1.0 - The functionality behind the value is deprecated, it
  262. * only tells you which job finished last, but since we now allow multiple
  263. * executors to run in parallel, it's not used to calculate the next job.
  264. */
  265. public function getLastJob() {
  266. return (int) $this->config->getAppValue('backgroundjob', 'lastjob', 0);
  267. }
  268. /**
  269. * set the lastRun of $job to now
  270. *
  271. * @param IJob $job
  272. */
  273. public function setLastRun($job) {
  274. $query = $this->connection->getQueryBuilder();
  275. $query->update('jobs')
  276. ->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
  277. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  278. $query->execute();
  279. }
  280. }