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 >
*
2020-04-29 05:57:22 -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\AppFramework\Utility\ITimeFactory ;
use OCP\ILogger ;
2022-06-28 06:55:26 -04:00
use Psr\Log\LoggerInterface ;
2018-10-09 06:36:43 -04:00
/**
* Base class for background jobs
*
* This is here if you want to do advanced stuff in your background jobs .
* For the most common use cases have a look at QueuedJob and TimedJob
*
* @ since 15.0 . 0
*/
2023-04-20 07:55:28 -04:00
abstract class Job implements IJob , IParallelAwareJob {
2022-06-28 06:09:08 -04:00
protected int $id = 0 ;
protected int $lastRun = 0 ;
2018-10-09 06:36:43 -04:00
protected $argument ;
2022-06-28 06:09:08 -04:00
protected ITimeFactory $time ;
2023-04-20 06:55:06 -04:00
protected bool $allowParallelRuns = true ;
2023-04-20 09:29:47 -04:00
private ? ILogger $logger = null ;
2018-10-09 06:36:43 -04:00
/**
* @ since 15.0 . 0
*/
public function __construct ( ITimeFactory $time ) {
$this -> time = $time ;
}
/**
* The function to prepare the execution of the job .
2020-04-08 16:24:54 -04:00
*
2018-10-09 06:36:43 -04:00
*
* @ param IJobList $jobList
* @ param ILogger | null $logger
*
* @ since 15.0 . 0
*/
2020-11-24 03:33:49 -05:00
public function execute ( IJobList $jobList , ILogger $logger = null ) {
2023-04-20 09:29:47 -04:00
$this -> logger = $logger ;
2022-06-28 06:09:08 -04:00
$this -> start ( $jobList );
}
/**
* @ inheritdoc
* @ since 25.0 . 0
*/
public function start ( IJobList $jobList ) : void {
2018-10-09 06:36:43 -04:00
$jobList -> setLastRun ( $this );
2023-04-20 09:29:47 -04:00
$logger = $this -> logger ? ? \OCP\Server :: get ( LoggerInterface :: class );
2018-10-09 06:36:43 -04:00
2023-04-20 06:55:06 -04:00
if ( ! $this -> getAllowParallelRuns () && $jobList -> hasReservedJob ( get_class ( $this ))) {
$logger -> debug ( 'Skipping ' . get_class ( $this ) . ' job with ID ' . $this -> getId () . ' because another job with the same class is already running' , [ 'app' => 'cron' ]);
return ;
}
2018-10-09 06:36:43 -04:00
try {
$jobStartTime = $this -> time -> getTime ();
$logger -> debug ( 'Run ' . get_class ( $this ) . ' job with ID ' . $this -> getId (), [ 'app' => 'cron' ]);
$this -> run ( $this -> argument );
$timeTaken = $this -> time -> getTime () - $jobStartTime ;
$logger -> debug ( 'Finished ' . get_class ( $this ) . ' job with ID ' . $this -> getId () . ' in ' . $timeTaken . ' seconds' , [ 'app' => 'cron' ]);
$jobList -> setExecutionTime ( $this , $timeTaken );
2023-04-20 09:29:47 -04:00
} catch ( \Throwable $e ) {
2018-10-09 06:36:43 -04:00
if ( $logger ) {
2022-06-28 06:09:08 -04:00
$logger -> error ( 'Error while running background job (class: ' . get_class ( $this ) . ', arguments: ' . print_r ( $this -> argument , true ) . ')' , [
2018-10-09 06:36:43 -04:00
'app' => 'core' ,
2022-06-28 06:09:08 -04:00
'exception' => $e ,
2018-10-09 06:36:43 -04:00
]);
}
}
}
/**
* @ since 15.0 . 0
*/
2020-11-24 03:33:49 -05:00
final public function setId ( int $id ) {
2018-10-09 06:36:43 -04:00
$this -> id = $id ;
}
/**
* @ since 15.0 . 0
*/
2020-11-24 03:33:49 -05:00
final public function setLastRun ( int $lastRun ) {
2018-10-09 06:36:43 -04:00
$this -> lastRun = $lastRun ;
}
/**
* @ since 15.0 . 0
*/
public function setArgument ( $argument ) {
$this -> argument = $argument ;
}
/**
* @ since 15.0 . 0
*/
final public function getId () : int {
return $this -> id ;
}
/**
* @ since 15.0 . 0
*/
final public function getLastRun () : int {
return $this -> lastRun ;
}
/**
* @ since 15.0 . 0
*/
public function getArgument () {
return $this -> argument ;
}
2023-04-20 06:55:06 -04:00
/**
* Set this to false to prevent two Jobs from this class from running in parallel
*
* @ param bool $allow
* @ return void
* @ since 27.0 . 0
*/
2023-04-20 07:55:28 -04:00
public function setAllowParallelRuns ( bool $allow ) : void {
2023-04-20 06:55:06 -04:00
$this -> allowParallelRuns = $allow ;
}
/**
* @ return bool
* @ since 27.0 . 0
*/
public function getAllowParallelRuns () : bool {
return $this -> allowParallelRuns ;
}
2018-10-09 06:36:43 -04:00
/**
* The actual function that is called to run the job
*
* @ param $argument
2018-10-09 09:10:38 -04:00
*
* @ since 15.0 . 0
2018-10-09 06:36:43 -04:00
*/
abstract protected function run ( $argument );
}