JobList.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 OCP\AppFramework\QueryException;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\AutoloadNotAllowedException;
  34. use OCP\BackgroundJob\IJob;
  35. use OCP\BackgroundJob\IJobList;
  36. use OCP\DB\QueryBuilder\IQueryBuilder;
  37. use OCP\IConfig;
  38. use OCP\IDBConnection;
  39. class JobList implements IJobList {
  40. /** @var IDBConnection */
  41. protected $connection;
  42. /**@var IConfig */
  43. protected $config;
  44. /**@var ITimeFactory */
  45. protected $timeFactory;
  46. /**
  47. * @param IDBConnection $connection
  48. * @param IConfig $config
  49. * @param ITimeFactory $timeFactory
  50. */
  51. public function __construct(IDBConnection $connection, IConfig $config, ITimeFactory $timeFactory) {
  52. $this->connection = $connection;
  53. $this->config = $config;
  54. $this->timeFactory = $timeFactory;
  55. }
  56. /**
  57. * @param IJob|string $job
  58. * @param mixed $argument
  59. */
  60. public function add($job, $argument = null) {
  61. if (!$this->has($job, $argument)) {
  62. if ($job instanceof IJob) {
  63. $class = get_class($job);
  64. } else {
  65. $class = $job;
  66. }
  67. $argument = json_encode($argument);
  68. if (strlen($argument) > 4000) {
  69. throw new \InvalidArgumentException('Background job arguments can\'t exceed 4000 characters (json encoded)');
  70. }
  71. $query = $this->connection->getQueryBuilder();
  72. $query->insert('jobs')
  73. ->values([
  74. 'class' => $query->createNamedParameter($class),
  75. 'argument' => $query->createNamedParameter($argument),
  76. 'last_run' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  77. 'last_checked' => $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT),
  78. ]);
  79. $query->execute();
  80. }
  81. }
  82. /**
  83. * @param IJob|string $job
  84. * @param mixed $argument
  85. */
  86. public function remove($job, $argument = null) {
  87. if ($job instanceof IJob) {
  88. $class = get_class($job);
  89. } else {
  90. $class = $job;
  91. }
  92. $query = $this->connection->getQueryBuilder();
  93. $query->delete('jobs')
  94. ->where($query->expr()->eq('class', $query->createNamedParameter($class)));
  95. if (!is_null($argument)) {
  96. $argument = json_encode($argument);
  97. $query->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)));
  98. }
  99. $query->execute();
  100. }
  101. /**
  102. * @param int $id
  103. */
  104. protected function removeById($id) {
  105. $query = $this->connection->getQueryBuilder();
  106. $query->delete('jobs')
  107. ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  108. $query->execute();
  109. }
  110. /**
  111. * check if a job is in the list
  112. *
  113. * @param IJob|string $job
  114. * @param mixed $argument
  115. * @return bool
  116. */
  117. public function has($job, $argument) {
  118. if ($job instanceof IJob) {
  119. $class = get_class($job);
  120. } else {
  121. $class = $job;
  122. }
  123. $argument = json_encode($argument);
  124. $query = $this->connection->getQueryBuilder();
  125. $query->select('id')
  126. ->from('jobs')
  127. ->where($query->expr()->eq('class', $query->createNamedParameter($class)))
  128. ->andWhere($query->expr()->eq('argument', $query->createNamedParameter($argument)))
  129. ->setMaxResults(1);
  130. $result = $query->execute();
  131. $row = $result->fetch();
  132. $result->closeCursor();
  133. return (bool) $row;
  134. }
  135. /**
  136. * get all jobs in the list
  137. *
  138. * @return IJob[]
  139. * @deprecated 9.0.0 - This method is dangerous since it can cause load and
  140. * memory problems when creating too many instances.
  141. */
  142. public function getAll() {
  143. $query = $this->connection->getQueryBuilder();
  144. $query->select('*')
  145. ->from('jobs');
  146. $result = $query->execute();
  147. $jobs = [];
  148. while ($row = $result->fetch()) {
  149. $job = $this->buildJob($row);
  150. if ($job) {
  151. $jobs[] = $job;
  152. }
  153. }
  154. $result->closeCursor();
  155. return $jobs;
  156. }
  157. /**
  158. * get the next job in the list
  159. *
  160. * @return IJob|null
  161. */
  162. public function getNext() {
  163. $query = $this->connection->getQueryBuilder();
  164. $query->select('*')
  165. ->from('jobs')
  166. ->where($query->expr()->lte('reserved_at', $query->createNamedParameter($this->timeFactory->getTime() - 12 * 3600, IQueryBuilder::PARAM_INT)))
  167. ->andWhere($query->expr()->lte('last_checked', $query->createNamedParameter($this->timeFactory->getTime(), IQueryBuilder::PARAM_INT)))
  168. ->orderBy('last_checked', 'ASC')
  169. ->setMaxResults(1);
  170. $update = $this->connection->getQueryBuilder();
  171. $update->update('jobs')
  172. ->set('reserved_at', $update->createNamedParameter($this->timeFactory->getTime()))
  173. ->set('last_checked', $update->createNamedParameter($this->timeFactory->getTime()))
  174. ->where($update->expr()->eq('id', $update->createParameter('jobid')))
  175. ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at')))
  176. ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked')));
  177. $result = $query->execute();
  178. $row = $result->fetch();
  179. $result->closeCursor();
  180. if ($row) {
  181. $update->setParameter('jobid', $row['id']);
  182. $update->setParameter('reserved_at', $row['reserved_at']);
  183. $update->setParameter('last_checked', $row['last_checked']);
  184. $count = $update->execute();
  185. if ($count === 0) {
  186. // Background job already executed elsewhere, try again.
  187. return $this->getNext();
  188. }
  189. $job = $this->buildJob($row);
  190. if ($job === null) {
  191. // set the last_checked to 12h in the future to not check failing jobs all over again
  192. $reset = $this->connection->getQueryBuilder();
  193. $reset->update('jobs')
  194. ->set('reserved_at', $reset->expr()->literal(0, IQueryBuilder::PARAM_INT))
  195. ->set('last_checked', $reset->createNamedParameter($this->timeFactory->getTime() + 12 * 3600, IQueryBuilder::PARAM_INT))
  196. ->where($reset->expr()->eq('id', $reset->createNamedParameter($row['id'], IQueryBuilder::PARAM_INT)));
  197. $reset->execute();
  198. // Background job from disabled app, try again.
  199. return $this->getNext();
  200. }
  201. return $job;
  202. } else {
  203. return null;
  204. }
  205. }
  206. /**
  207. * @param int $id
  208. * @return IJob|null
  209. */
  210. public function getById($id) {
  211. $query = $this->connection->getQueryBuilder();
  212. $query->select('*')
  213. ->from('jobs')
  214. ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  215. $result = $query->execute();
  216. $row = $result->fetch();
  217. $result->closeCursor();
  218. if ($row) {
  219. return $this->buildJob($row);
  220. } else {
  221. return null;
  222. }
  223. }
  224. /**
  225. * get the job object from a row in the db
  226. *
  227. * @param array $row
  228. * @return IJob|null
  229. */
  230. private function buildJob($row) {
  231. try {
  232. try {
  233. // Try to load the job as a service
  234. /** @var IJob $job */
  235. $job = \OC::$server->query($row['class']);
  236. } catch (QueryException $e) {
  237. if (class_exists($row['class'])) {
  238. $class = $row['class'];
  239. $job = new $class();
  240. } else {
  241. // job from disabled app or old version of an app, no need to do anything
  242. return null;
  243. }
  244. }
  245. $job->setId((int) $row['id']);
  246. $job->setLastRun((int) $row['last_run']);
  247. $job->setArgument(json_decode($row['argument'], true));
  248. return $job;
  249. } catch (AutoloadNotAllowedException $e) {
  250. // job is from a disabled app, ignore
  251. return null;
  252. }
  253. }
  254. /**
  255. * set the job that was last ran
  256. *
  257. * @param IJob $job
  258. */
  259. public function setLastJob(IJob $job) {
  260. $this->unlockJob($job);
  261. $this->config->setAppValue('backgroundjob', 'lastjob', $job->getId());
  262. }
  263. /**
  264. * Remove the reservation for a job
  265. *
  266. * @param IJob $job
  267. */
  268. public function unlockJob(IJob $job) {
  269. $query = $this->connection->getQueryBuilder();
  270. $query->update('jobs')
  271. ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
  272. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  273. $query->execute();
  274. }
  275. /**
  276. * set the lastRun of $job to now
  277. *
  278. * @param IJob $job
  279. */
  280. public function setLastRun(IJob $job) {
  281. $query = $this->connection->getQueryBuilder();
  282. $query->update('jobs')
  283. ->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
  284. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  285. $query->execute();
  286. }
  287. /**
  288. * @param IJob $job
  289. * @param $timeTaken
  290. */
  291. public function setExecutionTime(IJob $job, $timeTaken) {
  292. $query = $this->connection->getQueryBuilder();
  293. $query->update('jobs')
  294. ->set('execution_duration', $query->createNamedParameter($timeTaken, IQueryBuilder::PARAM_INT))
  295. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  296. $query->execute();
  297. }
  298. }