DummyJobList.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 $jobs = [];
  20. private $last = 0;
  21. public function __construct() {
  22. }
  23. /**
  24. * @param IJob|string $job
  25. * @param mixed $argument
  26. */
  27. public function add($job, $argument = null) {
  28. if (is_string($job)) {
  29. /** @var \OC\BackgroundJob\Job $job */
  30. $job = new $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) {
  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) {
  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() {
  63. return $this->jobs;
  64. }
  65. /**
  66. * get the next job in the list
  67. *
  68. * @param bool $onlyTimeSensitive
  69. * @return IJob|null
  70. */
  71. public function getNext(bool $onlyTimeSensitive = false): ?IJob {
  72. if (count($this->jobs) > 0) {
  73. if ($this->last < (count($this->jobs) - 1)) {
  74. $i = $this->last + 1;
  75. } else {
  76. $i = 0;
  77. }
  78. return $this->jobs[$i];
  79. } else {
  80. return null;
  81. }
  82. }
  83. /**
  84. * set the job that was last ran
  85. *
  86. * @param \OC\BackgroundJob\Job $job
  87. */
  88. public function setLastJob(IJob $job) {
  89. $i = array_search($job, $this->jobs);
  90. if ($i !== false) {
  91. $this->last = $i;
  92. } else {
  93. $this->last = 0;
  94. }
  95. }
  96. /**
  97. * @param int $id
  98. * @return IJob
  99. */
  100. public function getById($id) {
  101. foreach ($this->jobs as $job) {
  102. if ($job->getId() === $id) {
  103. return $job;
  104. }
  105. }
  106. return null;
  107. }
  108. /**
  109. * set the lastRun of $job to now
  110. *
  111. * @param IJob $job
  112. */
  113. public function setLastRun(IJob $job) {
  114. $job->setLastRun(time());
  115. }
  116. public function setExecutionTime(IJob $job, $timeTaken) {
  117. }
  118. }