Skip to content

Commit dde95a7

Browse files
Merge pull request #51807 from nextcloud/backport/51600/stable30
[stable30] feat: Limit `ExpireTrash` job to 30 minutes
2 parents b98b888 + 7bdf8f0 commit dde95a7

File tree

4 files changed

+79
-54
lines changed

4 files changed

+79
-54
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,24 @@
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 {
19-
private IConfig $config;
20-
private Expiration $expiration;
21-
private IUserManager $userManager;
2218

2319
public function __construct(
24-
IConfig $config,
25-
IUserManager $userManager,
26-
Expiration $expiration,
20+
private IAppConfig $appConfig,
21+
private IUserManager $userManager,
22+
private Expiration $expiration,
2723
ITimeFactory $time
2824
) {
2925
parent::__construct($time);
3026
// Run once per 30 minutes
3127
$this->setInterval(60 * 30);
32-
33-
$this->config = $config;
34-
$this->userManager = $userManager;
35-
$this->expiration = $expiration;
3628
}
3729

38-
/**
39-
* @param $argument
40-
* @throws \Exception
41-
*/
4230
protected function run($argument) {
43-
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
31+
$backgroundJob = $this->appConfig->getValueString('files_trashbin', 'background_job_expire_trash', 'yes');
4432
if ($backgroundJob === 'no') {
4533
return;
4634
}
@@ -50,15 +38,28 @@ protected function run($argument) {
5038
return;
5139
}
5240

53-
$this->userManager->callForSeenUsers(function (IUser $user) {
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) {
5446
$uid = $user->getUID();
5547
if (!$this->setupFS($uid)) {
56-
return;
48+
continue;
5749
}
5850
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
5951
Trashbin::deleteExpiredFiles($dirContent, $uid);
60-
});
6152

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);
6263
\OC_Util::tearDownFS();
6364
}
6465

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
}

lib/private/User/Manager.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -620,30 +620,14 @@ public function countSeenUsers() {
620620
return $result;
621621
}
622622

623-
/**
624-
* @param \Closure $callback
625-
* @psalm-param \Closure(\OCP\IUser):?bool $callback
626-
* @since 11.0.0
627-
*/
628623
public function callForSeenUsers(\Closure $callback) {
629-
$limit = 1000;
630-
$offset = 0;
631-
do {
632-
$userIds = $this->getSeenUserIds($limit, $offset);
633-
$offset += $limit;
634-
foreach ($userIds as $userId) {
635-
foreach ($this->backends as $backend) {
636-
if ($backend->userExists($userId)) {
637-
$user = $this->getUserObject($userId, $backend, false);
638-
$return = $callback($user);
639-
if ($return === false) {
640-
return;
641-
}
642-
break;
643-
}
644-
}
624+
$users = $this->getSeenUsers();
625+
foreach ($users as $user) {
626+
$return = $callback($user);
627+
if ($return === false) {
628+
return;
645629
}
646-
} while (count($userIds) >= $limit);
630+
}
647631
}
648632

649633
/**
@@ -819,4 +803,30 @@ private function verifyUid(string $uid, bool $checkDataDirectory = false): bool
819803
public function getDisplayNameCache(): DisplayNameCache {
820804
return $this->displayNameCache;
821805
}
806+
807+
/**
808+
* Gets the list of users sorted by lastLogin, from most recent to least recent
809+
*
810+
* @param int $offset from which offset to fetch
811+
* @return \Iterator<IUser> list of user IDs
812+
* @since 30.0.0
813+
*/
814+
public function getSeenUsers(int $offset = 0): \Iterator {
815+
$limit = 1000;
816+
817+
do {
818+
$userIds = $this->getSeenUserIds($limit, $offset);
819+
$offset += $limit;
820+
821+
foreach ($userIds as $userId) {
822+
foreach ($this->backends as $backend) {
823+
if ($backend->userExists($userId)) {
824+
$user = $this->getUserObject($userId, $backend, false);
825+
yield $user;
826+
break;
827+
}
828+
}
829+
}
830+
} while (count($userIds) === $limit);
831+
}
822832
}

lib/public/IUserManager.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,15 @@ public function validateUserId(string $uid, bool $checkDataDirectory = false): v
221221
* @since 30.0.0
222222
*/
223223
public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array;
224+
225+
/**
226+
* Gets the list of users.
227+
* An iterator is returned allowing the caller to stop the iteration at any time.
228+
* The offset argument allows the caller to continue the iteration at a specific offset.
229+
*
230+
* @param int $offset from which offset to fetch
231+
* @return \Iterator<IUser> list of IUser object
232+
* @since 32.0.0
233+
*/
234+
public function getSeenUsers(int $offset = 0): \Iterator;
224235
}

0 commit comments

Comments
 (0)