Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: Limit trash expire job to 30 minutes
And pick up where it left off, leveraging getSeenUsers.

Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge authored and AndyScherzinger committed Mar 30, 2025
commit bd9a2eba760af44c4308e5b907b55ed6bdf8e77a
30 changes: 19 additions & 11 deletions apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
use OCA\Files_Trashbin\Trashbin;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
use OCP\IUser;
use OCP\IAppConfig;
use OCP\IUserManager;

class ExpireTrash extends TimedJob {
public function __construct(
private IConfig $config,
private IAppConfig $appConfig,
private IUserManager $userManager,
private Expiration $expiration,
ITimeFactory $time,
Expand All @@ -28,12 +27,8 @@ public function __construct(
$this->setInterval(60 * 30);
}

/**
* @param $argument
* @throws \Exception
*/
protected function run($argument) {
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
$backgroundJob = $this->appConfig->getValueString('files_trashbin', 'background_job_expire_trash', 'yes');
if ($backgroundJob === 'no') {
return;
}
Expand All @@ -43,15 +38,28 @@ protected function run($argument) {
return;
}

$this->userManager->callForSeenUsers(function (IUser $user): void {
$stopTime = time() + 60 * 30; // Stops after 30 minutes.
$offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
$users = $this->userManager->getSeenUsers($offset);

foreach ($users as $user) {
$uid = $user->getUID();
if (!$this->setupFS($uid)) {
return;
continue;
}
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
Trashbin::deleteExpiredFiles($dirContent, $uid);
});

$offset++;

if ($stopTime < time()) {
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset);
\OC_Util::tearDownFS();
return;
}
}

$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
\OC_Util::tearDownFS();
}

Expand Down
27 changes: 15 additions & 12 deletions apps/files_trashbin/tests/BackgroundJob/ExpireTrashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@
use OCA\Files_Trashbin\Expiration;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IAppConfig;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class ExpireTrashTest extends TestCase {
/** @var IConfig|MockObject */
private $config;
/** @var IAppConfig&MockObject */
private $appConfig;

/** @var IUserManager|MockObject */
/** @var IUserManager&MockObject */
private $userManager;

/** @var Expiration|MockObject */
/** @var Expiration&MockObject */
private $expiration;

/** @var IJobList|MockObject */
/** @var IJobList&MockObject */
private $jobList;

/** @var ITimeFactory|MockObject */
/** @var ITimeFactory&MockObject */
private $time;

protected function setUp(): void {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->expiration = $this->createMock(Expiration::class);
$this->jobList = $this->createMock(IJobList::class);
Expand All @@ -51,22 +51,25 @@ protected function setUp(): void {
}

public function testConstructAndRun(): void {
$this->config->method('getAppValue')
$this->appConfig->method('getValueString')
->with('files_trashbin', 'background_job_expire_trash', 'yes')
->willReturn('yes');
$this->appConfig->method('getValueInt')
->with('files_trashbin', 'background_job_expire_trash_offset', 0)
->willReturn(0);

$job = new ExpireTrash($this->config, $this->userManager, $this->expiration, $this->time);
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
$job->start($this->jobList);
}

public function testBackgroundJobDeactivated(): void {
$this->config->method('getAppValue')
$this->appConfig->method('getValueString')
->with('files_trashbin', 'background_job_expire_trash', 'yes')
->willReturn('no');
$this->expiration->expects($this->never())
->method('getMaxAgeAsTimestamp');

$job = new ExpireTrash($this->config, $this->userManager, $this->expiration, $this->time);
$job = new ExpireTrash($this->appConfig, $this->userManager, $this->expiration, $this->time);
$job->start($this->jobList);
}
}
Loading