Skip to content

Commit af33a00

Browse files
committed
Port existing server code to new interface
Signed-off-by: Carl Schwan <[email protected]>
1 parent ad54c7f commit af33a00

File tree

29 files changed

+153
-223
lines changed

29 files changed

+153
-223
lines changed

apps/federatedfilesharing/lib/BackgroundJob/RetryJob.php

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,14 @@
4141
* @package OCA\FederatedFileSharing\BackgroundJob
4242
*/
4343
class RetryJob extends Job {
44-
45-
/** @var bool */
46-
private $retainJob = true;
47-
48-
/** @var Notifications */
49-
private $notifications;
44+
private bool $retainJob = true;
45+
private Notifications $notifications;
5046

5147
/** @var int max number of attempts to send the request */
52-
private $maxTry = 20;
48+
private int $maxTry = 20;
5349

5450
/** @var int how much time should be between two tries (10 minutes) */
55-
private $interval = 600;
56-
51+
private int $interval = 600;
5752

5853
public function __construct(Notifications $notifications,
5954
ITimeFactory $time) {
@@ -62,14 +57,11 @@ public function __construct(Notifications $notifications,
6257
}
6358

6459
/**
65-
* run the job, then remove it from the jobList
66-
*
67-
* @param IJobList $jobList
68-
* @param ILogger|null $logger
60+
* Run the job, then remove it from the jobList
6961
*/
70-
public function execute(IJobList $jobList, ILogger $logger = null) {
62+
public function start(IJobList $jobList): void {
7163
if ($this->shouldRun($this->argument)) {
72-
parent::execute($jobList, $logger);
64+
parent::start($jobList);
7365
$jobList->remove($this, $this->argument);
7466
if ($this->retainJob) {
7567
$this->reAddJob($jobList, $this->argument);
@@ -93,12 +85,9 @@ protected function run($argument) {
9385
}
9486

9587
/**
96-
* re-add background job with new arguments
97-
*
98-
* @param IJobList $jobList
99-
* @param array $argument
88+
* Re-add background job with new arguments
10089
*/
101-
protected function reAddJob(IJobList $jobList, array $argument) {
90+
protected function reAddJob(IJobList $jobList, array $argument): void {
10291
$jobList->add(RetryJob::class,
10392
[
10493
'remote' => $argument['remote'],
@@ -113,12 +102,9 @@ protected function reAddJob(IJobList $jobList, array $argument) {
113102
}
114103

115104
/**
116-
* test if it is time for the next run
117-
*
118-
* @param array $argument
119-
* @return bool
105+
* Test if it is time for the next run
120106
*/
121-
protected function shouldRun(array $argument) {
107+
protected function shouldRun(array $argument): bool {
122108
$lastRun = (int)$argument['lastRun'];
123109
return (($this->time->getTime() - $lastRun) > $this->interval);
124110
}

apps/federation/lib/BackgroundJob/GetSharedSecret.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use OCP\Http\Client\IClient;
4040
use OCP\Http\Client\IClientService;
4141
use OCP\Http\Client\IResponse;
42-
use OCP\ILogger;
4342
use OCP\IURLGenerator;
4443
use OCP\OCS\IDiscoveryService;
4544
use Psr\Log\LoggerInterface;
@@ -60,7 +59,6 @@ class GetSharedSecret extends Job {
6059
private LoggerInterface $logger;
6160
protected bool $retainJob = false;
6261
private string $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret';
63-
6462
/** 30 day = 2592000sec */
6563
private int $maxLifespan = 2592000;
6664

@@ -83,16 +81,13 @@ public function __construct(
8381
}
8482

8583
/**
86-
* run the job, then remove it from the joblist
87-
*
88-
* @param IJobList $jobList
89-
* @param ILogger|null $logger
84+
* Run the job, then remove it from the joblist
9085
*/
91-
public function execute(IJobList $jobList, ILogger $logger = null) {
86+
public function start(IJobList $jobList): void {
9287
$target = $this->argument['url'];
9388
// only execute if target is still in the list of trusted domains
9489
if ($this->trustedServers->isTrustedServer($target)) {
95-
$this->parentExecute($jobList, $logger);
90+
$this->parentStart($jobList);
9691
}
9792

9893
$jobList->remove($this, $this->argument);
@@ -102,14 +97,8 @@ public function execute(IJobList $jobList, ILogger $logger = null) {
10297
}
10398
}
10499

105-
/**
106-
* Call execute() method of parent
107-
*
108-
* @param IJobList $jobList
109-
* @param ILogger $logger
110-
*/
111-
protected function parentExecute($jobList, $logger = null) {
112-
parent::execute($jobList, $logger);
100+
protected function parentStart(IJobList $jobList): void {
101+
parent::start($jobList);
113102
}
114103

115104
protected function run($argument) {
@@ -162,12 +151,10 @@ protected function run($argument) {
162151
$status = -1; // There is no status code if we could not connect
163152
$this->logger->info('Could not connect to ' . $target, [
164153
'exception' => $e,
165-
'app' => 'federation',
166154
]);
167155
} catch (\Throwable $e) {
168156
$status = Http::STATUS_INTERNAL_SERVER_ERROR;
169157
$this->logger->error($e->getMessage(), [
170-
'app' => 'federation',
171158
'exception' => $e,
172159
]);
173160
}
@@ -190,22 +177,22 @@ protected function run($argument) {
190177
);
191178
} else {
192179
$this->logger->error(
193-
'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body,
194-
['app' => 'federation']
180+
'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body,
181+
['app' => 'federation']
195182
);
196183
$this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE);
197184
}
198185
}
199186
}
200187

201188
/**
202-
* re-add background job
189+
* Re-add background job
203190
*
204191
* @param array $argument
205192
*/
206193
protected function reAddJob(array $argument): void {
207194
$url = $argument['url'];
208-
$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
195+
$created = $argument['created'] ?? $this->time->getTime();
209196
$token = $argument['token'];
210197
$this->jobList->add(
211198
GetSharedSecret::class,

apps/federation/tests/BackgroundJob/GetSharedSecretTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ class GetSharedSecretTest extends TestCase {
7676
/** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
7777
private $timeFactory;
7878

79-
/** @var GetSharedSecret */
80-
private $getSharedSecret;
79+
private GetSharedSecret $getSharedSecret;
8180

8281
protected function setUp(): void {
8382
parent::setUp();
@@ -113,9 +112,9 @@ protected function setUp(): void {
113112
* @param bool $isTrustedServer
114113
* @param bool $retainBackgroundJob
115114
*/
116-
public function testExecute($isTrustedServer, $retainBackgroundJob) {
115+
public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): void {
117116
/** @var GetSharedSecret |\PHPUnit\Framework\MockObject\MockObject $getSharedSecret */
118-
$getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret')
117+
$getSharedSecret = $this->getMockBuilder(GetSharedSecret::class)
119118
->setConstructorArgs(
120119
[
121120
$this->httpClientService,
@@ -126,15 +125,15 @@ public function testExecute($isTrustedServer, $retainBackgroundJob) {
126125
$this->discoverService,
127126
$this->timeFactory
128127
]
129-
)->setMethods(['parentExecute'])->getMock();
128+
)->setMethods(['parentStart'])->getMock();
130129
$this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
131130

132131
$this->trustedServers->expects($this->once())->method('isTrustedServer')
133132
->with('url')->willReturn($isTrustedServer);
134133
if ($isTrustedServer) {
135-
$getSharedSecret->expects($this->once())->method('parentExecute');
134+
$getSharedSecret->expects($this->once())->method('parentStart');
136135
} else {
137-
$getSharedSecret->expects($this->never())->method('parentExecute');
136+
$getSharedSecret->expects($this->never())->method('parentStart');
138137
}
139138
$this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
140139
$this->jobList->expects($this->once())->method('remove');

apps/files/lib/BackgroundJob/ScanFiles.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
namespace OCA\Files\BackgroundJob;
2626

2727
use OC\Files\Utils\Scanner;
28+
use OCP\AppFramework\Utility\ITimeFactory;
29+
use OCP\BackgroundJob\TimedJob;
2830
use OCP\DB\QueryBuilder\IQueryBuilder;
2931
use OCP\EventDispatcher\IEventDispatcher;
3032
use OCP\IConfig;
@@ -37,13 +39,11 @@
3739
*
3840
* @package OCA\Files\BackgroundJob
3941
*/
40-
class ScanFiles extends \OC\BackgroundJob\TimedJob {
41-
/** @var IConfig */
42-
private $config;
43-
/** @var IEventDispatcher */
44-
private $dispatcher;
42+
class ScanFiles extends TimedJob {
43+
private IConfig $config;
44+
private IEventDispatcher $dispatcher;
4545
private LoggerInterface $logger;
46-
private $connection;
46+
private IDBConnection $connection;
4747

4848
/** Amount of users that should get scanned per execution */
4949
public const USERS_PER_SESSION = 500;
@@ -52,8 +52,10 @@ public function __construct(
5252
IConfig $config,
5353
IEventDispatcher $dispatcher,
5454
LoggerInterface $logger,
55-
IDBConnection $connection
55+
IDBConnection $connection,
56+
ITimeFactory $time
5657
) {
58+
parent::__construct($time);
5759
// Run once per 10 minutes
5860
$this->setInterval(60 * 10);
5961

@@ -63,10 +65,7 @@ public function __construct(
6365
$this->connection = $connection;
6466
}
6567

66-
/**
67-
* @param string $user
68-
*/
69-
protected function runScanner(string $user) {
68+
protected function runScanner(string $user): void {
7069
try {
7170
$scanner = new Scanner(
7271
$user,
@@ -95,7 +94,7 @@ private function getUserToScan() {
9594
->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
9695
->setMaxResults(1);
9796

98-
return $query->execute()->fetchOne();
97+
return $query->executeQuery()->fetchOne();
9998
}
10099

101100
/**

apps/files/tests/BackgroundJob/ScanFilesTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OC\Files\Mount\MountPoint;
2727
use OC\Files\Storage\Temporary;
2828
use OCA\Files\BackgroundJob\ScanFiles;
29+
use OCP\AppFramework\Utility\ITimeFactory;
2930
use OCP\EventDispatcher\IEventDispatcher;
3031
use OCP\IConfig;
3132
use OCP\IUser;
@@ -64,6 +65,7 @@ protected function setUp(): void {
6465
$dispatcher,
6566
$logger,
6667
$connection,
68+
$this->createMock(ITimeFactory::class)
6769
])
6870
->setMethods(['runScanner'])
6971
->getMock();

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,30 @@
3030
use OCA\Files_Trashbin\Expiration;
3131
use OCA\Files_Trashbin\Helper;
3232
use OCA\Files_Trashbin\Trashbin;
33+
use OCP\AppFramework\Utility\ITimeFactory;
34+
use OCP\BackgroundJob\TimedJob;
3335
use OCP\IConfig;
3436
use OCP\IUser;
3537
use OCP\IUserManager;
3638

37-
class ExpireTrash extends \OC\BackgroundJob\TimedJob {
39+
class ExpireTrash extends TimedJob {
40+
private IConfig $config;
41+
private Expiration $expiration;
42+
private IUserManager $userManager;
3843

39-
/** @var IConfig */
40-
private $config;
41-
42-
/**
43-
* @var Expiration
44-
*/
45-
private $expiration;
46-
47-
/**
48-
* @var IUserManager
49-
*/
50-
private $userManager;
51-
52-
public function __construct(IConfig $config = null,
53-
IUserManager $userManager = null,
54-
Expiration $expiration = null) {
44+
public function __construct(
45+
IConfig $config,
46+
IUserManager $userManager,
47+
Expiration $expiration,
48+
ITimeFactory $time
49+
) {
50+
parent::__construct($time);
5551
// Run once per 30 minutes
5652
$this->setInterval(60 * 30);
5753

58-
if ($config === null || $expiration === null || $userManager === null) {
59-
$this->fixDIForJobs();
60-
} else {
61-
$this->config = $config;
62-
$this->userManager = $userManager;
63-
$this->expiration = $expiration;
64-
}
65-
}
66-
67-
protected function fixDIForJobs() {
68-
/** @var Application $application */
69-
$application = \OC::$server->query(Application::class);
70-
$this->config = $application->getContainer()->get(IConfig::class);
71-
$this->userManager = \OC::$server->getUserManager();
72-
$this->expiration = $application->getContainer()->query('Expiration');
54+
$this->config = $config;
55+
$this->userManager = $userManager;
56+
$this->expiration = $expiration;
7357
}
7458

7559
/**
@@ -101,10 +85,8 @@ protected function run($argument) {
10185

10286
/**
10387
* Act on behalf on trash item owner
104-
* @param string $user
105-
* @return boolean
10688
*/
107-
protected function setupFS($user) {
89+
protected function setupFS(string $user): bool {
10890
\OC_Util::tearDownFS();
10991
\OC_Util::setupFS($user);
11092

0 commit comments

Comments
 (0)