2626 */
2727namespace OCA \Files_Trashbin \BackgroundJob ;
2828
29+ use OC \Files \SetupManager ;
2930use OCA \Files_Trashbin \Expiration ;
3031use OCA \Files_Trashbin \Helper ;
3132use OCA \Files_Trashbin \Trashbin ;
3637use Psr \Log \LoggerInterface ;
3738
3839class ExpireTrash extends TimedJob {
40+ private const THIRTY_MINUTES = 30 * 60 ;
3941
4042 public function __construct (
4143 private IAppConfig $ appConfig ,
4244 private IUserManager $ userManager ,
4345 private Expiration $ expiration ,
4446 private LoggerInterface $ logger ,
47+ private SetupManager $ setupManager ,
4548 ITimeFactory $ time
4649 ) {
4750 parent ::__construct ($ time );
48- // Run once per 30 minutes
49- $ this ->setInterval (60 * 30 );
51+ $ this ->setInterval (self ::THIRTY_MINUTES );
5052 }
5153
5254 protected function run ($ argument ) {
@@ -60,44 +62,43 @@ protected function run($argument) {
6062 return ;
6163 }
6264
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 );
65+ $ stopTime = time () + self ::THIRTY_MINUTES ;
6666
67- foreach ($ users as $ user ) {
68- try {
69- $ uid = $ user ->getUID ();
70- if (!$ this ->setupFS ($ uid )) {
71- continue ;
67+ do {
68+ $ this ->appConfig ->clearCache ();
69+ $ offset = $ this ->appConfig ->getValueInt ('files_trashbin ' , 'background_job_expire_trash_offset ' , 0 );
70+ $ this ->appConfig ->setValueInt ('files_trashbin ' , 'background_job_expire_trash_offset ' , $ offset + 10 );
71+
72+ $ users = $ this ->userManager ->getLastLoggedInUsers (10 , $ offset );
73+
74+ foreach ($ users as $ uid ) {
75+ try {
76+ if ($ this ->setupFS ($ uid )) {
77+ $ dirContent = Helper::getTrashFiles ('/ ' , $ uid , 'mtime ' );
78+ Trashbin::deleteExpiredFiles ($ dirContent , $ uid );
79+ }
80+ } catch (\Throwable $ e ) {
81+ $ this ->logger ->error ('Error while expiring trashbin for user ' . $ uid , ['exception ' => $ e ]);
7282 }
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 ]);
83+
84+ $ this ->setupManager ->tearDown ();
7785 }
7886
79- $ offset ++ ;
87+ } while ( time () < $ stopTime && count ( $ users ) === 10 ) ;
8088
81- if ($ stopTime < time ()) {
82- $ this ->appConfig ->setValueInt ('files_trashbin ' , 'background_job_expire_trash_offset ' , $ offset );
83- \OC_Util::tearDownFS ();
84- return ;
85- }
89+ if (count ($ users ) < 10 ) {
90+ $ this ->appConfig ->setValueInt ('files_trashbin ' , 'background_job_expire_trash_offset ' , 0 );
8691 }
87-
88- $ this ->appConfig ->setValueInt ('files_trashbin ' , 'background_job_expire_trash_offset ' , 0 );
89- \OC_Util::tearDownFS ();
9092 }
9193
9294 /**
9395 * Act on behalf on trash item owner
9496 */
95- protected function setupFS (string $ user ): bool {
96- \OC_Util::tearDownFS ();
97- \OC_Util::setupFS ($ user );
97+ protected function setupFS (string $ uid ): bool {
98+ $ this ->setupManager ->setupForUser ($ this ->userManager ->get ($ uid ));
9899
99100 // Check if this user has a trashbin directory
100- $ view = new \ OC \ Files \ View ('/ ' . $ user );
101+ $ view = new View ('/ ' . $ uid );
101102 if (!$ view ->is_dir ('/files_trashbin/files ' )) {
102103 return false ;
103104 }
0 commit comments