TimedJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 OC\BackgroundJob\JobList;
  27. use OCP\ILogger;
  28. /**
  29. * Simple base class to extend to run periodic background jobs.
  30. * Call setInterval with your desired interval in seconds from the constructor.
  31. *
  32. * @since 15.0.0
  33. */
  34. abstract class TimedJob extends Job {
  35. protected int $interval = 0;
  36. protected int $timeSensitivity = IJob::TIME_SENSITIVE;
  37. /**
  38. * Set the interval for the job
  39. *
  40. * @param int $seconds the time to pass between two runs of the same job in seconds
  41. *
  42. * @since 15.0.0
  43. */
  44. public function setInterval(int $seconds) {
  45. $this->interval = $seconds;
  46. }
  47. /**
  48. * Whether the background job is time sensitive and needs to run soon after
  49. * the scheduled interval, of if it is okay to be delayed until a later time.
  50. *
  51. * @return bool
  52. * @since 24.0.0
  53. */
  54. public function isTimeSensitive(): bool {
  55. return $this->timeSensitivity === IJob::TIME_SENSITIVE;
  56. }
  57. /**
  58. * If your background job is not time sensitive (sending instant email
  59. * notifications, etc.) it would be nice to set it to IJob::TIME_INSENSITIVE
  60. * This way the execution can be delayed during high usage times.
  61. *
  62. * @param int $sensitivity
  63. * @psalm-param IJob::TIME_* $sensitivity
  64. * @return void
  65. * @since 24.0.0
  66. */
  67. public function setTimeSensitivity(int $sensitivity): void {
  68. if ($sensitivity !== IJob::TIME_SENSITIVE &&
  69. $sensitivity !== IJob::TIME_INSENSITIVE) {
  70. throw new \InvalidArgumentException('Invalid sensitivity');
  71. }
  72. $this->timeSensitivity = $sensitivity;
  73. }
  74. /**
  75. * run the job if the last run is is more than the interval ago
  76. *
  77. * @param JobList $jobList
  78. * @param ILogger|null $logger
  79. *
  80. * @since 15.0.0
  81. * @deprecated since 25.0.0 Use start() instead
  82. */
  83. final public function execute($jobList, ILogger $logger = null) {
  84. $this->start($jobList);
  85. }
  86. /**
  87. * Run the job if the last run is is more than the interval ago
  88. *
  89. * @since 25.0.0
  90. */
  91. final public function start(IJobList $jobList): void {
  92. if (($this->time->getTime() - $this->lastRun) > $this->interval) {
  93. parent::start($jobList);
  94. }
  95. }
  96. }