2626 */
2727namespace OCA \Files_Trashbin \BackgroundJob ;
2828
29+ use OC \Files \SetupManager ;
30+ use OC \Files \View ;
2931use OCA \Files_Trashbin \Expiration ;
3032use OCA \Files_Trashbin \Helper ;
3133use OCA \Files_Trashbin \Trashbin ;
3234use OCP \AppFramework \Utility \ITimeFactory ;
3335use OCP \BackgroundJob \TimedJob ;
3436use OCP \IAppConfig ;
37+ use OCP \IUser ;
3538use OCP \IUserManager ;
3639use Psr \Log \LoggerInterface ;
3740
3841class 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 }
0 commit comments