DummyJobList.php 3.0 KB

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