Skip to content

Commit 4279aa8

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

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
@@ -33,13 +33,15 @@
3333
use OCP\BackgroundJob\TimedJob;
3434
use OCP\IAppConfig;
3535
use OCP\IUserManager;
36+
use Psr\Log\LoggerInterface;
3637

3738
class ExpireTrash extends TimedJob {
3839

3940
public function __construct(
4041
private IAppConfig $appConfig,
4142
private IUserManager $userManager,
4243
private Expiration $expiration,
44+
private LoggerInterface $logger,
4345
ITimeFactory $time
4446
) {
4547
parent::__construct($time);
@@ -63,12 +65,16 @@ protected function run($argument) {
6365
$users = $this->userManager->getSeenUsers($offset);
6466

6567
foreach ($users as $user) {
66-
$uid = $user->getUID();
67-
if (!$this->setupFS($uid)) {
68-
continue;
68+
try {
69+
$uid = $user->getUID();
70+
if (!$this->setupFS($uid)) {
71+
continue;
72+
}
73+
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
74+
Trashbin::deleteExpiredFiles($dirContent, $uid);
75+
} catch (\Throwable $e) {
76+
$this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]);
6977
}
70-
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
71-
Trashbin::deleteExpiredFiles($dirContent, $uid);
7278

7379
$offset++;
7480

apps/files_trashbin/lib/Command/ExpireTrash.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\Files_Trashbin\Trashbin;
3131
use OCP\IUser;
3232
use OCP\IUserManager;
33+
use Psr\Log\LoggerInterface;
3334
use Symfony\Component\Console\Command\Command;
3435
use Symfony\Component\Console\Helper\ProgressBar;
3536
use Symfony\Component\Console\Input\InputArgument;
@@ -107,12 +108,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107108
}
108109

109110
public function expireTrashForUser(IUser $user) {
110-
$uid = $user->getUID();
111-
if (!$this->setupFS($uid)) {
112-
return;
111+
try {
112+
$uid = $user->getUID();
113+
if (!$this->setupFS($uid)) {
114+
return;
115+
}
116+
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
117+
Trashbin::deleteExpiredFiles($dirContent, $uid);
118+
} catch (\Throwable $e) {
119+
$this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]);
113120
}
114-
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
115-
Trashbin::deleteExpiredFiles($dirContent, $uid);
116121
}
117122

118123
/**

apps/files_trashbin/tests/BackgroundJob/ExpireTrashTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\IAppConfig;
3333
use OCP\IUserManager;
3434
use PHPUnit\Framework\MockObject\MockObject;
35+
use Psr\Log\LoggerInterface;
3536
use Test\TestCase;
3637

3738
class ExpireTrashTest extends TestCase {
@@ -47,6 +48,9 @@ class ExpireTrashTest extends TestCase {
4748
/** @var IJobList&MockObject */
4849
private $jobList;
4950

51+
/** @var LoggerInterface&MockObject */
52+
private $logger;
53+
5054
/** @var ITimeFactory&MockObject */
5155
private $time;
5256

@@ -57,6 +61,7 @@ protected function setUp(): void {
5761
$this->userManager = $this->createMock(IUserManager::class);
5862
$this->expiration = $this->createMock(Expiration::class);
5963
$this->jobList = $this->createMock(IJobList::class);
64+
$this->logger = $this->createMock(LoggerInterface::class);
6065

6166
$this->time = $this->createMock(ITimeFactory::class);
6267
$this->time->method('getTime')
@@ -76,7 +81,7 @@ public function testConstructAndRun(): void {
7681
->with('files_trashbin', 'background_job_expire_trash_offset', 0)
7782
->willReturn(0);
7883

79-
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
84+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->time);
8085
$job->start($this->jobList);
8186
}
8287

@@ -87,7 +92,7 @@ public function testBackgroundJobDeactivated(): void {
8792
$this->expiration->expects($this->never())
8893
->method('getMaxAgeAsTimestamp');
8994

90-
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
95+
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->logger, $this->time);
9196
$job->start($this->jobList);
9297
}
9398
}

0 commit comments

Comments
 (0)