Skip to content

Commit b439b93

Browse files
committed
fix: Catch exceptions when expiring trashbin
Signed-off-by: Louis Chemineau <louis@chmn.me>
1 parent d25a563 commit b439b93

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
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: 10 additions & 5 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;
@@ -88,12 +89,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8889
}
8990

9091
public function expireTrashForUser(IUser $user) {
91-
$uid = $user->getUID();
92-
if (!$this->setupFS($uid)) {
93-
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]);
94101
}
95-
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
96-
Trashbin::deleteExpiredFiles($dirContent, $uid);
97102
}
98103

99104
/**

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)