1
0

DummyJobList.php 3.5 KB

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