2018-10-09 06:36:43 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-10-09 06:36:43 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-10-09 06:36:43 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCP\BackgroundJob;
|
|
|
|
|
|
|
|
|
|
use OCP\ILogger;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Simple base class to extend to run periodic background jobs.
|
|
|
|
|
* Call setInterval with your desired interval in seconds from the constructor.
|
|
|
|
|
*
|
|
|
|
|
* @since 15.0.0
|
|
|
|
|
*/
|
|
|
|
|
abstract class TimedJob extends Job {
|
2022-06-28 06:09:08 -04:00
|
|
|
protected int $interval = 0;
|
|
|
|
|
protected int $timeSensitivity = IJob::TIME_SENSITIVE;
|
2018-10-09 06:36:43 -04:00
|
|
|
|
|
|
|
|
/**
|
2022-06-28 06:09:08 -04:00
|
|
|
* Set the interval for the job
|
2018-10-09 06:36:43 -04:00
|
|
|
*
|
2021-03-26 04:13:05 -04:00
|
|
|
* @param int $seconds the time to pass between two runs of the same job in seconds
|
|
|
|
|
*
|
2018-10-09 06:36:43 -04:00
|
|
|
* @since 15.0.0
|
|
|
|
|
*/
|
2021-03-26 04:13:05 -04:00
|
|
|
public function setInterval(int $seconds) {
|
|
|
|
|
$this->interval = $seconds;
|
2018-10-09 06:36:43 -04:00
|
|
|
}
|
|
|
|
|
|
2022-01-31 11:56:43 -05:00
|
|
|
/**
|
|
|
|
|
* Whether the background job is time sensitive and needs to run soon after
|
|
|
|
|
* the scheduled interval, of if it is okay to be delayed until a later time.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 24.0.0
|
|
|
|
|
*/
|
|
|
|
|
public function isTimeSensitive(): bool {
|
|
|
|
|
return $this->timeSensitivity === IJob::TIME_SENSITIVE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If your background job is not time sensitive (sending instant email
|
|
|
|
|
* notifications, etc.) it would be nice to set it to IJob::TIME_INSENSITIVE
|
|
|
|
|
* This way the execution can be delayed during high usage times.
|
|
|
|
|
*
|
|
|
|
|
* @param int $sensitivity
|
|
|
|
|
* @psalm-param IJob::TIME_* $sensitivity
|
|
|
|
|
* @return void
|
|
|
|
|
* @since 24.0.0
|
|
|
|
|
*/
|
|
|
|
|
public function setTimeSensitivity(int $sensitivity): void {
|
|
|
|
|
if ($sensitivity !== IJob::TIME_SENSITIVE &&
|
|
|
|
|
$sensitivity !== IJob::TIME_INSENSITIVE) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid sensitivity');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->timeSensitivity = $sensitivity;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 06:36:43 -04:00
|
|
|
/**
|
2023-06-28 08:56:39 -04:00
|
|
|
* Run the job if the last run is more than the interval ago
|
2018-10-09 06:36:43 -04:00
|
|
|
*
|
2023-06-28 08:56:39 -04:00
|
|
|
* @param IJobList $jobList
|
2018-10-09 06:36:43 -04:00
|
|
|
* @param ILogger|null $logger
|
|
|
|
|
*
|
|
|
|
|
* @since 15.0.0
|
2022-06-28 06:09:08 -04:00
|
|
|
* @deprecated since 25.0.0 Use start() instead
|
2018-10-09 06:36:43 -04:00
|
|
|
*/
|
2024-03-28 11:13:19 -04:00
|
|
|
final public function execute(IJobList $jobList, ?ILogger $logger = null) {
|
2022-06-28 06:09:08 -04:00
|
|
|
$this->start($jobList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-28 08:56:39 -04:00
|
|
|
* Run the job if the last run is more than the interval ago
|
2022-06-28 06:09:08 -04:00
|
|
|
*
|
|
|
|
|
* @since 25.0.0
|
|
|
|
|
*/
|
|
|
|
|
final public function start(IJobList $jobList): void {
|
2018-10-09 06:36:43 -04:00
|
|
|
if (($this->time->getTime() - $this->lastRun) > $this->interval) {
|
2022-06-28 06:09:08 -04:00
|
|
|
parent::start($jobList);
|
2018-10-09 06:36:43 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|