diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-06-28 12:09:08 +0200 |
---|---|---|
committer | Carl Schwan <carl@carlschwan.eu> | 2022-08-08 17:03:13 +0200 |
commit | 19a2d6f6e7d54eb9318279809bf6c3f40bf566e2 (patch) | |
tree | 498590804c91807f34e91e93c6fa8c0d3fe06cea | |
parent | 9475cc02b218f7cab402ec0b2b370ed6c68650c1 (diff) | |
download | nextcloud-server-19a2d6f6e7d54eb9318279809bf6c3f40bf566e2.tar.gz nextcloud-server-19a2d6f6e7d54eb9318279809bf6c3f40bf566e2.zip |
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 <carl@carlschwan.eu>
-rw-r--r-- | lib/public/BackgroundJob/IJob.php | 21 | ||||
-rw-r--r-- | lib/public/BackgroundJob/Job.php | 30 | ||||
-rw-r--r-- | lib/public/BackgroundJob/QueuedJob.php | 15 | ||||
-rw-r--r-- | lib/public/BackgroundJob/TimedJob.php | 20 |
4 files changed, 61 insertions, 25 deletions
diff --git a/lib/public/BackgroundJob/IJob.php b/lib/public/BackgroundJob/IJob.php index 3c2da42bf88..24d8e7aad4a 100644 --- a/lib/public/BackgroundJob/IJob.php +++ b/lib/public/BackgroundJob/IJob.php @@ -29,7 +29,10 @@ namespace OCP\BackgroundJob; 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 */ @@ -49,10 +52,26 @@ 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 */ public function setId(int $id); diff --git a/lib/public/BackgroundJob/Job.php b/lib/public/BackgroundJob/Job.php index 5b20ac82684..5831891705a 100644 --- a/lib/public/BackgroundJob/Job.php +++ b/lib/public/BackgroundJob/Job.php @@ -38,18 +38,10 @@ use OCP\ILogger; * @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 @@ -68,10 +60,16 @@ abstract class Job implements IJob { * @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(); @@ -83,9 +81,9 @@ abstract class Job implements IJob { $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, ]); } } diff --git a/lib/public/BackgroundJob/QueuedJob.php b/lib/public/BackgroundJob/QueuedJob.php index e7e6e9a2939..71f09fccdea 100644 --- a/lib/public/BackgroundJob/QueuedJob.php +++ b/lib/public/BackgroundJob/QueuedJob.php @@ -35,15 +35,26 @@ use OCP\ILogger; 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); } } diff --git a/lib/public/BackgroundJob/TimedJob.php b/lib/public/BackgroundJob/TimedJob.php index 579486f6fbf..9d6b599c21b 100644 --- a/lib/public/BackgroundJob/TimedJob.php +++ b/lib/public/BackgroundJob/TimedJob.php @@ -36,13 +36,11 @@ use OCP\ILogger; * @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 * @@ -89,10 +87,20 @@ abstract class TimedJob extends Job { * @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); } } } |