DummyJobList.php 3.2 KB

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