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);
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-10-09 06:36:43 -04:00
|
|
|
/**
|
|
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
|
*
|
2021-06-04 15:52:51 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2018-10-09 06:36:43 -04:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
|
*
|
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2021-06-04 15:52:51 -04:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-10-09 06:36:43 -04:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
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
|
|
|
*/
|
2023-06-28 08:56:39 -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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|