Skip to content

Commit b7cd6fb

Browse files
artongeskjnldsv
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>
1 parent 926af94 commit b7cd6fb

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
use OCA\Files_Trashbin\Trashbin;
1313
use OCP\AppFramework\Utility\ITimeFactory;
1414
use OCP\BackgroundJob\TimedJob;
15-
use OCP\IConfig;
16-
use OCP\IUser;
15+
use OCP\IAppConfig;
1716
use OCP\IUserManager;
1817

1918
class ExpireTrash extends TimedJob {
2019
public function __construct(
21-
private IConfig $config,
20+
private IAppConfig $appConfig,
2221
private IUserManager $userManager,
2322
private Expiration $expiration,
2423
ITimeFactory $time,
@@ -28,12 +27,8 @@ public function __construct(
2827
$this->setInterval(60 * 30);
2928
}
3029

31-
/**
32-
* @param $argument
33-
* @throws \Exception
34-
*/
3530
protected function run($argument) {
36-
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
31+
$backgroundJob = $this->appConfig->getValueString('files_trashbin', 'background_job_expire_trash', 'yes');
3732
if ($backgroundJob === 'no') {
3833
return;
3934
}
@@ -43,15 +38,28 @@ protected function run($argument) {
4338
return;
4439
}
4540

46-
$this->userManager->callForSeenUsers(function (IUser $user): void {
41+
$stopTime = time() + 60 * 30; // Stops after 30 minutes.
42+
$offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
43+
$users = $this->userManager->getSeenUsers($offset);
44+
45+
foreach ($users as $user) {
4746
$uid = $user->getUID();
4847
if (!$this->setupFS($uid)) {
49-
return;
48+
continue;
5049
}
5150
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
5251
Trashbin::deleteExpiredFiles($dirContent, $uid);
53-
});
5452

53+
$offset++;
54+
55+
if ($stopTime < time()) {
56+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset);
57+
\OC_Util::tearDownFS();
58+
return;
59+
}
60+
}
61+
62+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
5563
\OC_Util::tearDownFS();
5664
}
5765

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)