You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JobList.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. ->andWhere($update->expr()->eq('reserved_at', $update->createParameter('reserved_at')))
  170. ->andWhere($update->expr()->eq('last_checked', $update->createParameter('last_checked')));
  171. $result = $query->execute();
  172. $row = $result->fetch();
  173. $result->closeCursor();
  174. if ($row) {
  175. $update->setParameter('jobid', $row['id']);
  176. $update->setParameter('reserved_at', $row['reserved_at']);
  177. $update->setParameter('last_checked', $row['last_checked']);
  178. $count = $update->execute();
  179. if ($count === 0) {
  180. // Background job already executed elsewhere, try again.
  181. return $this->getNext();
  182. }
  183. $job = $this->buildJob($row);
  184. if ($job === null) {
  185. // Background job from disabled app, try again.
  186. return $this->getNext();
  187. }
  188. return $job;
  189. } else {
  190. return null;
  191. }
  192. }
  193. /**
  194. * @param int $id
  195. * @return IJob|null
  196. */
  197. public function getById($id) {
  198. $query = $this->connection->getQueryBuilder();
  199. $query->select('*')
  200. ->from('jobs')
  201. ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  202. $result = $query->execute();
  203. $row = $result->fetch();
  204. $result->closeCursor();
  205. if ($row) {
  206. return $this->buildJob($row);
  207. } else {
  208. return null;
  209. }
  210. }
  211. /**
  212. * get the job object from a row in the db
  213. *
  214. * @param array $row
  215. * @return IJob|null
  216. */
  217. private function buildJob($row) {
  218. try {
  219. try {
  220. // Try to load the job as a service
  221. /** @var IJob $job */
  222. $job = \OC::$server->query($row['class']);
  223. } catch (QueryException $e) {
  224. if (class_exists($row['class'])) {
  225. $class = $row['class'];
  226. $job = new $class();
  227. } else {
  228. // job from disabled app or old version of an app, no need to do anything
  229. return null;
  230. }
  231. }
  232. $job->setId($row['id']);
  233. $job->setLastRun($row['last_run']);
  234. $job->setArgument(json_decode($row['argument'], true));
  235. return $job;
  236. } catch (AutoloadNotAllowedException $e) {
  237. // job is from a disabled app, ignore
  238. return null;
  239. }
  240. }
  241. /**
  242. * set the job that was last ran
  243. *
  244. * @param IJob $job
  245. */
  246. public function setLastJob(IJob $job) {
  247. $this->unlockJob($job);
  248. $this->config->setAppValue('backgroundjob', 'lastjob', $job->getId());
  249. }
  250. /**
  251. * Remove the reservation for a job
  252. *
  253. * @param IJob $job
  254. */
  255. public function unlockJob(IJob $job) {
  256. $query = $this->connection->getQueryBuilder();
  257. $query->update('jobs')
  258. ->set('reserved_at', $query->expr()->literal(0, IQueryBuilder::PARAM_INT))
  259. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  260. $query->execute();
  261. }
  262. /**
  263. * get the id of the last ran job
  264. *
  265. * @return int
  266. * @deprecated 9.1.0 - The functionality behind the value is deprecated, it
  267. * only tells you which job finished last, but since we now allow multiple
  268. * executors to run in parallel, it's not used to calculate the next job.
  269. */
  270. public function getLastJob() {
  271. return (int) $this->config->getAppValue('backgroundjob', 'lastjob', 0);
  272. }
  273. /**
  274. * set the lastRun of $job to now
  275. *
  276. * @param IJob $job
  277. */
  278. public function setLastRun(IJob $job) {
  279. $query = $this->connection->getQueryBuilder();
  280. $query->update('jobs')
  281. ->set('last_run', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
  282. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  283. $query->execute();
  284. }
  285. /**
  286. * @param IJob $job
  287. * @param $timeTaken
  288. */
  289. public function setExecutionTime(IJob $job, $timeTaken) {
  290. $query = $this->connection->getQueryBuilder();
  291. $query->update('jobs')
  292. ->set('execution_duration', $query->createNamedParameter($timeTaken, IQueryBuilder::PARAM_INT))
  293. ->where($query->expr()->eq('id', $query->createNamedParameter($job->getId(), IQueryBuilder::PARAM_INT)));
  294. $query->execute();
  295. }
  296. }