DummyJobList.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 array $jobs = [];
  20. /**
  21. * @var bool[]
  22. */
  23. private array $reserved = [];
  24. private int $last = 0;
  25. private int $lastId = 0;
  26. public function __construct() {
  27. }
  28. /**
  29. * @param IJob|class-string<IJob> $job
  30. * @param mixed $argument
  31. */
  32. public function add($job, $argument = null, ?int $firstCheck = null): void {
  33. if (is_string($job)) {
  34. /** @var IJob $job */
  35. $job = \OCP\Server::get($job);
  36. }
  37. $job->setArgument($argument);
  38. $job->setId($this->lastId);
  39. $this->lastId++;
  40. if (!$this->has($job, null)) {
  41. $this->jobs[] = $job;
  42. }
  43. }
  44. public function scheduleAfter(string $job, int $runAfter, $argument = null): void {
  45. $this->add($job, $argument, $runAfter);
  46. }
  47. /**
  48. * @param IJob|string $job
  49. * @param mixed $argument
  50. */
  51. public function remove($job, $argument = null): void {
  52. foreach ($this->jobs as $index => $listJob) {
  53. if (get_class($job) === get_class($listJob) && $job->getArgument() == $listJob->getArgument()) {
  54. unset($this->jobs[$index]);
  55. return;
  56. }
  57. }
  58. }
  59. public function removeById(int $id): void {
  60. foreach ($this->jobs as $index => $listJob) {
  61. if ($listJob->getId() === $id) {
  62. unset($this->jobs[$index]);
  63. return;
  64. }
  65. }
  66. }
  67. /**
  68. * check if a job is in the list
  69. *
  70. * @param $job
  71. * @param mixed $argument
  72. * @return bool
  73. */
  74. public function has($job, $argument): bool {
  75. return array_search($job, $this->jobs) !== false;
  76. }
  77. /**
  78. * get all jobs in the list
  79. *
  80. * @return IJob[]
  81. */
  82. public function getAll(): array {
  83. return $this->jobs;
  84. }
  85. public function getJobsIterator($job, ?int $limit, int $offset): array {
  86. if ($job instanceof IJob) {
  87. $jobClass = get_class($job);
  88. } else {
  89. $jobClass = $job;
  90. }
  91. return array_slice(
  92. array_filter(
  93. $this->jobs,
  94. fn ($job) => ($jobClass === null) || (get_class($job) == $jobClass)
  95. ),
  96. $offset,
  97. $limit
  98. );
  99. }
  100. /**
  101. * get the next job in the list
  102. */
  103. public function getNext(bool $onlyTimeSensitive = false): ?IJob {
  104. if (count($this->jobs) > 0) {
  105. if ($this->last < (count($this->jobs) - 1)) {
  106. $i = $this->last + 1;
  107. } else {
  108. $i = 0;
  109. }
  110. return $this->jobs[$i];
  111. } else {
  112. return null;
  113. }
  114. }
  115. /**
  116. * set the job that was last ran
  117. *
  118. * @param \OCP\BackgroundJob\Job $job
  119. */
  120. public function setLastJob(IJob $job): void {
  121. $i = array_search($job, $this->jobs);
  122. if ($i !== false) {
  123. $this->last = $i;
  124. } else {
  125. $this->last = 0;
  126. }
  127. }
  128. public function getById(int $id): ?IJob {
  129. foreach ($this->jobs as $job) {
  130. if ($job->getId() === $id) {
  131. return $job;
  132. }
  133. }
  134. return null;
  135. }
  136. public function getDetailsById(int $id): ?array {
  137. return null;
  138. }
  139. public function setLastRun(IJob $job): void {
  140. $job->setLastRun(time());
  141. }
  142. public function hasReservedJob(?string $className = null): bool {
  143. return isset($this->reserved[$className ?? '']) && $this->reserved[$className ?? ''];
  144. }
  145. public function setHasReservedJob(?string $className, bool $hasReserved): void {
  146. $this->reserved[$className ?? ''] = $hasReserved;
  147. }
  148. public function setExecutionTime(IJob $job, $timeTaken): void {
  149. }
  150. public function resetBackgroundJob(IJob $job): void {
  151. }
  152. }