DummyJobList.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Test\BackgroundJob;
  9. use OCP\BackgroundJob\IJob;
  10. /**
  11. * Class DummyJobList
  12. *
  13. * in memory job list for testing purposes
  14. */
  15. class DummyJobList extends \OC\BackgroundJob\JobList {
  16. /**
  17. * @var IJob[]
  18. */
  19. private $jobs = [];
  20. private $last = 0;
  21. public function __construct() {
  22. }
  23. /**
  24. * @param IJob|string $job
  25. * @param mixed $argument
  26. */
  27. public function add($job, $argument = null) {
  28. if (is_string($job)) {
  29. /** @var \OC\BackgroundJob\Job $job */
  30. $job = new $job;
  31. }
  32. $job->setArgument($argument);
  33. if (!$this->has($job, null)) {
  34. $this->jobs[] = $job;
  35. }
  36. }
  37. /**
  38. * @param IJob|string $job
  39. * @param mixed $argument
  40. */
  41. public function remove($job, $argument = null) {
  42. $index = array_search($job, $this->jobs);
  43. if ($index !== false) {
  44. unset($this->jobs[$index]);
  45. }
  46. }
  47. /**
  48. * check if a job is in the list
  49. *
  50. * @param $job
  51. * @param mixed $argument
  52. * @return bool
  53. */
  54. public function has($job, $argument) {
  55. return array_search($job, $this->jobs) !== false;
  56. }
  57. /**
  58. * get all jobs in the list
  59. *
  60. * @return IJob[]
  61. */
  62. public function getAll() {
  63. return $this->jobs;
  64. }
  65. /**
  66. * get the next job in the list
  67. *
  68. * @return IJob|null
  69. */
  70. public function getNext() {
  71. if (count($this->jobs) > 0) {
  72. if ($this->last < (count($this->jobs) - 1)) {
  73. $i = $this->last + 1;
  74. } else {
  75. $i = 0;
  76. }
  77. return $this->jobs[$i];
  78. } else {
  79. return null;
  80. }
  81. }
  82. /**
  83. * set the job that was last ran
  84. *
  85. * @param \OC\BackgroundJob\Job $job
  86. */
  87. public function setLastJob(IJob $job) {
  88. $i = array_search($job, $this->jobs);
  89. if ($i !== false) {
  90. $this->last = $i;
  91. } else {
  92. $this->last = 0;
  93. }
  94. }
  95. /**
  96. * @param int $id
  97. * @return IJob
  98. */
  99. public function getById($id) {
  100. foreach ($this->jobs as $job) {
  101. if ($job->getId() === $id) {
  102. return $job;
  103. }
  104. }
  105. return null;
  106. }
  107. /**
  108. * set the lastRun of $job to now
  109. *
  110. * @param IJob $job
  111. */
  112. public function setLastRun(IJob $job) {
  113. $job->setLastRun(time());
  114. }
  115. public function setExecutionTime(IJob $job, $timeTaken) {
  116. }
  117. }