DummyJobList.php 2.6 KB

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