DummyJobList.php 3.1 KB

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