Skip to content

Commit c8bccde

Browse files
authored
Merge pull request #51911 from nextcloud/backport/51861/stable30
[stable30] fix: Catch exceptions when expiring trashbin
2 parents 4d1e0cd + b439b93 commit c8bccde

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
use OCP\BackgroundJob\TimedJob;
1414
use OCP\IAppConfig;
1515
use OCP\IUserManager;
16+
use Psr\Log\LoggerInterface;
1617

1718
class ExpireTrash extends TimedJob {
1819

1920
public function __construct(
2021
private IAppConfig $appConfig,
2122
private IUserManager $userManager,
2223
private Expiration $expiration,
24+
private LoggerInterface $logger,
2325
ITimeFactory $time
2426
) {
2527
parent::__construct($time);
@@ -43,12 +45,16 @@ protected function run($argument) {
4345
$users = $this->userManager->getSeenUsers($offset);
4446

4547
foreach ($users as $user) {
46-
$uid = $user->getUID();
47-
if (!$this->setupFS($uid)) {
48-
continue;
48+
try {
49+
$uid = $user->getUID();
50+
if (!$this->setupFS($uid)) {
51+
continue;
52+
}
53+
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
54+
Trashbin::deleteExpiredFiles($dirContent, $uid);
55+
} catch (\Throwable $e) {
56+
$this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]);
4957
}
50-
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
51-
Trashbin::deleteExpiredFiles($dirContent, $uid);
5258

5359
$offset++;
5460

apps/files_trashbin/lib/Command/ExpireTrash.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCA\Files_Trashbin\Trashbin;
1212
use OCP\IUser;
1313
use OCP\IUserManager;
14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Helper\ProgressBar;
1617
use Symfony\Component\Console\Input\InputArgument;
@@ -33,7 +34,8 @@ class ExpireTrash extends Command {
3334
* @param IUserManager|null $userManager
3435
* @param Expiration|null $expiration
3536
*/
36-
public function __construct(?IUserManager $userManager = null,
37+
public function __construct(private LoggerInterface $logger,
38+
?IUserManager $userManager = null,
3739
?Expiration $expiration = null) {
3840
parent::__construct();
3941

@@ -74,23 +76,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7476
} else {
7577
$p = new ProgressBar($output);
7678
$p->start();
77-
$this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
79+
80+
$users = $this->userManager->getSeenUsers();
81+
foreach ($users as $user) {
7882
$p->advance();
7983
$this->expireTrashForUser($user);
80-
});
84+
}
8185
$p->finish();
8286
$output->writeln('');
8387
}
8488
return 0;
8589
}
8690

8791
public function expireTrashForUser(IUser $user) {
88-
$uid = $user->getUID();
89-
if (!$this->setupFS($uid)) {
90-
return;
92+
try {
93+
$uid = $user->getUID();
94+
if (!$this->setupFS($uid)) {
95+
return;
96+
}
97+
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
98+
Trashbin::deleteExpiredFiles($dirContent, $uid);
99+
} catch (\Throwable $e) {
100+
$this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]);
91101
}
92-
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
93-
Trashbin::deleteExpiredFiles($dirContent, $uid);
94102
}
95103

96104
/**

apps/files_trashbin/tests/BackgroundJob/ExpireTrashTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\IAppConfig;
1515
use OCP\IUserManager;
1616
use PHPUnit\Framework\MockObject\MockObject;
17+
use Psr\Log\LoggerInterface;
1718
use Test\TestCase;
1819

1920
class ExpireTrashTest extends TestCase {
@@ -29,6 +30,9 @@ class ExpireTrashTest extends TestCase {
2930
/** @var IJobList&MockObject */
3031
private $jobList;
3132

33+
/** @var LoggerInterface&MockObject */
34+
private $logger;
35+
3236
/** @var ITimeFactory&MockObject */
3337
private $time;
3438

@@ -39,6 +43,7 @@ protected function setUp(): void {
3943
$this->userManager = $this->createMock(IUserManager::class);
4044
$this->expiration = $this->createMock(Expiration::class);
4145
$this->jobList = $this->createMock(IJobList::class);
46+
$this->logger = $this->createMock(LoggerInterface::class);
4247

4348
$this->time = $this->createMock(ITimeFactory::class);
4449
$this->time->method('getTime')
@@ -58,7 +63,7 @@ public function testConstructAndRun(): void {
5863
->with('files_trashbin', 'background_job_expire_trash_offset', 0)
5964
->willReturn(0);
6065

61-
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
66+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->time);
6267
$job->start($this->jobList);
6368
}
6469

@@ -69,7 +74,7 @@ public function testBackgroundJobDeactivated(): void {
6974
$this->expiration->expects($this->never())
7075
->method('getMaxAgeAsTimestamp');
7176

72-
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
77+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->time);
7378
$job->start($this->jobList);
7479
}
7580
}

0 commit comments

Comments
 (0)