Skip to content

Commit 3e45b3b

Browse files
artongebackportbot[bot]
authored andcommitted
feat: Limit trash expire job to 30 minutes
And pick up where it left off, leveraging getSeenUsers. Signed-off-by: Louis Chemineau <louis@chmn.me> [skip ci]
1 parent 822761a commit 3e45b3b

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
use OCA\Files_Trashbin\Trashbin;
1212
use OCP\AppFramework\Utility\ITimeFactory;
1313
use OCP\BackgroundJob\TimedJob;
14-
use OCP\IConfig;
15-
use OCP\IUser;
14+
use OCP\IAppConfig;
1615
use OCP\IUserManager;
1716

1817
class ExpireTrash extends TimedJob {
@@ -35,12 +34,8 @@ public function __construct(
3534
$this->expiration = $expiration;
3635
}
3736

38-
/**
39-
* @param $argument
40-
* @throws \Exception
41-
*/
4237
protected function run($argument) {
43-
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
38+
$backgroundJob = $this->appConfig->getValueString('files_trashbin', 'background_job_expire_trash', 'yes');
4439
if ($backgroundJob === 'no') {
4540
return;
4641
}
@@ -53,12 +48,21 @@ protected function run($argument) {
5348
$this->userManager->callForSeenUsers(function (IUser $user) {
5449
$uid = $user->getUID();
5550
if (!$this->setupFS($uid)) {
56-
return;
51+
continue;
5752
}
5853
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
5954
Trashbin::deleteExpiredFiles($dirContent, $uid);
60-
});
6155

56+
$offset++;
57+
58+
if ($stopTime < time()) {
59+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset);
60+
\OC_Util::tearDownFS();
61+
return;
62+
}
63+
}
64+
65+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
6266
\OC_Util::tearDownFS();
6367
}
6468

apps/files_trashbin/tests/BackgroundJob/ExpireTrashTest.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
use OCA\Files_Trashbin\Expiration;
1212
use OCP\AppFramework\Utility\ITimeFactory;
1313
use OCP\BackgroundJob\IJobList;
14-
use OCP\IConfig;
14+
use OCP\IAppConfig;
1515
use OCP\IUserManager;
1616
use PHPUnit\Framework\MockObject\MockObject;
1717
use Test\TestCase;
1818

1919
class ExpireTrashTest extends TestCase {
20-
/** @var IConfig|MockObject */
21-
private $config;
20+
/** @var IAppConfig&MockObject */
21+
private $appConfig;
2222

23-
/** @var IUserManager|MockObject */
23+
/** @var IUserManager&MockObject */
2424
private $userManager;
2525

26-
/** @var Expiration|MockObject */
26+
/** @var Expiration&MockObject */
2727
private $expiration;
2828

29-
/** @var IJobList|MockObject */
29+
/** @var IJobList&MockObject */
3030
private $jobList;
3131

32-
/** @var ITimeFactory|MockObject */
32+
/** @var ITimeFactory&MockObject */
3333
private $time;
3434

3535
protected function setUp(): void {
3636
parent::setUp();
3737

38-
$this->config = $this->createMock(IConfig::class);
38+
$this->appConfig = $this->createMock(IAppConfig::class);
3939
$this->userManager = $this->createMock(IUserManager::class);
4040
$this->expiration = $this->createMock(Expiration::class);
4141
$this->jobList = $this->createMock(IJobList::class);
@@ -51,22 +51,25 @@ protected function setUp(): void {
5151
}
5252

5353
public function testConstructAndRun(): void {
54-
$this->config->method('getAppValue')
54+
$this->appConfig->method('getValueString')
5555
->with('files_trashbin', 'background_job_expire_trash', 'yes')
5656
->willReturn('yes');
57+
$this->appConfig->method('getValueInt')
58+
->with('files_trashbin', 'background_job_expire_trash_offset', 0)
59+
->willReturn(0);
5760

58-
$job = new ExpireTrash($this->config, $this->userManager, $this->expiration, $this->time);
61+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
5962
$job->start($this->jobList);
6063
}
6164

6265
public function testBackgroundJobDeactivated(): void {
63-
$this->config->method('getAppValue')
66+
$this->appConfig->method('getValueString')
6467
->with('files_trashbin', 'background_job_expire_trash', 'yes')
6568
->willReturn('no');
6669
$this->expiration->expects($this->never())
6770
->method('getMaxAgeAsTimestamp');
6871

69-
$job = new ExpireTrash($this->config, $this->userManager, $this->expiration, $this->time);
72+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
7073
$job->start($this->jobList);
7174
}
7275
}

0 commit comments

Comments
 (0)