Browse Source

fix!: Migrate jobs away from deprecated interfaces

BREAKING CHANGE: Removed ILogFactory::getCustomLogger deprecated method

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
tags/v29.0.0beta1
Côme Chilliet 4 months ago
parent
commit
166773879b

+ 15
- 36
apps/dav/lib/BackgroundJob/RefreshWebcalJob.php View File

@@ -6,6 +6,7 @@ declare(strict_types=1);
* @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Citharel <nextcloud@tcit.fr>
@@ -34,42 +35,18 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job;
use OCP\IConfig;
use OCP\ILogger;
use Psr\Log\LoggerInterface;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\InvalidDataException;

class RefreshWebcalJob extends Job {

/**
* @var RefreshWebcalService
*/
private $refreshWebcalService;

/**
* @var IConfig
*/
private $config;

/** @var ILogger */
private $logger;

/** @var ITimeFactory */
private $timeFactory;

/**
* RefreshWebcalJob constructor.
*
* @param RefreshWebcalService $refreshWebcalService
* @param IConfig $config
* @param ILogger $logger
* @param ITimeFactory $timeFactory
*/
public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) {
public function __construct(
private RefreshWebcalService $refreshWebcalService,
private IConfig $config,
private LoggerInterface $logger,
ITimeFactory $timeFactory,
) {
parent::__construct($timeFactory);
$this->refreshWebcalService = $refreshWebcalService;
$this->config = $config;
$this->logger = $logger;
$this->timeFactory = $timeFactory;
}

/**
@@ -77,7 +54,7 @@ class RefreshWebcalJob extends Job {
*
* @inheritdoc
*/
public function execute(IJobList $jobList, ILogger $logger = null) {
public function start(IJobList $jobList): void {
$subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
if (!$subscription) {
return;
@@ -95,17 +72,19 @@ class RefreshWebcalJob extends Job {
/** @var DateInterval $dateInterval */
$dateInterval = DateTimeParser::parseDuration($refreshRate);
} catch (InvalidDataException $ex) {
$this->logger->logException($ex);
$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
$this->logger->error(
"Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid",
['exception' => $ex]
);
return;
}

$interval = $this->getIntervalFromDateInterval($dateInterval);
if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) {
if (($this->time->getTime() - $this->lastRun) <= $interval) {
return;
}

parent::execute($jobList, $logger);
parent::start($jobList);
}

/**

+ 17
- 38
apps/dav/lib/Migration/BuildCalendarSearchIndexBackgroundJob.php View File

@@ -1,7 +1,11 @@
<?php

declare(strict_types=1);

/**
* @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Joas Schilling <coding@schilljs.com>
*
@@ -23,47 +27,22 @@
*/
namespace OCA\DAV\Migration;

use OC\BackgroundJob\QueuedJob;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\IDBConnection;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {

/** @var IDBConnection */
private $db;

/** @var CalDavBackend */
private $calDavBackend;

/** @var ILogger */
private $logger;

/** @var IJobList */
private $jobList;

/** @var ITimeFactory */
private $timeFactory;

/**
* @param IDBConnection $db
* @param CalDavBackend $calDavBackend
* @param ILogger $logger
* @param IJobList $jobList
* @param ITimeFactory $timeFactory
*/
public function __construct(IDBConnection $db,
CalDavBackend $calDavBackend,
ILogger $logger,
IJobList $jobList,
ITimeFactory $timeFactory) {
$this->db = $db;
$this->calDavBackend = $calDavBackend;
$this->logger = $logger;
$this->jobList = $jobList;
$this->timeFactory = $timeFactory;
public function __construct(
private IDBConnection $db,
private CalDavBackend $calDavBackend,
private LoggerInterface $logger,
private IJobList $jobList,
ITimeFactory $timeFactory
) {
parent::__construct($timeFactory);
}

public function run($arguments) {
@@ -72,8 +51,8 @@ class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {

$this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');

$startTime = $this->timeFactory->getTime();
while (($this->timeFactory->getTime() - $startTime) < 15) {
$startTime = $this->time->getTime();
while (($this->time->getTime() - $startTime) < 15) {
$offset = $this->buildIndex($offset, $stopAt);
if ($offset >= $stopAt) {
break;
@@ -105,7 +84,7 @@ class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
->orderBy('id', 'ASC')
->setMaxResults(500);

$result = $query->execute();
$result = $query->executeQuery();
while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
$offset = $row['id'];


+ 17
- 38
apps/dav/lib/Migration/BuildSocialSearchIndexBackgroundJob.php View File

@@ -1,8 +1,12 @@
<?php

declare(strict_types=1);

/**
* @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @author call-me-matt <nextcloud@matthiasheinisch.de>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
@@ -22,47 +26,22 @@
*/
namespace OCA\DAV\Migration;

use OC\BackgroundJob\QueuedJob;
use OCA\DAV\CardDAV\CardDavBackend;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\IDBConnection;
use OCP\ILogger;
use Psr\Log\LoggerInterface;

class BuildSocialSearchIndexBackgroundJob extends QueuedJob {

/** @var IDBConnection */
private $db;

/** @var CardDavBackend */
private $davBackend;

/** @var ILogger */
private $logger;

/** @var IJobList */
private $jobList;

/** @var ITimeFactory */
private $timeFactory;

/**
* @param IDBConnection $db
* @param CardDavBackend $davBackend
* @param ILogger $logger
* @param IJobList $jobList
* @param ITimeFactory $timeFactory
*/
public function __construct(IDBConnection $db,
CardDavBackend $davBackend,
ILogger $logger,
IJobList $jobList,
ITimeFactory $timeFactory) {
$this->db = $db;
$this->davBackend = $davBackend;
$this->logger = $logger;
$this->jobList = $jobList;
$this->timeFactory = $timeFactory;
public function __construct(
private IDBConnection $db,
private CardDavBackend $davBackend,
private LoggerInterface $logger,
private IJobList $jobList,
ITimeFactory $timeFactory,
) {
parent::__construct($timeFactory);
}

public function run($arguments) {
@@ -90,7 +69,7 @@ class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
* @return int
*/
private function buildIndex($offset, $stopAt) {
$startTime = $this->timeFactory->getTime();
$startTime = $this->time->getTime();

// get contacts with social profiles
$query = $this->db->getQueryBuilder();
@@ -99,7 +78,7 @@ class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
->orderBy('id', 'ASC')
->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%')))
->setMaxResults(100);
$social_cards = $query->execute()->fetchAll();
$social_cards = $query->executeQuery()->fetchAll();

if (empty($social_cards)) {
return $stopAt;
@@ -111,7 +90,7 @@ class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
$this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $contact['carddata']);

// stop after 15sec (to be continued with next chunk)
if (($this->timeFactory->getTime() - $startTime) > 15) {
if (($this->time->getTime() - $startTime) > 15) {
break;
}
}

+ 4
- 5
apps/dav/tests/unit/BackgroundJob/RefreshWebcalJobTest.php View File

@@ -33,8 +33,8 @@ use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\ILogger;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

use Test\TestCase;

@@ -46,8 +46,7 @@ class RefreshWebcalJobTest extends TestCase {
/** @var IConfig | MockObject */
private $config;

/** @var ILogger | MockObject */
private $logger;
private LoggerInterface $logger;

/** @var ITimeFactory | MockObject */
private $timeFactory;
@@ -60,7 +59,7 @@ class RefreshWebcalJobTest extends TestCase {

$this->refreshWebcalService = $this->createMock(RefreshWebcalService::class);
$this->config = $this->createMock(IConfig::class);
$this->logger = $this->createMock(ILogger::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);

$this->jobList = $this->createMock(IJobList::class);
@@ -115,7 +114,7 @@ class RefreshWebcalJobTest extends TestCase {
->with('principals/users/testuser', 'sub123');
}

$backgroundJob->execute($this->jobList, $this->logger);
$backgroundJob->start($this->jobList);
}

/**

+ 18
- 42
apps/settings/lib/BackgroundJobs/VerifyUserData.php View File

@@ -1,10 +1,14 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Bjoern Schiessle <bjoern@schiessle.org>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Joas Schilling <coding@schilljs.com>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Morris Jobke <hey@morrisjobke.de>
@@ -37,65 +41,37 @@ use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

class VerifyUserData extends Job {

/** @var bool */
private $retainJob = true;
private bool $retainJob = true;

/** @var int max number of attempts to send the request */
private $maxTry = 24;
private int $maxTry = 24;

/** @var int how much time should be between two tries (1 hour) */
private $interval = 3600;

/** @var IAccountManager */
private $accountManager;

/** @var IUserManager */
private $userManager;

/** @var IClientService */
private $httpClientService;

/** @var ILogger */
private $logger;

/** @var string */
private $lookupServerUrl;

/** @var IConfig */
private $config;

public function __construct(IAccountManager $accountManager,
IUserManager $userManager,
IClientService $clientService,
ILogger $logger,
private int $interval = 3600;
private string $lookupServerUrl;

public function __construct(
private IAccountManager $accountManager,
private IUserManager $userManager,
private IClientService $httpClientService,
private LoggerInterface $logger,
ITimeFactory $timeFactory,
IConfig $config
private IConfig $config,
) {
parent::__construct($timeFactory);
$this->accountManager = $accountManager;
$this->userManager = $userManager;
$this->httpClientService = $clientService;
$this->logger = $logger;

$lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
$this->lookupServerUrl = rtrim($lookupServerUrl, '/');
$this->config = $config;
}

/**
* run the job, then remove it from the jobList
*
* @param IJobList $jobList
* @param ILogger|null $logger
*/
public function execute(IJobList $jobList, ILogger $logger = null) {
public function start(IJobList $jobList): void {
if ($this->shouldRun($this->argument)) {
parent::execute($jobList, $logger);
parent::start($jobList);
$jobList->remove($this, $this->argument);
if ($this->retainJob) {
$this->reAddJob($jobList, $this->argument);

+ 3
- 6
apps/user_ldap/lib/Migration/UUIDFix.php View File

@@ -23,17 +23,14 @@
*/
namespace OCA\User_LDAP\Migration;

use OC\BackgroundJob\QueuedJob;
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\Proxy;
use OCA\User_LDAP\User_Proxy;
use OCP\BackgroundJob\QueuedJob;

abstract class UUIDFix extends QueuedJob {
/** @var AbstractMapping */
protected $mapper;

/** @var Proxy */
protected $proxy;
protected AbstractMapping $mapper;
protected Proxy $proxy;

public function run($argument) {
$isUser = $this->proxy instanceof User_Proxy;

+ 3
- 1
apps/user_ldap/lib/Migration/UUIDFixGroup.php View File

@@ -24,9 +24,11 @@ namespace OCA\User_LDAP\Migration;

use OCA\User_LDAP\Group_Proxy;
use OCA\User_LDAP\Mapping\GroupMapping;
use OCP\AppFramework\Utility\ITimeFactory;

class UUIDFixGroup extends UUIDFix {
public function __construct(GroupMapping $mapper, Group_Proxy $proxy) {
public function __construct(ITimeFactory $time, GroupMapping $mapper, Group_Proxy $proxy) {
parent::__construct($time);
$this->mapper = $mapper;
$this->proxy = $proxy;
}

+ 3
- 1
apps/user_ldap/lib/Migration/UUIDFixUser.php View File

@@ -24,9 +24,11 @@ namespace OCA\User_LDAP\Migration;

use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User_Proxy;
use OCP\AppFramework\Utility\ITimeFactory;

class UUIDFixUser extends UUIDFix {
public function __construct(UserMapping $mapper, User_Proxy $proxy) {
public function __construct(ITimeFactory $time, UserMapping $mapper, User_Proxy $proxy) {
parent::__construct($time);
$this->mapper = $mapper;
$this->proxy = $proxy;
}

+ 15
- 28
apps/user_ldap/tests/Migration/AbstractUUIDFixTest.php View File

@@ -27,37 +27,23 @@ namespace OCA\User_LDAP\Tests\Migration;
use OCA\User_LDAP\Access;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Mapping\GroupMapping;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\Migration\UUIDFixUser;
use OCA\User_LDAP\User_Proxy;
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\Migration\UUIDFix;
use OCA\User_LDAP\Proxy;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use Test\TestCase;

abstract class AbstractUUIDFixTest extends TestCase {
/** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
protected $helper;

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;

/** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
protected $ldap;

/** @var UserMapping|GroupMapping|\PHPUnit\Framework\MockObject\MockObject */
protected $mapper;

/** @var UUIDFixUser */
protected $job;

/** @var User_Proxy|\PHPUnit\Framework\MockObject\MockObject */
protected $proxy;

/** @var Access|\PHPUnit\Framework\MockObject\MockObject */
protected $access;

/** @var bool */
protected $isUser = true;
protected Helper $helper;
protected IConfig $config;
protected LDAP $ldap;
protected AbstractMapping $mapper;
protected UUIDFix $job;
protected Proxy $proxy;
protected Access $access;
protected ITimeFactory $time;
protected bool $isUser = true;

protected function setUp(): void {
parent::setUp();
@@ -65,6 +51,7 @@ abstract class AbstractUUIDFixTest extends TestCase {
$this->ldap = $this->createMock(LDAP::class);
$this->config = $this->createMock(IConfig::class);
$this->access = $this->createMock(Access::class);
$this->time = $this->createMock(ITimeFactory::class);

$this->helper = $this->createMock(Helper::class);
$this->helper->expects($this->any())
@@ -74,7 +61,7 @@ abstract class AbstractUUIDFixTest extends TestCase {
}

protected function instantiateJob($className) {
$this->job = new $className($this->mapper, $this->proxy);
$this->job = new $className($this->time, $this->mapper, $this->proxy);
$this->proxy->expects($this->any())
->method('getLDAPAccess')
->willReturn($this->access);

+ 1
- 3
core/Command/Background/Job.php View File

@@ -27,7 +27,6 @@ namespace OC\Core\Command\Background;

use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\ILogger;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@@ -37,7 +36,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Job extends Command {
public function __construct(
protected IJobList $jobList,
protected ILogger $logger,
) {
parent::__construct();
}
@@ -86,7 +84,7 @@ class Job extends Command {
$output->writeln('<error>Something went wrong when trying to retrieve Job with ID ' . $jobId . ' from database</error>');
return 1;
}
$job->execute($this->jobList, $this->logger);
$job->start($this->jobList);
$job = $this->jobList->getById($jobId);

if (($job === null) || ($lastRun !== $job->getLastRun())) {

+ 7
- 5
core/ajax/update.php View File

@@ -43,8 +43,8 @@ use OCP\EventDispatcher\IEventDispatcher;
use OCP\IEventSource;
use OCP\IEventSourceFactory;
use OCP\IL10N;
use OCP\ILogger;
use OCP\L10N\IFactory;
use Psr\Log\LoggerInterface;

if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
@set_time_limit(0);
@@ -186,10 +186,12 @@ if (\OCP\Util::needUpgrade()) {
try {
$updater->upgrade();
} catch (\Exception $e) {
\OC::$server->getLogger()->logException($e, [
'level' => ILogger::ERROR,
'app' => 'update',
]);
\OCP\Server::get(LoggerInterface::class)->error(
$e->getMessage(),
[
'exception' => $e,
'app' => 'update',
]);
$eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
$eventSource->close();
exit();

+ 6
- 7
lib/public/BackgroundJob/Job.php View File

@@ -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
*/

+ 0
- 10
lib/public/Log/ILogFactory.php View File

@@ -24,7 +24,6 @@
*/
namespace OCP\Log;

use OCP\ILogger;
use Psr\Log\LoggerInterface;

/**
@@ -40,15 +39,6 @@ interface ILogFactory {
*/
public function get(string $type): IWriter;

/**
* @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

Loading…
Cancel
Save