diff options
author | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2024-02-08 11:52:40 +0100 |
---|---|---|
committer | Louis Chemineau <louis@chmn.me> | 2024-02-08 18:27:44 +0100 |
commit | 303e2febc717065581a3e3bdc92e1672bb8253dc (patch) | |
tree | c8f88c01fa1e7f93958a6b48bee3aa4bf5525dfb /lib | |
parent | f6ed1effbf4bb040ab35c625159ad80caff5e687 (diff) | |
download | nextcloud-server-303e2febc717065581a3e3bdc92e1672bb8253dc.tar.gz nextcloud-server-303e2febc717065581a3e3bdc92e1672bb8253dc.zip |
Merge pull request #43387 from nextcloud/fix/migrate-away-from-ilogger-in-jobs
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 3 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 3 | ||||
-rw-r--r-- | lib/private/BackgroundJob/Job.php | 98 | ||||
-rw-r--r-- | lib/private/BackgroundJob/QueuedJob.php | 49 | ||||
-rw-r--r-- | lib/private/BackgroundJob/TimedJob.php | 63 | ||||
-rw-r--r-- | lib/private/Command/CallableJob.php | 2 | ||||
-rw-r--r-- | lib/private/Command/ClosureJob.php | 2 | ||||
-rw-r--r-- | lib/private/Command/CommandJob.php | 2 | ||||
-rw-r--r-- | lib/private/Command/CronBus.php | 28 | ||||
-rw-r--r-- | lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php | 47 | ||||
-rw-r--r-- | lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php | 34 | ||||
-rw-r--r-- | lib/public/BackgroundJob/IJobList.php | 4 | ||||
-rw-r--r-- | lib/public/BackgroundJob/Job.php | 13 | ||||
-rw-r--r-- | lib/public/Log/ILogFactory.php | 10 |
14 files changed, 53 insertions, 305 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 2fffd0c2f2d..90ab0c8e207 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -955,10 +955,7 @@ return array( 'OC\\Avatar\\GuestAvatar' => $baseDir . '/lib/private/Avatar/GuestAvatar.php', 'OC\\Avatar\\PlaceholderAvatar' => $baseDir . '/lib/private/Avatar/PlaceholderAvatar.php', 'OC\\Avatar\\UserAvatar' => $baseDir . '/lib/private/Avatar/UserAvatar.php', - 'OC\\BackgroundJob\\Job' => $baseDir . '/lib/private/BackgroundJob/Job.php', 'OC\\BackgroundJob\\JobList' => $baseDir . '/lib/private/BackgroundJob/JobList.php', - 'OC\\BackgroundJob\\QueuedJob' => $baseDir . '/lib/private/BackgroundJob/QueuedJob.php', - 'OC\\BackgroundJob\\TimedJob' => $baseDir . '/lib/private/BackgroundJob/TimedJob.php', 'OC\\BinaryFinder' => $baseDir . '/lib/private/BinaryFinder.php', 'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => $baseDir . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php', 'OC\\Broadcast\\Events\\BroadcastEvent' => $baseDir . '/lib/private/Broadcast/Events/BroadcastEvent.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index d38db8050cc..983a1081621 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -988,10 +988,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Avatar\\GuestAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/GuestAvatar.php', 'OC\\Avatar\\PlaceholderAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/PlaceholderAvatar.php', 'OC\\Avatar\\UserAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/UserAvatar.php', - 'OC\\BackgroundJob\\Job' => __DIR__ . '/../../..' . '/lib/private/BackgroundJob/Job.php', 'OC\\BackgroundJob\\JobList' => __DIR__ . '/../../..' . '/lib/private/BackgroundJob/JobList.php', - 'OC\\BackgroundJob\\QueuedJob' => __DIR__ . '/../../..' . '/lib/private/BackgroundJob/QueuedJob.php', - 'OC\\BackgroundJob\\TimedJob' => __DIR__ . '/../../..' . '/lib/private/BackgroundJob/TimedJob.php', 'OC\\BinaryFinder' => __DIR__ . '/../../..' . '/lib/private/BinaryFinder.php', 'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => __DIR__ . '/../../..' . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php', 'OC\\Broadcast\\Events\\BroadcastEvent' => __DIR__ . '/../../..' . '/lib/private/Broadcast/Events/BroadcastEvent.php', diff --git a/lib/private/BackgroundJob/Job.php b/lib/private/BackgroundJob/Job.php deleted file mode 100644 index ffcaaf8c36d..00000000000 --- a/lib/private/BackgroundJob/Job.php +++ /dev/null @@ -1,98 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Noveen Sachdeva <noveen.sachdeva@research.iiit.ac.in> - * @author Robin Appelman <robin@icewind.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ -namespace OC\BackgroundJob; - -use OCP\BackgroundJob\IJob; -use OCP\BackgroundJob\IJobList; -use OCP\ILogger; - -/** - * @deprecated internal class, use \OCP\BackgroundJob\Job - */ -abstract class Job implements IJob { - /** @var int */ - protected $id; - - /** @var int */ - protected $lastRun; - - /** @var mixed */ - protected $argument; - - public function execute(IJobList $jobList, ILogger $logger = null) { - $jobList->setLastRun($this); - if ($logger === null) { - $logger = \OC::$server->getLogger(); - } - - try { - $jobStartTime = time(); - $logger->debug('Run ' . get_class($this) . ' job with ID ' . $this->getId(), ['app' => 'cron']); - $this->run($this->argument); - $timeTaken = time() - $jobStartTime; - - $logger->debug('Finished ' . get_class($this) . ' job with ID ' . $this->getId() . ' in ' . $timeTaken . ' seconds', ['app' => 'cron']); - $jobList->setExecutionTime($this, $timeTaken); - } catch (\Throwable $e) { - if ($logger) { - $logger->logException($e, [ - 'app' => 'core', - 'message' => 'Error while running background job (class: ' . get_class($this) . ', arguments: ' . print_r($this->argument, true) . ')' - ]); - } - } - } - - public function start(IJobList $jobList): void { - $this->execute($jobList); - } - - abstract protected function run($argument); - - public function setId(int $id) { - $this->id = $id; - } - - public function setLastRun(int $lastRun) { - $this->lastRun = $lastRun; - } - - public function setArgument($argument) { - $this->argument = $argument; - } - - public function getId() { - return $this->id; - } - - public function getLastRun() { - return $this->lastRun; - } - - public function getArgument() { - return $this->argument; - } -} diff --git a/lib/private/BackgroundJob/QueuedJob.php b/lib/private/BackgroundJob/QueuedJob.php deleted file mode 100644 index 28d86481e62..00000000000 --- a/lib/private/BackgroundJob/QueuedJob.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ -namespace OC\BackgroundJob; - -use OCP\ILogger; - -/** - * Class QueuedJob - * - * create a background job that is to be executed once - * - * @package OC\BackgroundJob - * - * @deprecated internal class, use \OCP\BackgroundJob\QueuedJob - */ -abstract class QueuedJob extends Job { - /** - * run the job, then remove it from the joblist - * - * @param JobList $jobList - * @param ILogger|null $logger - */ - public function execute($jobList, ILogger $logger = null) { - $jobList->remove($this, $this->argument); - parent::execute($jobList, $logger); - } -} diff --git a/lib/private/BackgroundJob/TimedJob.php b/lib/private/BackgroundJob/TimedJob.php deleted file mode 100644 index 0f0951e1aec..00000000000 --- a/lib/private/BackgroundJob/TimedJob.php +++ /dev/null @@ -1,63 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * - */ -namespace OC\BackgroundJob; - -use OCP\BackgroundJob\IJobList; -use OCP\ILogger; - -/** - * Class QueuedJob - * - * create a background job that is to be executed at an interval - * - * @package OC\BackgroundJob - * - * @deprecated internal class, use \OCP\BackgroundJob\TimedJob - */ -abstract class TimedJob extends Job { - protected $interval = 0; - - /** - * set the interval for the job - * - * @param int $interval - */ - public function setInterval($interval) { - $this->interval = $interval; - } - - /** - * run the job if - * - * @param IJobList $jobList - * @param ILogger|null $logger - */ - public function execute($jobList, ILogger $logger = null) { - if ((time() - $this->lastRun) > $this->interval) { - parent::execute($jobList, $logger); - } - } -} diff --git a/lib/private/Command/CallableJob.php b/lib/private/Command/CallableJob.php index 8bb3c76c9af..7f515660955 100644 --- a/lib/private/Command/CallableJob.php +++ b/lib/private/Command/CallableJob.php @@ -21,7 +21,7 @@ */ namespace OC\Command; -use OC\BackgroundJob\QueuedJob; +use OCP\BackgroundJob\QueuedJob; class CallableJob extends QueuedJob { protected function run($serializedCallable) { diff --git a/lib/private/Command/ClosureJob.php b/lib/private/Command/ClosureJob.php index f7b0ee1a3d3..3e0fe73b029 100644 --- a/lib/private/Command/ClosureJob.php +++ b/lib/private/Command/ClosureJob.php @@ -23,7 +23,7 @@ namespace OC\Command; use Laravel\SerializableClosure\SerializableClosure as LaravelClosure; -use OC\BackgroundJob\QueuedJob; +use OCP\BackgroundJob\QueuedJob; class ClosureJob extends QueuedJob { protected function run($argument) { diff --git a/lib/private/Command/CommandJob.php b/lib/private/Command/CommandJob.php index e34ffe9440b..368d264f3bb 100644 --- a/lib/private/Command/CommandJob.php +++ b/lib/private/Command/CommandJob.php @@ -22,7 +22,7 @@ */ namespace OC\Command; -use OC\BackgroundJob\QueuedJob; +use OCP\BackgroundJob\QueuedJob; use OCP\Command\ICommand; /** diff --git a/lib/private/Command/CronBus.php b/lib/private/Command/CronBus.php index 42ff458a95c..495cd011db1 100644 --- a/lib/private/Command/CronBus.php +++ b/lib/private/Command/CronBus.php @@ -26,31 +26,25 @@ namespace OC\Command; use Laravel\SerializableClosure\SerializableClosure; +use OCP\BackgroundJob\IJob; +use OCP\BackgroundJob\IJobList; use OCP\Command\ICommand; class CronBus extends AsyncBus { - /** - * @var \OCP\BackgroundJob\IJobList - */ - private $jobList; - - - /** - * @param \OCP\BackgroundJob\IJobList $jobList - */ - public function __construct($jobList) { - $this->jobList = $jobList; + public function __construct( + private IJobList $jobList, + ) { } - protected function queueCommand($command) { + protected function queueCommand($command): void { $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command)); } /** - * @param \OCP\Command\ICommand | callable $command - * @return string + * @param ICommand|callable $command + * @return class-string<IJob> */ - private function getJobClass($command) { + private function getJobClass($command): string { if ($command instanceof \Closure) { return ClosureJob::class; } elseif (is_callable($command)) { @@ -63,10 +57,10 @@ class CronBus extends AsyncBus { } /** - * @param \OCP\Command\ICommand | callable $command + * @param ICommand|callable $command * @return string */ - private function serializeCommand($command) { + private function serializeCommand($command): string { if ($command instanceof \Closure) { return serialize(new SerializableClosure($command)); } elseif (is_callable($command) or $command instanceof ICommand) { diff --git a/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php b/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php index 4ba9ad083e3..a23816e4711 100644 --- a/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php +++ b/lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php @@ -1,4 +1,7 @@ <?php + +declare(strict_types=1); + /** * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @@ -22,9 +25,9 @@ */ namespace OC\Repair\Owncloud; -use OC\BackgroundJob\QueuedJob; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; +use OCP\BackgroundJob\QueuedJob; use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; @@ -33,33 +36,14 @@ use OCP\IUserManager; use Psr\Log\LoggerInterface; class CleanPreviewsBackgroundJob extends QueuedJob { - /** @var IRootFolder */ - private $rootFolder; - - private LoggerInterface $logger; - - /** @var IJobList */ - private $jobList; - - /** @var ITimeFactory */ - private $timeFactory; - - /** @var IUserManager */ - private $userManager; - - /** - * CleanPreviewsBackgroundJob constructor. - */ - public function __construct(IRootFolder $rootFolder, - LoggerInterface $logger, - IJobList $jobList, + public function __construct( + private IRootFolder $rootFolder, + private LoggerInterface $logger, + private IJobList $jobList, ITimeFactory $timeFactory, - IUserManager $userManager) { - $this->rootFolder = $rootFolder; - $this->logger = $logger; - $this->jobList = $jobList; - $this->timeFactory = $timeFactory; - $this->userManager = $userManager; + private IUserManager $userManager, + ) { + parent::__construct($timeFactory); } public function run($arguments) { @@ -80,10 +64,9 @@ class CleanPreviewsBackgroundJob extends QueuedJob { } /** - * @param $uid - * @return bool + * @param string $uid */ - private function cleanupPreviews($uid) { + private function cleanupPreviews($uid): bool { try { $userFolder = $this->rootFolder->getUserFolder($uid); } catch (NotFoundException $e) { @@ -101,7 +84,7 @@ class CleanPreviewsBackgroundJob extends QueuedJob { $thumbnails = $thumbnailFolder->getDirectoryListing(); - $start = $this->timeFactory->getTime(); + $start = $this->time->getTime(); foreach ($thumbnails as $thumbnail) { try { $thumbnail->delete(); @@ -109,7 +92,7 @@ class CleanPreviewsBackgroundJob extends QueuedJob { // Ignore } - if (($this->timeFactory->getTime() - $start) > 15) { + if (($this->time->getTime() - $start) > 15) { return false; } } diff --git a/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php b/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php index 83c78c2cba4..9f01c4051e6 100644 --- a/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php +++ b/lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php @@ -1,4 +1,7 @@ <?php + +declare(strict_types=1); + /** * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @@ -23,10 +26,11 @@ */ namespace OC\Repair\Owncloud; -use OC\BackgroundJob\QueuedJob; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\QueuedJob; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; -use OCP\Files\Storage; +use OCP\Files\Storage\IStorage; use OCP\IAvatarManager; use OCP\IUser; use OCP\IUserManager; @@ -34,22 +38,16 @@ use Psr\Log\LoggerInterface; use function is_resource; class MoveAvatarsBackgroundJob extends QueuedJob { - /** @var IUserManager */ - private $userManager; - - /** @var LoggerInterface */ - private $logger; - - /** @var IAvatarManager */ - private $avatarManager; - - /** @var Storage */ - private $owncloudAvatarStorage; + private ?IStorage $owncloudAvatarStorage = null; - public function __construct(IUserManager $userManager, LoggerInterface $logger, IAvatarManager $avatarManager, IRootFolder $rootFolder) { - $this->userManager = $userManager; - $this->logger = $logger; - $this->avatarManager = $avatarManager; + public function __construct( + private IUserManager $userManager, + private LoggerInterface $logger, + private IAvatarManager $avatarManager, + private IRootFolder $rootFolder, + ITimeFactory $time, + ) { + parent::__construct($time); try { $this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage(); } catch (\Exception $e) { @@ -69,7 +67,7 @@ class MoveAvatarsBackgroundJob extends QueuedJob { } $counter = 0; - $this->userManager->callForSeenUsers(function (IUser $user) use ($counter) { + $this->userManager->callForSeenUsers(function (IUser $user) use (&$counter) { $uid = $user->getUID(); $path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid); diff --git a/lib/public/BackgroundJob/IJobList.php b/lib/public/BackgroundJob/IJobList.php index 0b00326ca1a..07b5ebcf48b 100644 --- a/lib/public/BackgroundJob/IJobList.php +++ b/lib/public/BackgroundJob/IJobList.php @@ -32,8 +32,8 @@ namespace OCP\BackgroundJob; * This interface provides functions to register background jobs * * To create a new background job create a new class that inherits from either - * \OC\BackgroundJob\Job, \OC\BackgroundJob\QueuedJob or - * \OC\BackgroundJob\TimedJob and register it using ->add($job, $argument), + * \OCP\BackgroundJob\Job, \OCP\BackgroundJob\QueuedJob or + * \OCP\BackgroundJob\TimedJob and register it using ->add($job, $argument), * $argument will be passed to the run() function of the job when the job is * executed. * diff --git a/lib/public/BackgroundJob/Job.php b/lib/public/BackgroundJob/Job.php index 2842486b74d..3d1a117ac9e 100644 --- a/lib/public/BackgroundJob/Job.php +++ b/lib/public/BackgroundJob/Job.php @@ -44,7 +44,6 @@ abstract class Job implements IJob, IParallelAwareJob { protected $argument; protected ITimeFactory $time; protected bool $allowParallelRuns = true; - private ?ILogger $logger = null; /** * @since 15.0.0 @@ -56,14 +55,13 @@ abstract class Job implements IJob, IParallelAwareJob { /** * The function to prepare the execution of the job. * - * - * @param IJobList $jobList - * @param ILogger|null $logger + * @return void * * @since 15.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) { - $this->logger = $logger; + public function execute(IJobList $jobList, ?ILogger $logger = null) { $this->start($jobList); } @@ -73,7 +71,7 @@ abstract class Job implements IJob, IParallelAwareJob { */ public function start(IJobList $jobList): void { $jobList->setLastRun($this); - $logger = $this->logger ?? \OCP\Server::get(LoggerInterface::class); + $logger = \OCP\Server::get(LoggerInterface::class); try { $jobDetails = get_class($this) . ' (id: ' . $this->getId() . ', arguments: ' . json_encode($this->getArgument()) . ')'; @@ -159,6 +157,7 @@ abstract class Job implements IJob, IParallelAwareJob { * The actual function that is called to run the job * * @param $argument + * @return void * * @since 15.0.0 */ diff --git a/lib/public/Log/ILogFactory.php b/lib/public/Log/ILogFactory.php index e0128d6b11c..e51a674afbd 100644 --- a/lib/public/Log/ILogFactory.php +++ b/lib/public/Log/ILogFactory.php @@ -24,7 +24,6 @@ */ namespace OCP\Log; -use OCP\ILogger; use Psr\Log\LoggerInterface; /** @@ -42,15 +41,6 @@ interface ILogFactory { /** * @param string $path - * @return ILogger - * @since 14.0.0 - * @deprecated 22.0.0 Use \OCP\Log\ILogFactory::getCustomPsrLogger - * @see \OCP\Log\ILogFactory::getCustomPsrLogger - */ - public function getCustomLogger(string $path): ILogger; - - /** - * @param string $path * @param string $type * @param string $tag * @return LoggerInterface |