1
0

TimedJob.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\BackgroundJob;
  8. use OCP\ILogger;
  9. use OCP\Server;
  10. use Psr\Log\LoggerInterface;
  11. /**
  12. * Simple base class to extend to run periodic background jobs.
  13. * Call setInterval with your desired interval in seconds from the constructor.
  14. *
  15. * @since 15.0.0
  16. */
  17. abstract class TimedJob extends Job {
  18. protected int $interval = 0;
  19. protected int $timeSensitivity = IJob::TIME_SENSITIVE;
  20. /**
  21. * Set the interval for the job
  22. *
  23. * @param int $seconds the time to pass between two runs of the same job in seconds
  24. *
  25. * @since 15.0.0
  26. */
  27. public function setInterval(int $seconds) {
  28. $this->interval = $seconds;
  29. }
  30. /**
  31. * Whether the background job is time sensitive and needs to run soon after
  32. * the scheduled interval, of if it is okay to be delayed until a later time.
  33. *
  34. * @return bool
  35. * @since 24.0.0
  36. */
  37. public function isTimeSensitive(): bool {
  38. return $this->timeSensitivity === IJob::TIME_SENSITIVE;
  39. }
  40. /**
  41. * If your background job is not time sensitive (sending instant email
  42. * notifications, etc.) it would be nice to set it to IJob::TIME_INSENSITIVE
  43. * This way the execution can be delayed during high usage times.
  44. *
  45. * @param int $sensitivity
  46. * @psalm-param IJob::TIME_* $sensitivity
  47. * @return void
  48. * @since 24.0.0
  49. */
  50. public function setTimeSensitivity(int $sensitivity): void {
  51. if ($sensitivity !== self::TIME_SENSITIVE &&
  52. $sensitivity !== self::TIME_INSENSITIVE) {
  53. throw new \InvalidArgumentException('Invalid sensitivity');
  54. }
  55. $this->timeSensitivity = $sensitivity;
  56. }
  57. /**
  58. * Run the job if the last run is more than the interval ago
  59. *
  60. * @param IJobList $jobList
  61. * @param ILogger|null $logger
  62. *
  63. * @since 15.0.0
  64. * @deprecated 25.0.0 Use start() instead
  65. */
  66. final public function execute(IJobList $jobList, ?ILogger $logger = null) {
  67. $this->start($jobList);
  68. }
  69. /**
  70. * Run the job if the last run is more than the interval ago
  71. *
  72. * @since 25.0.0
  73. */
  74. final public function start(IJobList $jobList): void {
  75. if (($this->time->getTime() - $this->lastRun) > $this->interval) {
  76. if ($this->interval >= 12 * 60 * 60 && $this->isTimeSensitive()) {
  77. Server::get(LoggerInterface::class)->debug('TimedJob ' . get_class($this) . ' has a configured interval of ' . $this->interval . ' seconds, but is also marked as time sensitive. Please consider marking it as time insensitive to allow more sensitive jobs to run when needed.');
  78. }
  79. parent::start($jobList);
  80. }
  81. }
  82. }