TimedJob.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /** @var int */
  36. protected $interval = 0;
  37. /** @var int */
  38. protected $timeSensitivity = IJob::TIME_SENSITIVE;
  39. /**
  40. * set the interval for the job
  41. *
  42. * @param int $seconds the time to pass between two runs of the same job in seconds
  43. *
  44. * @since 15.0.0
  45. */
  46. public function setInterval(int $seconds) {
  47. $this->interval = $seconds;
  48. }
  49. /**
  50. * Whether the background job is time sensitive and needs to run soon after
  51. * the scheduled interval, of if it is okay to be delayed until a later time.
  52. *
  53. * @return bool
  54. * @since 24.0.0
  55. */
  56. public function isTimeSensitive(): bool {
  57. return $this->timeSensitivity === IJob::TIME_SENSITIVE;
  58. }
  59. /**
  60. * If your background job is not time sensitive (sending instant email
  61. * notifications, etc.) it would be nice to set it to IJob::TIME_INSENSITIVE
  62. * This way the execution can be delayed during high usage times.
  63. *
  64. * @param int $sensitivity
  65. * @psalm-param IJob::TIME_* $sensitivity
  66. * @return void
  67. * @since 24.0.0
  68. */
  69. public function setTimeSensitivity(int $sensitivity): void {
  70. if ($sensitivity !== IJob::TIME_SENSITIVE &&
  71. $sensitivity !== IJob::TIME_INSENSITIVE) {
  72. throw new \InvalidArgumentException('Invalid sensitivity');
  73. }
  74. $this->timeSensitivity = $sensitivity;
  75. }
  76. /**
  77. * run the job if the last run is is more than the interval ago
  78. *
  79. * @param JobList $jobList
  80. * @param ILogger|null $logger
  81. *
  82. * @since 15.0.0
  83. */
  84. final public function execute($jobList, ILogger $logger = null) {
  85. if (($this->time->getTime() - $this->lastRun) > $this->interval) {
  86. parent::execute($jobList, $logger);
  87. }
  88. }
  89. }