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 ;
2024-10-07 11:37:38 -04:00
use OCP\Server ;
use Psr\Log\LoggerInterface ;
2018-10-09 06:36:43 -04:00
/**
* 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
2024-09-18 18:37:55 -04:00
* @ since 25.0 . 0 deprecated `execute()` method in favor of `start()`
* @ since 33.0 . 0 removed deprecated `execute()` method
2018-10-09 06:36:43 -04:00
*/
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
*/
2024-09-18 18:37:55 -04:00
public function setInterval ( int $seconds ) : void {
2021-03-26 04:13:05 -04:00
$this -> interval = $seconds ;
2018-10-09 06:36:43 -04:00
}
2025-02-12 03:54:07 -05:00
/**
* Get the interval [ seconds ] for the job
*
* @ since 32.0 . 0
*/
public function getInterval () : int {
return $this -> interval ;
}
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 {
2024-10-07 11:36:55 -04:00
if ( $sensitivity !== self :: TIME_SENSITIVE
&& $sensitivity !== self :: TIME_INSENSITIVE ) {
2022-01-31 11:56:43 -05:00
throw new \InvalidArgumentException ( 'Invalid sensitivity' );
}
$this -> timeSensitivity = $sensitivity ;
}
2022-06-28 06:09:08 -04:00
/**
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 ) {
2024-10-07 11:37:38 -04:00
if ( $this -> interval >= 12 * 60 * 60 && $this -> isTimeSensitive ()) {
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.' );
}
2022-06-28 06:09:08 -04:00
parent :: start ( $jobList );
2018-10-09 06:36:43 -04:00
}
}
}