1
0

TimedJob.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCP\BackgroundJob;
  26. use OCP\ILogger;
  27. /**
  28. * Simple base class to extend to run periodic background jobs.
  29. * Call setInterval with your desired interval in seconds from the constructor.
  30. *
  31. * @since 15.0.0
  32. */
  33. abstract class TimedJob extends Job {
  34. protected int $interval = 0;
  35. protected int $timeSensitivity = IJob::TIME_SENSITIVE;
  36. /**
  37. * Set the interval for the job
  38. *
  39. * @param int $seconds the time to pass between two runs of the same job in seconds
  40. *
  41. * @since 15.0.0
  42. */
  43. public function setInterval(int $seconds) {
  44. $this->interval = $seconds;
  45. }
  46. /**
  47. * Whether the background job is time sensitive and needs to run soon after
  48. * the scheduled interval, of if it is okay to be delayed until a later time.
  49. *
  50. * @return bool
  51. * @since 24.0.0
  52. */
  53. public function isTimeSensitive(): bool {
  54. return $this->timeSensitivity === IJob::TIME_SENSITIVE;
  55. }
  56. /**
  57. * If your background job is not time sensitive (sending instant email
  58. * notifications, etc.) it would be nice to set it to IJob::TIME_INSENSITIVE
  59. * This way the execution can be delayed during high usage times.
  60. *
  61. * @param int $sensitivity
  62. * @psalm-param IJob::TIME_* $sensitivity
  63. * @return void
  64. * @since 24.0.0
  65. */
  66. public function setTimeSensitivity(int $sensitivity): void {
  67. if ($sensitivity !== IJob::TIME_SENSITIVE &&
  68. $sensitivity !== IJob::TIME_INSENSITIVE) {
  69. throw new \InvalidArgumentException('Invalid sensitivity');
  70. }
  71. $this->timeSensitivity = $sensitivity;
  72. }
  73. /**
  74. * Run the job if the last run is more than the interval ago
  75. *
  76. * @param IJobList $jobList
  77. * @param ILogger|null $logger
  78. *
  79. * @since 15.0.0
  80. * @deprecated since 25.0.0 Use start() instead
  81. */
  82. final public function execute(IJobList $jobList, ILogger $logger = null) {
  83. $this->start($jobList);
  84. }
  85. /**
  86. * Run the job if the last run is more than the interval ago
  87. *
  88. * @since 25.0.0
  89. */
  90. final public function start(IJobList $jobList): void {
  91. if (($this->time->getTime() - $this->lastRun) > $this->interval) {
  92. parent::start($jobList);
  93. }
  94. }
  95. }