Skip to content

Commit 8253ed7

Browse files
committed
Merge #283 [FIX 29]After applying the patch, you should be able to execute multiple inst…
2 parents 3a7ece5 + b431711 commit 8253ed7

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,31 @@
2626
*/
2727
namespace OCA\Files_Trashbin\BackgroundJob;
2828

29+
use OC\Files\SetupManager;
30+
use OC\Files\View;
2931
use OCA\Files_Trashbin\Expiration;
3032
use OCA\Files_Trashbin\Helper;
3133
use OCA\Files_Trashbin\Trashbin;
3234
use OCP\AppFramework\Utility\ITimeFactory;
3335
use OCP\BackgroundJob\TimedJob;
3436
use OCP\IAppConfig;
37+
use OCP\IUser;
3538
use OCP\IUserManager;
3639
use Psr\Log\LoggerInterface;
3740

3841
class ExpireTrash extends TimedJob {
42+
private const THIRTY_MINUTES = 30 * 60;
3943

4044
public function __construct(
4145
private IAppConfig $appConfig,
4246
private IUserManager $userManager,
4347
private Expiration $expiration,
4448
private LoggerInterface $logger,
49+
private SetupManager $setupManager,
4550
ITimeFactory $time
4651
) {
4752
parent::__construct($time);
48-
// Run once per 30 minutes
49-
$this->setInterval(60 * 30);
53+
$this->setInterval(self::THIRTY_MINUTES);
5054
}
5155

5256
protected function run($argument) {
@@ -60,44 +64,47 @@ protected function run($argument) {
6064
return;
6165
}
6266

63-
$stopTime = time() + 60 * 30; // Stops after 30 minutes.
64-
$offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
65-
$users = $this->userManager->getSeenUsers($offset);
67+
$stopTime = time() + self::THIRTY_MINUTES;
6668

67-
foreach ($users as $user) {
68-
try {
69+
do {
70+
$this->appConfig->clearCache();
71+
$offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
72+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset + 10);
73+
74+
$users = $this->userManager->getSeenUsers($offset, 10);
75+
$count = 0;
76+
77+
foreach ($users as $user) {
6978
$uid = $user->getUID();
70-
if (!$this->setupFS($uid)) {
71-
continue;
79+
$count++;
80+
81+
try {
82+
if ($this->setupFS($user)) {
83+
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
84+
Trashbin::deleteExpiredFiles($dirContent, $uid);
85+
}
86+
} catch (\Throwable $e) {
87+
$this->logger->error('Error while expiring trashbin for user ' . $uid, ['exception' => $e]);
88+
} finally {
89+
$this->setupManager->tearDown();
7290
}
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]);
7791
}
7892

79-
$offset++;
93+
} while (time() < $stopTime && $count === 10);
8094

81-
if ($stopTime < time()) {
82-
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset);
83-
\OC_Util::tearDownFS();
84-
return;
85-
}
95+
if ($count < 10) {
96+
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
8697
}
87-
88-
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
89-
\OC_Util::tearDownFS();
9098
}
9199

92100
/**
93101
* Act on behalf on trash item owner
94102
*/
95-
protected function setupFS(string $user): bool {
96-
\OC_Util::tearDownFS();
97-
\OC_Util::setupFS($user);
103+
protected function setupFS(IUser $user): bool {
104+
$this->setupManager->setupForUser($user);
98105

99106
// Check if this user has a trashbin directory
100-
$view = new \OC\Files\View('/' . $user);
107+
$view = new View('/' . $user->getUID());
101108
if (!$view->is_dir('/files_trashbin/files')) {
102109
return false;
103110
}

lib/private/User/Manager.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ public function callForSeenUsers(\Closure $callback) {
633633
}
634634

635635
/**
636-
* Getting all userIds that have a listLogin value requires checking the
636+
* Getting all userIds that have a lastLogin value requires checking the
637637
* value in php because on oracle you cannot use a clob in a where clause,
638638
* preventing us from doing a not null or length(value) > 0 check.
639639
*
@@ -755,19 +755,19 @@ public function getDisplayNameCache(): DisplayNameCache {
755755
return $this->displayNameCache;
756756
}
757757

758-
/**
759-
* Gets the list of users sorted by lastLogin, from most recent to least recent
760-
*
761-
* @param int $offset from which offset to fetch
762-
* @return \Iterator<IUser> list of user IDs
763-
* @since 30.0.0
764-
*/
765-
public function getSeenUsers(int $offset = 0): \Iterator {
766-
$limit = 1000;
758+
public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator {
759+
$maxBatchSize = 1000;
767760

768761
do {
769-
$userIds = $this->getSeenUserIds($limit, $offset);
770-
$offset += $limit;
762+
if ($limit !== null) {
763+
$batchSize = min($limit, $maxBatchSize);
764+
$limit -= $batchSize;
765+
} else {
766+
$batchSize = $maxBatchSize;
767+
}
768+
769+
$userIds = $this->getSeenUserIds($batchSize, $offset);
770+
$offset += $batchSize;
771771

772772
foreach ($userIds as $userId) {
773773
foreach ($this->backends as $backend) {
@@ -778,6 +778,6 @@ public function getSeenUsers(int $offset = 0): \Iterator {
778778
}
779779
}
780780
}
781-
} while (count($userIds) === $limit);
781+
} while (count($userIds) === $batchSize && $limit !== 0);
782782
}
783783
}

lib/public/IUserManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ public function validateUserId(string $uid, bool $checkDataDirectory = false): v
239239
* The offset argument allows the caller to continue the iteration at a specific offset.
240240
*
241241
* @param int $offset from which offset to fetch
242+
* @param int|null $limit maximum number of records to fetch
242243
* @return \Iterator<IUser> list of IUser object
243244
* @since 29.0.15
245+
* @since 29.0.16 Added the $limit argument
244246
*/
245-
public function getSeenUsers(int $offset = 0): \Iterator;
247+
public function getSeenUsers(int $offset = 0, ?int $limit = null): \Iterator;
246248
}

0 commit comments

Comments
 (0)