PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/backgroundjob/joblist.php

https://github.com/sezuan/core
PHP | 172 lines | 94 code | 11 blank | 67 comment | 16 complexity | 210de7871a3fc4cfb1b0bd5cec1adb3d MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\BackgroundJob;
  9. /**
  10. * Class QueuedJob
  11. *
  12. * create a background job that is to be executed once
  13. *
  14. * @package OC\BackgroundJob
  15. */
  16. class JobList {
  17. /**
  18. * @param Job|string $job
  19. * @param mixed $argument
  20. */
  21. public function add($job, $argument = null) {
  22. if (!$this->has($job, $argument)) {
  23. if ($job instanceof Job) {
  24. $class = get_class($job);
  25. } else {
  26. $class = $job;
  27. }
  28. $argument = json_encode($argument);
  29. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*jobs`(`class`, `argument`, `last_run`) VALUES(?, ?, 0)');
  30. $query->execute(array($class, $argument));
  31. }
  32. }
  33. /**
  34. * @param Job|string $job
  35. * @param mixed $argument
  36. */
  37. public function remove($job, $argument = null) {
  38. if ($job instanceof Job) {
  39. $class = get_class($job);
  40. } else {
  41. $class = $job;
  42. }
  43. if (!is_null($argument)) {
  44. $argument = json_encode($argument);
  45. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?');
  46. $query->execute(array($class, $argument));
  47. } else {
  48. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*jobs` WHERE `class` = ?');
  49. $query->execute(array($class));
  50. }
  51. }
  52. /**
  53. * check if a job is in the list
  54. *
  55. * @param $job
  56. * @param mixed $argument
  57. * @return bool
  58. */
  59. public function has($job, $argument) {
  60. if ($job instanceof Job) {
  61. $class = get_class($job);
  62. } else {
  63. $class = $job;
  64. }
  65. $argument = json_encode($argument);
  66. $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*jobs` WHERE `class` = ? AND `argument` = ?');
  67. $result = $query->execute(array($class, $argument));
  68. return (bool)$result->fetchRow();
  69. }
  70. /**
  71. * get all jobs in the list
  72. *
  73. * @return Job[]
  74. */
  75. public function getAll() {
  76. $query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs`');
  77. $result = $query->execute();
  78. $jobs = array();
  79. while ($row = $result->fetchRow()) {
  80. $jobs[] = $this->buildJob($row);
  81. }
  82. return $jobs;
  83. }
  84. /**
  85. * get the next job in the list
  86. *
  87. * @return Job
  88. */
  89. public function getNext() {
  90. $lastId = $this->getLastJob();
  91. $query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` > ? ORDER BY `id` ASC', 1);
  92. $result = $query->execute(array($lastId));
  93. if ($row = $result->fetchRow()) {
  94. return $this->buildJob($row);
  95. } else {
  96. //begin at the start of the queue
  97. $query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` ORDER BY `id` ASC', 1);
  98. $result = $query->execute();
  99. if ($row = $result->fetchRow()) {
  100. return $this->buildJob($row);
  101. } else {
  102. return null; //empty job list
  103. }
  104. }
  105. }
  106. /**
  107. * @param int $id
  108. * @return Job
  109. */
  110. public function getById($id) {
  111. $query = \OC_DB::prepare('SELECT `id`, `class`, `last_run`, `argument` FROM `*PREFIX*jobs` WHERE `id` = ?');
  112. $result = $query->execute(array($id));
  113. if ($row = $result->fetchRow()) {
  114. return $this->buildJob($row);
  115. } else {
  116. return null;
  117. }
  118. }
  119. /**
  120. * get the job object from a row in the db
  121. *
  122. * @param array $row
  123. * @return Job
  124. */
  125. private function buildJob($row) {
  126. $class = $row['class'];
  127. /**
  128. * @var Job $job
  129. */
  130. $job = new $class();
  131. $job->setId($row['id']);
  132. $job->setLastRun($row['last_run']);
  133. $job->setArgument(json_decode($row['argument']));
  134. return $job;
  135. }
  136. /**
  137. * set the job that was last ran
  138. *
  139. * @param Job $job
  140. */
  141. public function setLastJob($job) {
  142. \OC_Appconfig::setValue('backgroundjob', 'lastjob', $job->getId());
  143. }
  144. /**
  145. * get the id of the last ran job
  146. *
  147. * @return int
  148. */
  149. public function getLastJob() {
  150. return \OC_Appconfig::getValue('backgroundjob', 'lastjob', 0);
  151. }
  152. /**
  153. * set the lastRun of $job to now
  154. *
  155. * @param Job $job
  156. */
  157. public function setLastRun($job) {
  158. $query = \OC_DB::prepare('UPDATE `*PREFIX*jobs` SET `last_run` = ? WHERE `id` = ?');
  159. $query->execute(array(time(), $job->getId()));
  160. }
  161. }