joblist.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }