Merge pull request #43387 from nextcloud/fix/migrate-away-from-ilogger-in-jobs

pull/43465/head
Côme Chilliet 10 months ago committed by Louis Chemineau
parent f6ed1effbf
commit 303e2febc7
No known key found for this signature in database
  1. 51
      apps/dav/lib/BackgroundJob/RefreshWebcalJob.php
  2. 55
      apps/dav/lib/Migration/BuildCalendarSearchIndexBackgroundJob.php
  3. 55
      apps/dav/lib/Migration/BuildSocialSearchIndexBackgroundJob.php
  4. 9
      apps/dav/tests/unit/BackgroundJob/RefreshWebcalJobTest.php
  5. 2
      apps/federation/tests/BackgroundJob/GetSharedSecretTest.php
  6. 60
      apps/settings/lib/BackgroundJobs/VerifyUserData.php
  7. 9
      apps/user_ldap/lib/Migration/UUIDFix.php
  8. 4
      apps/user_ldap/lib/Migration/UUIDFixGroup.php
  9. 4
      apps/user_ldap/lib/Migration/UUIDFixUser.php
  10. 43
      apps/user_ldap/tests/Migration/AbstractUUIDFixTest.php
  11. 10
      core/Command/Background/Job.php
  12. 12
      core/ajax/update.php
  13. 4
      cron.php
  14. 3
      lib/composer/composer/autoload_classmap.php
  15. 3
      lib/composer/composer/autoload_static.php
  16. 98
      lib/private/BackgroundJob/Job.php
  17. 49
      lib/private/BackgroundJob/QueuedJob.php
  18. 63
      lib/private/BackgroundJob/TimedJob.php
  19. 2
      lib/private/Command/CallableJob.php
  20. 2
      lib/private/Command/ClosureJob.php
  21. 2
      lib/private/Command/CommandJob.php
  22. 28
      lib/private/Command/CronBus.php
  23. 47
      lib/private/Repair/Owncloud/CleanPreviewsBackgroundJob.php
  24. 34
      lib/private/Repair/Owncloud/MoveAvatarsBackgroundJob.php
  25. 4
      lib/public/BackgroundJob/IJobList.php
  26. 13
      lib/public/BackgroundJob/Job.php
  27. 10
      lib/public/Log/ILogFactory.php
  28. 2
      tests/lib/BackgroundJob/DummyJobList.php
  29. 22
      tests/lib/BackgroundJob/JobTest.php
  30. 34
      tests/lib/BackgroundJob/QueuedJobTest.php
  31. 2
      tests/lib/BackgroundJob/TestJob.php
  32. 67
      tests/lib/BackgroundJob/TimedJobTest.php
  33. 2
      tests/lib/Command/CronBusTest.php

@ -6,6 +6,7 @@ declare(strict_types=1);
* @copyright 2018 Georg Ehrke <oc.list@georgehrke.com> * @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
* *
* @author Christoph Wurst <christoph@winzerhof-wurst.at> * @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Georg Ehrke <oc.list@georgehrke.com> * @author Georg Ehrke <oc.list@georgehrke.com>
* @author Roeland Jago Douma <roeland@famdouma.nl> * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Citharel <nextcloud@tcit.fr> * @author Thomas Citharel <nextcloud@tcit.fr>
@ -34,42 +35,18 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job; use OCP\BackgroundJob\Job;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use Psr\Log\LoggerInterface;
use Sabre\VObject\DateTimeParser; use Sabre\VObject\DateTimeParser;
use Sabre\VObject\InvalidDataException; use Sabre\VObject\InvalidDataException;
class RefreshWebcalJob extends Job { class RefreshWebcalJob extends Job {
public function __construct(
/** private RefreshWebcalService $refreshWebcalService,
* @var RefreshWebcalService private IConfig $config,
*/ private LoggerInterface $logger,
private $refreshWebcalService; ITimeFactory $timeFactory,
) {
/**
* @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) {
parent::__construct($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 * @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']); $subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
if (!$subscription) { if (!$subscription) {
return; return;
@ -95,17 +72,19 @@ class RefreshWebcalJob extends Job {
/** @var DateInterval $dateInterval */ /** @var DateInterval $dateInterval */
$dateInterval = DateTimeParser::parseDuration($refreshRate); $dateInterval = DateTimeParser::parseDuration($refreshRate);
} catch (InvalidDataException $ex) { } catch (InvalidDataException $ex) {
$this->logger->logException($ex); $this->logger->error(
$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid"); "Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid",
['exception' => $ex]
);
return; return;
} }
$interval = $this->getIntervalFromDateInterval($dateInterval); $interval = $this->getIntervalFromDateInterval($dateInterval);
if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) { if (($this->time->getTime() - $this->lastRun) <= $interval) {
return; return;
} }
parent::execute($jobList, $logger); parent::start($jobList);
} }
/** /**

@ -1,7 +1,11 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright 2017 Georg Ehrke <oc.list@georgehrke.com> * @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
* *
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Georg Ehrke <oc.list@georgehrke.com> * @author Georg Ehrke <oc.list@georgehrke.com>
* @author Joas Schilling <coding@schilljs.com> * @author Joas Schilling <coding@schilljs.com>
* *
@ -23,47 +27,22 @@
*/ */
namespace OCA\DAV\Migration; namespace OCA\DAV\Migration;
use OC\BackgroundJob\QueuedJob;
use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\CalDavBackend;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\ILogger; use Psr\Log\LoggerInterface;
class BuildCalendarSearchIndexBackgroundJob extends QueuedJob { class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
public function __construct(
/** @var IDBConnection */ private IDBConnection $db,
private $db; private CalDavBackend $calDavBackend,
private LoggerInterface $logger,
/** @var CalDavBackend */ private IJobList $jobList,
private $calDavBackend; ITimeFactory $timeFactory
) {
/** @var ILogger */ parent::__construct($timeFactory);
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 run($arguments) { public function run($arguments) {
@ -72,8 +51,8 @@ class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
$this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')'); $this->logger->info('Building calendar index (' . $offset .'/' . $stopAt . ')');
$startTime = $this->timeFactory->getTime(); $startTime = $this->time->getTime();
while (($this->timeFactory->getTime() - $startTime) < 15) { while (($this->time->getTime() - $startTime) < 15) {
$offset = $this->buildIndex($offset, $stopAt); $offset = $this->buildIndex($offset, $stopAt);
if ($offset >= $stopAt) { if ($offset >= $stopAt) {
break; break;
@ -105,7 +84,7 @@ class BuildCalendarSearchIndexBackgroundJob extends QueuedJob {
->orderBy('id', 'ASC') ->orderBy('id', 'ASC')
->setMaxResults(500); ->setMaxResults(500);
$result = $query->execute(); $result = $query->executeQuery();
while ($row = $result->fetch(\PDO::FETCH_ASSOC)) { while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
$offset = $row['id']; $offset = $row['id'];

@ -1,8 +1,12 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de> * @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
* *
* @author call-me-matt <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 * @license GNU AGPL version 3 or any later version
* *
@ -22,47 +26,22 @@
*/ */
namespace OCA\DAV\Migration; namespace OCA\DAV\Migration;
use OC\BackgroundJob\QueuedJob;
use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\CardDavBackend;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\ILogger; use Psr\Log\LoggerInterface;
class BuildSocialSearchIndexBackgroundJob extends QueuedJob { class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
public function __construct(
/** @var IDBConnection */ private IDBConnection $db,
private $db; private CardDavBackend $davBackend,
private LoggerInterface $logger,
/** @var CardDavBackend */ private IJobList $jobList,
private $davBackend; ITimeFactory $timeFactory,
) {
/** @var ILogger */ parent::__construct($timeFactory);
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 run($arguments) { public function run($arguments) {
@ -90,7 +69,7 @@ class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
* @return int * @return int
*/ */
private function buildIndex($offset, $stopAt) { private function buildIndex($offset, $stopAt) {
$startTime = $this->timeFactory->getTime(); $startTime = $this->time->getTime();
// get contacts with social profiles // get contacts with social profiles
$query = $this->db->getQueryBuilder(); $query = $this->db->getQueryBuilder();
@ -99,7 +78,7 @@ class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
->orderBy('id', 'ASC') ->orderBy('id', 'ASC')
->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%'))) ->where($query->expr()->like('carddata', $query->createNamedParameter('%SOCIALPROFILE%')))
->setMaxResults(100); ->setMaxResults(100);
$social_cards = $query->execute()->fetchAll(); $social_cards = $query->executeQuery()->fetchAll();
if (empty($social_cards)) { if (empty($social_cards)) {
return $stopAt; return $stopAt;
@ -111,7 +90,7 @@ class BuildSocialSearchIndexBackgroundJob extends QueuedJob {
$this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $contact['carddata']); $this->davBackend->updateCard($contact['addressbookid'], $contact['uri'], $contact['carddata']);
// stop after 15sec (to be continued with next chunk) // stop after 15sec (to be continued with next chunk)
if (($this->timeFactory->getTime() - $startTime) > 15) { if (($this->time->getTime() - $startTime) > 15) {
break; break;
} }
} }

@ -33,8 +33,8 @@ use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase; use Test\TestCase;
@ -46,8 +46,7 @@ class RefreshWebcalJobTest extends TestCase {
/** @var IConfig | MockObject */ /** @var IConfig | MockObject */
private $config; private $config;
/** @var ILogger | MockObject */ private LoggerInterface $logger;
private $logger;
/** @var ITimeFactory | MockObject */ /** @var ITimeFactory | MockObject */
private $timeFactory; private $timeFactory;
@ -60,7 +59,7 @@ class RefreshWebcalJobTest extends TestCase {
$this->refreshWebcalService = $this->createMock(RefreshWebcalService::class); $this->refreshWebcalService = $this->createMock(RefreshWebcalService::class);
$this->config = $this->createMock(IConfig::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->timeFactory = $this->createMock(ITimeFactory::class);
$this->jobList = $this->createMock(IJobList::class); $this->jobList = $this->createMock(IJobList::class);
@ -115,7 +114,7 @@ class RefreshWebcalJobTest extends TestCase {
->with('principals/users/testuser', 'sub123'); ->with('principals/users/testuser', 'sub123');
} }
$backgroundJob->execute($this->jobList, $this->logger); $backgroundJob->start($this->jobList);
} }
/** /**

@ -155,7 +155,7 @@ class GetSharedSecretTest extends TestCase {
$this->jobList->expects($this->never())->method('add'); $this->jobList->expects($this->never())->method('add');
} }
$getSharedSecret->execute($this->jobList); $getSharedSecret->start($this->jobList);
} }
public function dataTestExecute() { public function dataTestExecute() {

@ -1,10 +1,14 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org> * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
* *
* @author Arthur Schiwon <blizzz@arthur-schiwon.de> * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Bjoern Schiessle <bjoern@schiessle.org> * @author Bjoern Schiessle <bjoern@schiessle.org>
* @author Christoph Wurst <christoph@winzerhof-wurst.at> * @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
* @author Joas Schilling <coding@schilljs.com> * @author Joas Schilling <coding@schilljs.com>
* @author Lukas Reschke <lukas@statuscode.ch> * @author Lukas Reschke <lukas@statuscode.ch>
* @author Morris Jobke <hey@morrisjobke.de> * @author Morris Jobke <hey@morrisjobke.de>
@ -37,65 +41,37 @@ use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\Job; use OCP\BackgroundJob\Job;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserManager; use OCP\IUserManager;
use Psr\Log\LoggerInterface;
class VerifyUserData extends Job { class VerifyUserData extends Job {
/** @var bool */ /** @var bool */
private $retainJob = true; private bool $retainJob = true;
/** @var int max number of attempts to send the request */ /** @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) */ /** @var int how much time should be between two tries (1 hour) */
private $interval = 3600; private int $interval = 3600;
private string $lookupServerUrl;
/** @var IAccountManager */
private $accountManager; public function __construct(
private IAccountManager $accountManager,
/** @var IUserManager */ private IUserManager $userManager,
private $userManager; private IClientService $httpClientService,
private LoggerInterface $logger,
/** @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,
ITimeFactory $timeFactory, ITimeFactory $timeFactory,
IConfig $config private IConfig $config,
) { ) {
parent::__construct($timeFactory); parent::__construct($timeFactory);
$this->accountManager = $accountManager;
$this->userManager = $userManager;
$this->httpClientService = $clientService;
$this->logger = $logger;
$lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com'); $lookupServerUrl = $config->getSystemValue('lookup_server', 'https://lookup.nextcloud.com');
$this->lookupServerUrl = rtrim($lookupServerUrl, '/'); $this->lookupServerUrl = rtrim($lookupServerUrl, '/');
$this->config = $config;
} }
/** public function start(IJobList $jobList): void {
* run the job, then remove it from the jobList
*
* @param IJobList $jobList
* @param ILogger|null $logger
*/
public function execute(IJobList $jobList, ILogger $logger = null) {
if ($this->shouldRun($this->argument)) { if ($this->shouldRun($this->argument)) {
parent::execute($jobList, $logger); parent::start($jobList);
$jobList->remove($this, $this->argument); $jobList->remove($this, $this->argument);
if ($this->retainJob) { if ($this->retainJob) {
$this->reAddJob($jobList, $this->argument); $this->reAddJob($jobList, $this->argument);

@ -23,17 +23,14 @@
*/ */
namespace OCA\User_LDAP\Migration; namespace OCA\User_LDAP\Migration;
use OC\BackgroundJob\QueuedJob;
use OCA\User_LDAP\Mapping\AbstractMapping; use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\Proxy; use OCA\User_LDAP\Proxy;
use OCA\User_LDAP\User_Proxy; use OCA\User_LDAP\User_Proxy;
use OCP\BackgroundJob\QueuedJob;
abstract class UUIDFix extends QueuedJob { abstract class UUIDFix extends QueuedJob {
/** @var AbstractMapping */ protected AbstractMapping $mapper;
protected $mapper; protected Proxy $proxy;
/** @var Proxy */
protected $proxy;
public function run($argument) { public function run($argument) {
$isUser = $this->proxy instanceof User_Proxy; $isUser = $this->proxy instanceof User_Proxy;

@ -24,9 +24,11 @@ namespace OCA\User_LDAP\Migration;
use OCA\User_LDAP\Group_Proxy; use OCA\User_LDAP\Group_Proxy;
use OCA\User_LDAP\Mapping\GroupMapping; use OCA\User_LDAP\Mapping\GroupMapping;
use OCP\AppFramework\Utility\ITimeFactory;
class UUIDFixGroup extends UUIDFix { 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->mapper = $mapper;
$this->proxy = $proxy; $this->proxy = $proxy;
} }

@ -24,9 +24,11 @@ namespace OCA\User_LDAP\Migration;
use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User_Proxy; use OCA\User_LDAP\User_Proxy;
use OCP\AppFramework\Utility\ITimeFactory;
class UUIDFixUser extends UUIDFix { 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->mapper = $mapper;
$this->proxy = $proxy; $this->proxy = $proxy;
} }

@ -27,37 +27,23 @@ namespace OCA\User_LDAP\Tests\Migration;
use OCA\User_LDAP\Access; use OCA\User_LDAP\Access;
use OCA\User_LDAP\Helper; use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP; use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Mapping\GroupMapping; use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Migration\UUIDFix;
use OCA\User_LDAP\Migration\UUIDFixUser; use OCA\User_LDAP\Proxy;
use OCA\User_LDAP\User_Proxy; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig; use OCP\IConfig;
use Test\TestCase; use Test\TestCase;
abstract class AbstractUUIDFixTest extends TestCase { abstract class AbstractUUIDFixTest extends TestCase {
/** @var Helper|\PHPUnit\Framework\MockObject\MockObject */ protected Helper $helper;
protected $helper; protected IConfig $config;
protected LDAP $ldap;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ protected AbstractMapping $mapper;
protected $config; protected UUIDFix $job;
protected Proxy $proxy;
/** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */ protected Access $access;
protected $ldap; protected ITimeFactory $time;
protected bool $isUser = true;
/** @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 function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -65,6 +51,7 @@ abstract class AbstractUUIDFixTest extends TestCase {
$this->ldap = $this->createMock(LDAP::class); $this->ldap = $this->createMock(LDAP::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->access = $this->createMock(Access::class); $this->access = $this->createMock(Access::class);
$this->time = $this->createMock(ITimeFactory::class);
$this->helper = $this->createMock(Helper::class); $this->helper = $this->createMock(Helper::class);
$this->helper->expects($this->any()) $this->helper->expects($this->any())
@ -74,7 +61,7 @@ abstract class AbstractUUIDFixTest extends TestCase {
} }
protected function instantiateJob($className) { 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()) $this->proxy->expects($this->any())
->method('getLDAPAccess') ->method('getLDAPAccess')
->willReturn($this->access); ->willReturn($this->access);

@ -27,7 +27,6 @@ namespace OC\Core\Command\Background;
use OCP\BackgroundJob\IJob; use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\ILogger;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -37,7 +36,6 @@ use Symfony\Component\Console\Output\OutputInterface;
class Job extends Command { class Job extends Command {
public function __construct( public function __construct(
protected IJobList $jobList, protected IJobList $jobList,
protected ILogger $logger,
) { ) {
parent::__construct(); parent::__construct();
} }
@ -86,14 +84,14 @@ class Job extends Command {
$output->writeln('<error>Something went wrong when trying to retrieve Job with ID ' . $jobId . ' from database</error>'); $output->writeln('<error>Something went wrong when trying to retrieve Job with ID ' . $jobId . ' from database</error>');
return 1; return 1;
} }
$job->execute($this->jobList, $this->logger); $job->start($this->jobList);
$job = $this->jobList->getById($jobId); $job = $this->jobList->getById($jobId);
if (($job === null) || ($lastRun !== $job->getLastRun())) { if (($job === null) || ($lastRun !== $job->getLastRun())) {
$output->writeln('<info>Job executed!</info>'); $output->writeln('<info>Job executed!</info>');
$output->writeln(''); $output->writeln('');
if ($job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob) { if ($job instanceof \OCP\BackgroundJob\TimedJob) {
$this->printJobInfo($jobId, $job, $output); $this->printJobInfo($jobId, $job, $output);
} }
} else { } else {
@ -117,10 +115,10 @@ class Job extends Command {
$output->writeln('Job class: ' . get_class($job)); $output->writeln('Job class: ' . get_class($job));
$output->writeln('Arguments: ' . json_encode($job->getArgument())); $output->writeln('Arguments: ' . json_encode($job->getArgument()));
$isTimedJob = $job instanceof \OC\BackgroundJob\TimedJob || $job instanceof \OCP\BackgroundJob\TimedJob; $isTimedJob = $job instanceof \OCP\BackgroundJob\TimedJob;
if ($isTimedJob) { if ($isTimedJob) {
$output->writeln('Type: timed'); $output->writeln('Type: timed');
} elseif ($job instanceof \OC\BackgroundJob\QueuedJob || $job instanceof \OCP\BackgroundJob\QueuedJob) { } elseif ($job instanceof \OCP\BackgroundJob\QueuedJob) {
$output->writeln('Type: queued'); $output->writeln('Type: queued');
} else { } else {
$output->writeln('Type: job'); $output->writeln('Type: job');

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

@ -172,7 +172,7 @@ try {
$memoryBefore = memory_get_usage(); $memoryBefore = memory_get_usage();
$memoryPeakBefore = memory_get_peak_usage(); $memoryPeakBefore = memory_get_peak_usage();
$job->execute($jobList, $logger); $job->start($jobList);
$memoryAfter = memory_get_usage(); $memoryAfter = memory_get_usage();
$memoryPeakAfter = memory_get_peak_usage(); $memoryPeakAfter = memory_get_peak_usage();
@ -207,7 +207,7 @@ try {
$job = $jobList->getNext(); $job = $jobList->getNext();
if ($job != null) { if ($job != null) {
$logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']); $logger->debug('WebCron call has selected job with ID ' . strval($job->getId()), ['app' => 'cron']);
$job->execute($jobList, $logger); $job->start($jobList);
$jobList->setLastJob($job); $jobList->setLastJob($job);
} }
OC_JSON::success(); OC_JSON::success();

@ -955,10 +955,7 @@ return array(
'OC\\Avatar\\GuestAvatar' => $baseDir . '/lib/private/Avatar/GuestAvatar.php', 'OC\\Avatar\\GuestAvatar' => $baseDir . '/lib/private/Avatar/GuestAvatar.php',
'OC\\Avatar\\PlaceholderAvatar' => $baseDir . '/lib/private/Avatar/PlaceholderAvatar.php', 'OC\\Avatar\\PlaceholderAvatar' => $baseDir . '/lib/private/Avatar/PlaceholderAvatar.php',
'OC\\Avatar\\UserAvatar' => $baseDir . '/lib/private/Avatar/UserAvatar.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\\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\\BinaryFinder' => $baseDir . '/lib/private/BinaryFinder.php',
'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => $baseDir . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php', 'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => $baseDir . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php',
'OC\\Broadcast\\Events\\BroadcastEvent' => $baseDir . '/lib/private/Broadcast/Events/BroadcastEvent.php', 'OC\\Broadcast\\Events\\BroadcastEvent' => $baseDir . '/lib/private/Broadcast/Events/BroadcastEvent.php',

@ -988,10 +988,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Avatar\\GuestAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/GuestAvatar.php', 'OC\\Avatar\\GuestAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/GuestAvatar.php',
'OC\\Avatar\\PlaceholderAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/PlaceholderAvatar.php', 'OC\\Avatar\\PlaceholderAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/PlaceholderAvatar.php',
'OC\\Avatar\\UserAvatar' => __DIR__ . '/../../..' . '/lib/private/Avatar/UserAvatar.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\\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\\BinaryFinder' => __DIR__ . '/../../..' . '/lib/private/BinaryFinder.php',
'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => __DIR__ . '/../../..' . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php', 'OC\\Blurhash\\Listener\\GenerateBlurhashMetadata' => __DIR__ . '/../../..' . '/lib/private/Blurhash/Listener/GenerateBlurhashMetadata.php',
'OC\\Broadcast\\Events\\BroadcastEvent' => __DIR__ . '/../../..' . '/lib/private/Broadcast/Events/BroadcastEvent.php', 'OC\\Broadcast\\Events\\BroadcastEvent' => __DIR__ . '/../../..' . '/lib/private/Broadcast/Events/BroadcastEvent.php',

@ -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;
}
}

@ -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);
}
}

@ -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);
}
}
}

@ -21,7 +21,7 @@
*/ */
namespace OC\Command; namespace OC\Command;
use OC\BackgroundJob\QueuedJob; use OCP\BackgroundJob\QueuedJob;
class CallableJob extends QueuedJob { class CallableJob extends QueuedJob {
protected function run($serializedCallable) { protected function run($serializedCallable) {

@ -23,7 +23,7 @@
namespace OC\Command; namespace OC\Command;
use Laravel\SerializableClosure\SerializableClosure as LaravelClosure; use Laravel\SerializableClosure\SerializableClosure as LaravelClosure;
use OC\BackgroundJob\QueuedJob; use OCP\BackgroundJob\QueuedJob;
class ClosureJob extends QueuedJob { class ClosureJob extends QueuedJob {
protected function run($argument) { protected function run($argument) {

@ -22,7 +22,7 @@
*/ */
namespace OC\Command; namespace OC\Command;
use OC\BackgroundJob\QueuedJob; use OCP\BackgroundJob\QueuedJob;
use OCP\Command\ICommand; use OCP\Command\ICommand;
/** /**

@ -26,31 +26,25 @@
namespace OC\Command; namespace OC\Command;
use Laravel\SerializableClosure\SerializableClosure; use Laravel\SerializableClosure\SerializableClosure;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\IJobList;
use OCP\Command\ICommand; use OCP\Command\ICommand;
class CronBus extends AsyncBus { class CronBus extends AsyncBus {
/** public function __construct(
* @var \OCP\BackgroundJob\IJobList private IJobList $jobList,
*/ ) {
private $jobList;
/**
* @param \OCP\BackgroundJob\IJobList $jobList
*/
public function __construct($jobList) {
$this->jobList = $jobList;
} }
protected function queueCommand($command) { protected function queueCommand($command): void {
$this->jobList->add($this->getJobClass($command), $this->serializeCommand($command)); $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
} }
/** /**
* @param \OCP\Command\ICommand | callable $command * @param ICommand|callable $command
* @return string * @return class-string<IJob>
*/ */
private function getJobClass($command) { private function getJobClass($command): string {
if ($command instanceof \Closure) { if ($command instanceof \Closure) {
return ClosureJob::class; return ClosureJob::class;
} elseif (is_callable($command)) { } elseif (is_callable($command)) {
@ -63,10 +57,10 @@ class CronBus extends AsyncBus {
} }
/** /**
* @param \OCP\Command\ICommand | callable $command * @param ICommand|callable $command
* @return string * @return string
*/ */
private function serializeCommand($command) { private function serializeCommand($command): string {
if ($command instanceof \Closure) { if ($command instanceof \Closure) {
return serialize(new SerializableClosure($command)); return serialize(new SerializableClosure($command));
} elseif (is_callable($command) or $command instanceof ICommand) { } elseif (is_callable($command) or $command instanceof ICommand) {

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
* *
@ -22,9 +25,9 @@
*/ */
namespace OC\Repair\Owncloud; namespace OC\Repair\Owncloud;
use OC\BackgroundJob\QueuedJob;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\QueuedJob;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
@ -33,33 +36,14 @@ use OCP\IUserManager;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class CleanPreviewsBackgroundJob extends QueuedJob { class CleanPreviewsBackgroundJob extends QueuedJob {
/** @var IRootFolder */ public function __construct(
private $rootFolder; private IRootFolder $rootFolder,
private LoggerInterface $logger,
private LoggerInterface $logger; private IJobList $jobList,
/** @var IJobList */
private $jobList;
/** @var ITimeFactory */
private $timeFactory;
/** @var IUserManager */
private $userManager;
/**
* CleanPreviewsBackgroundJob constructor.
*/
public function __construct(IRootFolder $rootFolder,
LoggerInterface $logger,
IJobList $jobList,
ITimeFactory $timeFactory, ITimeFactory $timeFactory,
IUserManager $userManager) { private IUserManager $userManager,
$this->rootFolder = $rootFolder; ) {
$this->logger = $logger; parent::__construct($timeFactory);
$this->jobList = $jobList;
$this->timeFactory = $timeFactory;
$this->userManager = $userManager;
} }
public function run($arguments) { public function run($arguments) {
@ -80,10 +64,9 @@ class CleanPreviewsBackgroundJob extends QueuedJob {
} }
/** /**
* @param $uid * @param string $uid
* @return bool
*/ */
private function cleanupPreviews($uid) { private function cleanupPreviews($uid): bool {
try { try {
$userFolder = $this->rootFolder->getUserFolder($uid); $userFolder = $this->rootFolder->getUserFolder($uid);
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
@ -101,7 +84,7 @@ class CleanPreviewsBackgroundJob extends QueuedJob {
$thumbnails = $thumbnailFolder->getDirectoryListing(); $thumbnails = $thumbnailFolder->getDirectoryListing();
$start = $this->timeFactory->getTime(); $start = $this->time->getTime();
foreach ($thumbnails as $thumbnail) { foreach ($thumbnails as $thumbnail) {
try { try {
$thumbnail->delete(); $thumbnail->delete();
@ -109,7 +92,7 @@ class CleanPreviewsBackgroundJob extends QueuedJob {
// Ignore // Ignore
} }
if (($this->timeFactory->getTime() - $start) > 15) { if (($this->time->getTime() - $start) > 15) {
return false; return false;
} }
} }

@ -1,4 +1,7 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
* *
@ -23,10 +26,11 @@
*/ */
namespace OC\Repair\Owncloud; namespace OC\Repair\Owncloud;
use OC\BackgroundJob\QueuedJob; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\Storage; use OCP\Files\Storage\IStorage;
use OCP\IAvatarManager; use OCP\IAvatarManager;
use OCP\IUser; use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
@ -34,22 +38,16 @@ use Psr\Log\LoggerInterface;
use function is_resource; use function is_resource;
class MoveAvatarsBackgroundJob extends QueuedJob { class MoveAvatarsBackgroundJob extends QueuedJob {
/** @var IUserManager */ private ?IStorage $owncloudAvatarStorage = null;
private $userManager;
/** @var LoggerInterface */
private $logger;
/** @var IAvatarManager */
private $avatarManager;
/** @var Storage */
private $owncloudAvatarStorage;
public function __construct(IUserManager $userManager, LoggerInterface $logger, IAvatarManager $avatarManager, IRootFolder $rootFolder) { public function __construct(
$this->userManager = $userManager; private IUserManager $userManager,
$this->logger = $logger; private LoggerInterface $logger,
$this->avatarManager = $avatarManager; private IAvatarManager $avatarManager,
private IRootFolder $rootFolder,
ITimeFactory $time,
) {
parent::__construct($time);
try { try {
$this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage(); $this->owncloudAvatarStorage = $rootFolder->get('avatars')->getStorage();
} catch (\Exception $e) { } catch (\Exception $e) {
@ -69,7 +67,7 @@ class MoveAvatarsBackgroundJob extends QueuedJob {
} }
$counter = 0; $counter = 0;
$this->userManager->callForSeenUsers(function (IUser $user) use ($counter) { $this->userManager->callForSeenUsers(function (IUser $user) use (&$counter) {
$uid = $user->getUID(); $uid = $user->getUID();
$path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid); $path = 'avatars/' . $this->buildOwnCloudAvatarPath($uid);

@ -32,8 +32,8 @@ namespace OCP\BackgroundJob;
* This interface provides functions to register background jobs * This interface provides functions to register background jobs
* *
* To create a new background job create a new class that inherits from either * To create a new background job create a new class that inherits from either
* \OC\BackgroundJob\Job, \OC\BackgroundJob\QueuedJob or * \OCP\BackgroundJob\Job, \OCP\BackgroundJob\QueuedJob or
* \OC\BackgroundJob\TimedJob and register it using ->add($job, $argument), * \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 * $argument will be passed to the run() function of the job when the job is
* executed. * executed.
* *

@ -44,7 +44,6 @@ abstract class Job implements IJob, IParallelAwareJob {
protected $argument; protected $argument;
protected ITimeFactory $time; protected ITimeFactory $time;
protected bool $allowParallelRuns = true; protected bool $allowParallelRuns = true;
private ?ILogger $logger = null;
/** /**
* @since 15.0.0 * @since 15.0.0
@ -56,14 +55,13 @@ abstract class Job implements IJob, IParallelAwareJob {
/** /**
* The function to prepare the execution of the job. * The function to prepare the execution of the job.
* *
* * @return void
* @param IJobList $jobList
* @param ILogger|null $logger
* *
* @since 15.0.0 * @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) { public function execute(IJobList $jobList, ?ILogger $logger = null) {
$this->logger = $logger;
$this->start($jobList); $this->start($jobList);
} }
@ -73,7 +71,7 @@ abstract class Job implements IJob, IParallelAwareJob {
*/ */
public function start(IJobList $jobList): void { public function start(IJobList $jobList): void {
$jobList->setLastRun($this); $jobList->setLastRun($this);
$logger = $this->logger ?? \OCP\Server::get(LoggerInterface::class); $logger = \OCP\Server::get(LoggerInterface::class);
try { try {
$jobDetails = get_class($this) . ' (id: ' . $this->getId() . ', arguments: ' . json_encode($this->getArgument()) . ')'; $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 * The actual function that is called to run the job
* *
* @param $argument * @param $argument
* @return void
* *
* @since 15.0.0 * @since 15.0.0
*/ */

@ -24,7 +24,6 @@
*/ */
namespace OCP\Log; namespace OCP\Log;
use OCP\ILogger;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
@ -40,15 +39,6 @@ interface ILogFactory {
*/ */
public function get(string $type): IWriter; 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 $path
* @param string $type * @param string $type

@ -116,7 +116,7 @@ class DummyJobList extends \OC\BackgroundJob\JobList {
/** /**
* set the job that was last ran * set the job that was last ran
* *
* @param \OC\BackgroundJob\Job $job * @param \OCP\BackgroundJob\Job $job
*/ */
public function setLastJob(IJob $job): void { public function setLastJob(IJob $job): void {
$i = array_search($job, $this->jobs); $i = array_search($job, $this->jobs);

@ -9,16 +9,20 @@
namespace Test\BackgroundJob; namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\ILogger; use Psr\Log\LoggerInterface;
class JobTest extends \Test\TestCase { class JobTest extends \Test\TestCase {
private $run = false; private $run = false;
private ITimeFactory $timeFactory; private ITimeFactory $timeFactory;
private LoggerInterface $logger;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->run = false; $this->run = false;
$this->timeFactory = \OC::$server->get(ITimeFactory::class); $this->timeFactory = \OCP\Server::get(ITimeFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
\OC::$server->registerService(LoggerInterface::class, fn ($c) => $this->logger);
} }
public function testRemoveAfterException() { public function testRemoveAfterException() {
@ -29,14 +33,11 @@ class JobTest extends \Test\TestCase {
}); });
$jobList->add($job); $jobList->add($job);
$logger = $this->getMockBuilder(ILogger::class) $this->logger->expects($this->once())
->disableOriginalConstructor()
->getMock();
$logger->expects($this->once())
->method('error'); ->method('error');
$this->assertCount(1, $jobList->getAll()); $this->assertCount(1, $jobList->getAll());
$job->execute($jobList, $logger); $job->start($jobList);
$this->assertTrue($this->run); $this->assertTrue($this->run);
$this->assertCount(1, $jobList->getAll()); $this->assertCount(1, $jobList->getAll());
} }
@ -49,14 +50,11 @@ class JobTest extends \Test\TestCase {
}); });
$jobList->add($job); $jobList->add($job);
$logger = $this->getMockBuilder(ILogger::class) $this->logger->expects($this->once())
->disableOriginalConstructor()
->getMock();
$logger->expects($this->once())
->method('error'); ->method('error');
$this->assertCount(1, $jobList->getAll()); $this->assertCount(1, $jobList->getAll());
$job->execute($jobList, $logger); $job->start($jobList);
$this->assertTrue($this->run); $this->assertTrue($this->run);
$this->assertCount(1, $jobList->getAll()); $this->assertCount(1, $jobList->getAll());
} }

@ -9,20 +9,10 @@
namespace Test\BackgroundJob; namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\QueuedJob;
class TestQueuedJob extends \OC\BackgroundJob\QueuedJob { class TestQueuedJobNew extends QueuedJob {
public $ran = false; public bool $ran = false;
public function run($argument) {
$this->ran = true;
}
}
class TestQueuedJobNew extends \OCP\BackgroundJob\QueuedJob {
public $ran = false;
public function run($argument) { public function run($argument) {
$this->ran = true; $this->ran = true;
@ -30,10 +20,7 @@ class TestQueuedJobNew extends \OCP\BackgroundJob\QueuedJob {
} }
class QueuedJobTest extends \Test\TestCase { class QueuedJobTest extends \Test\TestCase {
/** private DummyJobList $jobList;
* @var DummyJobList $jobList
*/
private $jobList;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
@ -41,22 +28,13 @@ class QueuedJobTest extends \Test\TestCase {
$this->jobList = new DummyJobList(); $this->jobList = new DummyJobList();
} }
public function testJobShouldBeRemoved() {
$job = new TestQueuedJob();
$this->jobList->add($job);
$this->assertTrue($this->jobList->has($job, null));
$job->execute($this->jobList);
$this->assertTrue($job->ran);
}
public function testJobShouldBeRemovedNew() { public function testJobShouldBeRemovedNew() {
$job = new TestQueuedJobNew(\OC::$server->query(ITimeFactory::class)); $job = new TestQueuedJobNew(\OCP\Server::get(ITimeFactory::class));
$job->setId(42); $job->setId(42);
$this->jobList->add($job); $this->jobList->add($job);
$this->assertTrue($this->jobList->has($job, null)); $this->assertTrue($this->jobList->has($job, null));
$job->execute($this->jobList); $job->start($this->jobList);
$this->assertTrue($job->ran); $this->assertTrue($job->ran);
} }
} }

@ -23,7 +23,7 @@ class TestJob extends \OCP\BackgroundJob\Job {
* @param callable $callback * @param callable $callback
*/ */
public function __construct(ITimeFactory $time = null, $testCase = null, $callback = null) { public function __construct(ITimeFactory $time = null, $testCase = null, $callback = null) {
parent::__construct($time ?? \OC::$server->get(ITimeFactory::class)); parent::__construct($time ?? \OCP\Server::get(ITimeFactory::class));
$this->testCase = $testCase; $this->testCase = $testCase;
$this->callback = $callback; $this->callback = $callback;
} }

@ -9,23 +9,10 @@
namespace Test\BackgroundJob; namespace Test\BackgroundJob;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
class TestTimedJob extends \OC\BackgroundJob\TimedJob { class TestTimedJobNew extends TimedJob {
/** @var bool */ public bool $ran = false;
public $ran = false;
public function __construct() {
$this->setInterval(10);
}
public function run($argument) {
$this->ran = true;
}
}
class TestTimedJobNew extends \OCP\BackgroundJob\TimedJob {
/** @var bool */
public $ran = false;
public function __construct(ITimeFactory $timeFactory) { public function __construct(ITimeFactory $timeFactory) {
parent::__construct($timeFactory); parent::__construct($timeFactory);
@ -38,57 +25,23 @@ class TestTimedJobNew extends \OCP\BackgroundJob\TimedJob {
} }
class TimedJobTest extends \Test\TestCase { class TimedJobTest extends \Test\TestCase {
/** @var DummyJobList $jobList */ private DummyJobList $jobList;
private $jobList; private ITimeFactory $time;
/** @var ITimeFactory */
private $time;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->jobList = new DummyJobList(); $this->jobList = new DummyJobList();
$this->time = \OC::$server->query(ITimeFactory::class); $this->time = \OCP\Server::get(ITimeFactory::class);
}
public function testShouldRunAfterInterval() {
$job = new TestTimedJob();
$this->jobList->add($job);
$job->setLastRun(time() - 12);
$job->execute($this->jobList);
$this->assertTrue($job->ran);
} }
public function testShouldNotRunWithinInterval() {
$job = new TestTimedJob();
$this->jobList->add($job);
$job->setLastRun(time() - 5);
$job->execute($this->jobList);
$this->assertFalse($job->ran);
}
public function testShouldNotTwice() {
$job = new TestTimedJob();
$this->jobList->add($job);
$job->setLastRun(time() - 15);
$job->execute($this->jobList);
$this->assertTrue($job->ran);
$job->ran = false;
$job->execute($this->jobList);
$this->assertFalse($job->ran);
}
public function testShouldRunAfterIntervalNew() { public function testShouldRunAfterIntervalNew() {
$job = new TestTimedJobNew($this->time); $job = new TestTimedJobNew($this->time);
$job->setId(42); $job->setId(42);
$this->jobList->add($job); $this->jobList->add($job);
$job->setLastRun(time() - 12); $job->setLastRun(time() - 12);
$job->execute($this->jobList); $job->start($this->jobList);
$this->assertTrue($job->ran); $this->assertTrue($job->ran);
} }
@ -98,7 +51,7 @@ class TimedJobTest extends \Test\TestCase {
$this->jobList->add($job); $this->jobList->add($job);
$job->setLastRun(time() - 5); $job->setLastRun(time() - 5);
$job->execute($this->jobList); $job->start($this->jobList);
$this->assertFalse($job->ran); $this->assertFalse($job->ran);
} }
@ -108,10 +61,10 @@ class TimedJobTest extends \Test\TestCase {
$this->jobList->add($job); $this->jobList->add($job);
$job->setLastRun(time() - 15); $job->setLastRun(time() - 15);
$job->execute($this->jobList); $job->start($this->jobList);
$this->assertTrue($job->ran); $this->assertTrue($job->ran);
$job->ran = false; $job->ran = false;
$job->execute($this->jobList); $job->start($this->jobList);
$this->assertFalse($job->ran); $this->assertFalse($job->ran);
} }
} }

@ -44,7 +44,7 @@ class CronBusTest extends AsyncBusTest {
protected function runJobs() { protected function runJobs() {
$jobs = $this->jobList->getAll(); $jobs = $this->jobList->getAll();
foreach ($jobs as $job) { foreach ($jobs as $job) {
$job->execute($this->jobList); $job->start($this->jobList);
} }
} }
} }

Loading…
Cancel
Save