Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Deprecated ILogger from IJob
Since the ILogger will be soon removed we need to ensure that nothing in
the public api use it.

Signed-off-by: Carl Schwan <[email protected]>
  • Loading branch information
CarlSchwan committed Aug 8, 2022
commit 19a2d6f6e7d54eb9318279809bf6c3f40bf566e2
21 changes: 20 additions & 1 deletion lib/public/BackgroundJob/IJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
use OCP\ILogger;

/**
* Interface IJob
* This interface represend a backgroud job run with cron
*
* To implement a background job, you must extend either \OCP\BackgroundJob\Job,
* \OCP\BackgroundJob\TimedJob or \OCP\BackgroundJob\QueuedJob
*
* @since 7.0.0
*/
Expand All @@ -49,9 +52,25 @@ interface IJob {
* @param IJobList $jobList The job list that manages the state of this job
* @param ILogger|null $logger
* @since 7.0.0
* @deprecated since 25.0.0 Use start() instead. This method will be removed
* with the ILogger interface
*/
public function execute(IJobList $jobList, ILogger $logger = null);

/**
* Start the background job with the registered argument
*
* This methods will take care of running the background job, of initializing
* the state and cleaning up the job list after running the job.
*
* For common background job scenario, you will want to use TimedJob or QueuedJob
* instead of overwritting this method.
*
* @param IJobList $jobList The job list that manages the state of this job
* @since 25.0.0
*/
public function start(IJobList $jobList): void;

/**
* @since 7.0.0
*/
Expand Down
30 changes: 14 additions & 16 deletions lib/public/BackgroundJob/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@
* @since 15.0.0
*/
abstract class Job implements IJob {

/** @var int $id */
protected $id;

/** @var int $lastRun */
protected $lastRun;

/** @var mixed $argument */
protected int $id = 0;
protected int $lastRun = 0;
protected $argument;

/** @var ITimeFactory */
protected $time;
protected ITimeFactory $time;

/**
* @since 15.0.0
Expand All @@ -68,10 +60,16 @@ public function __construct(ITimeFactory $time) {
* @since 15.0.0
*/
public function execute(IJobList $jobList, ILogger $logger = null) {
$this->start($jobList);
}

/**
* @inheritdoc
* @since 25.0.0
*/
public function start(IJobList $jobList): void {
$jobList->setLastRun($this);
if ($logger === null) {
$logger = \OC::$server->getLogger();
}
$logger = \OCP\Server::get(LoggerInterface::class);

try {
$jobStartTime = $this->time->getTime();
Expand All @@ -83,9 +81,9 @@ public function execute(IJobList $jobList, ILogger $logger = null) {
$jobList->setExecutionTime($this, $timeTaken);
} catch (\Exception $e) {
if ($logger) {
$logger->logException($e, [
$logger->error('Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')', [
'app' => 'core',
'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')'
'exception' => $e,
]);
}
}
Expand Down
15 changes: 13 additions & 2 deletions lib/public/BackgroundJob/QueuedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,26 @@
abstract class QueuedJob extends Job {

/**
* run the job, then remove it from the joblist
* Run the job, then remove it from the joblist
*
* @param IJobList $jobList
* @param ILogger|null $logger
*
* @since 15.0.0
* @deprecated since 25.0.0 Use start() instead. This method will be removed
* with the ILogger interface
*/
final public function execute($jobList, ILogger $logger = null) {
$this->start($jobList);
}

/**
* Run the job, then remove it from the joblist
*
* @since 15.0.0
*/
final public function start(IJobList $jobList): void {
$jobList->remove($this, $this->argument);
parent::execute($jobList, $logger);
parent::start($jobList);
}
}
20 changes: 14 additions & 6 deletions lib/public/BackgroundJob/TimedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@
* @since 15.0.0
*/
abstract class TimedJob extends Job {
/** @var int */
protected $interval = 0;
/** @var int */
protected $timeSensitivity = IJob::TIME_SENSITIVE;
protected int $interval = 0;
protected int $timeSensitivity = IJob::TIME_SENSITIVE;

/**
* set the interval for the job
* Set the interval for the job
*
* @param int $seconds the time to pass between two runs of the same job in seconds
*
Expand Down Expand Up @@ -89,10 +87,20 @@ public function setTimeSensitivity(int $sensitivity): void {
* @param ILogger|null $logger
*
* @since 15.0.0
* @deprecated since 25.0.0 Use start() instead
*/
final public function execute($jobList, ILogger $logger = null) {
$this->start($jobList);
}

/**
* Run the job if the last run is is more than the interval ago
*
* @since 25.0.0
*/
final public function start(IJobList $jobList): void {
if (($this->time->getTime() - $this->lastRun) > $this->interval) {
parent::execute($jobList, $logger);
parent::start($jobList);
}
}
}