77 */
88namespace OC \BackgroundJob ;
99
10- use OCP \AppFramework \QueryException ;
1110use OCP \AppFramework \Utility \ITimeFactory ;
1211use OCP \AutoloadNotAllowedException ;
1312use OCP \BackgroundJob \IJob ;
1716use OCP \DB \QueryBuilder \IQueryBuilder ;
1817use OCP \IConfig ;
1918use OCP \IDBConnection ;
19+ use OCP \Snowflake \IGenerator ;
20+ use Override ;
21+ use Psr \Container \ContainerExceptionInterface ;
2022use Psr \Log \LoggerInterface ;
2123use function get_class ;
2224use function json_encode ;
2325use function min ;
2426use function strlen ;
2527
2628class JobList implements IJobList {
27- /** @var array<string, int > */
29+ /** @var array<string, string > */
2830 protected array $ alreadyVisitedParallelBlocked = [];
2931
3032 public function __construct (
31- protected IDBConnection $ connection ,
32- protected IConfig $ config ,
33- protected ITimeFactory $ timeFactory ,
34- protected LoggerInterface $ logger ,
33+ protected readonly IDBConnection $ connection ,
34+ protected readonly IConfig $ config ,
35+ protected readonly ITimeFactory $ timeFactory ,
36+ protected readonly LoggerInterface $ logger ,
37+ protected readonly IGenerator $ generator ,
3538 ) {
3639 }
3740
38- public function add ($ job , $ argument = null , ?int $ firstCheck = null ): void {
41+ #[Override]
42+ public function add (IJob |string $ job , mixed $ argument = null , ?int $ firstCheck = null ): void {
3943 if ($ firstCheck === null ) {
4044 $ firstCheck = $ this ->timeFactory ->getTime ();
4145 }
@@ -51,6 +55,7 @@ public function add($job, $argument = null, ?int $firstCheck = null): void {
5155 if (!$ this ->has ($ job , $ argument )) {
5256 $ query ->insert ('jobs ' )
5357 ->values ([
58+ 'id ' => $ query ->createNamedParameter ($ this ->generator ->nextId (), IQueryBuilder::PARAM_INT ),
5459 'class ' => $ query ->createNamedParameter ($ class ),
5560 'argument ' => $ query ->createNamedParameter ($ argumentJson ),
5661 'argument_hash ' => $ query ->createNamedParameter (hash ('sha256 ' , $ argumentJson )),
@@ -68,15 +73,12 @@ public function add($job, $argument = null, ?int $firstCheck = null): void {
6873 $ query ->executeStatement ();
6974 }
7075
71- public function scheduleAfter (string $ job , int $ runAfter , $ argument = null ): void {
76+ public function scheduleAfter (string $ job , int $ runAfter , mixed $ argument = null ): void {
7277 $ this ->add ($ job , $ argument , $ runAfter );
7378 }
7479
75- /**
76- * @param IJob|string $job
77- * @param mixed $argument
78- */
79- public function remove ($ job , $ argument = null ): void {
80+ #[Override]
81+ public function remove (IJob |string $ job , mixed $ argument = null ): void {
8082 $ class = ($ job instanceof IJob) ? get_class ($ job ) : $ job ;
8183
8284 $ query = $ this ->connection ->getQueryBuilder ();
@@ -104,20 +106,16 @@ public function remove($job, $argument = null): void {
104106 }
105107 }
106108
107- public function removeById (int $ id ): void {
109+ #[Override]
110+ public function removeById (string $ id ): void {
108111 $ query = $ this ->connection ->getQueryBuilder ();
109112 $ query ->delete ('jobs ' )
110113 ->where ($ query ->expr ()->eq ('id ' , $ query ->createNamedParameter ($ id , IQueryBuilder::PARAM_INT )));
111114 $ query ->executeStatement ();
112115 }
113116
114- /**
115- * check if a job is in the list
116- *
117- * @param IJob|class-string<IJob> $job
118- * @param mixed $argument
119- */
120- public function has ($ job , $ argument ): bool {
117+ #[Override]
118+ public function has (IJob |string $ job , mixed $ argument ): bool {
121119 $ class = ($ job instanceof IJob) ? get_class ($ job ) : $ job ;
122120 $ argument = json_encode ($ argument );
123121
@@ -135,18 +133,16 @@ public function has($job, $argument): bool {
135133 return (bool )$ row ;
136134 }
137135
138- public function getJobs ($ job , ?int $ limit , int $ offset ): array {
136+ #[Override]
137+ public function getJobs (IJob |string |null $ job , ?int $ limit , int $ offset ): array {
139138 $ iterable = $ this ->getJobsIterator ($ job , $ limit , $ offset );
140139 return (is_array ($ iterable ))
141140 ? $ iterable
142141 : iterator_to_array ($ iterable );
143142 }
144143
145- /**
146- * @param IJob|class-string<IJob>|null $job
147- * @return iterable<IJob> Avoid to store these objects as they may share a Singleton instance. You should instead use these IJobs instances while looping on the iterable.
148- */
149- public function getJobsIterator ($ job , ?int $ limit , int $ offset ): iterable {
144+ #[Override]
145+ public function getJobsIterator (IJob |string |null $ job , ?int $ limit , int $ offset ): iterable {
150146 $ query = $ this ->connection ->getQueryBuilder ();
151147 $ query ->select ('* ' )
152148 ->from ('jobs ' )
@@ -169,9 +165,7 @@ public function getJobsIterator($job, ?int $limit, int $offset): iterable {
169165 $ result ->closeCursor ();
170166 }
171167
172- /**
173- * @inheritDoc
174- */
168+ #[Override]
175169 public function getNext (bool $ onlyTimeSensitive = false , ?array $ jobClasses = null ): ?IJob {
176170 $ query = $ this ->connection ->getQueryBuilder ();
177171 $ query ->select ('* ' )
@@ -279,10 +273,8 @@ public function getNext(bool $onlyTimeSensitive = false, ?array $jobClasses = nu
279273 }
280274 }
281275
282- /**
283- * @return ?IJob The job matching the id. Beware that this object may be a singleton and may be modified by the next call to buildJob.
284- */
285- public function getById (int $ id ): ?IJob {
276+ #[Override]
277+ public function getById (string $ id ): ?IJob {
286278 $ row = $ this ->getDetailsById ($ id );
287279
288280 if ($ row ) {
@@ -292,7 +284,8 @@ public function getById(int $id): ?IJob {
292284 return null ;
293285 }
294286
295- public function getDetailsById (int $ id ): ?array {
287+ #[Override]
288+ public function getDetailsById (string $ id ): ?array {
296289 $ query = $ this ->connection ->getQueryBuilder ();
297290 $ query ->select ('* ' )
298291 ->from ('jobs ' )
@@ -320,7 +313,7 @@ private function buildJob(array $row): ?IJob {
320313 // Try to load the job as a service
321314 /** @var IJob $job */
322315 $ job = \OCP \Server::get ($ row ['class ' ]);
323- } catch (QueryException $ e ) {
316+ } catch (ContainerExceptionInterface $ e ) {
324317 if (class_exists ($ row ['class ' ])) {
325318 $ class = $ row ['class ' ];
326319 $ job = new $ class ();
@@ -336,7 +329,7 @@ private function buildJob(array $row): ?IJob {
336329 // This most likely means an invalid job was enqueued. We can ignore it.
337330 return null ;
338331 }
339- $ job ->setId (( int ) $ row ['id ' ]);
332+ $ job ->setId ($ row ['id ' ]);
340333 $ job ->setLastRun ((int )$ row ['last_run ' ]);
341334 $ job ->setArgument (json_decode ($ row ['argument ' ], true ));
342335 return $ job ;
@@ -351,12 +344,10 @@ private function buildJob(array $row): ?IJob {
351344 */
352345 public function setLastJob (IJob $ job ): void {
353346 $ this ->unlockJob ($ job );
354- $ this ->config ->setAppValue ('backgroundjob ' , 'lastjob ' , ( string ) $ job ->getId ());
347+ $ this ->config ->setAppValue ('backgroundjob ' , 'lastjob ' , $ job ->getId ());
355348 }
356349
357- /**
358- * Remove the reservation for a job
359- */
350+ #[Override]
360351 public function unlockJob (IJob $ job ): void {
361352 $ query = $ this ->connection ->getQueryBuilder ();
362353 $ query ->update ('jobs ' )
@@ -365,9 +356,7 @@ public function unlockJob(IJob $job): void {
365356 $ query ->executeStatement ();
366357 }
367358
368- /**
369- * set the lastRun of $job to now
370- */
359+ #[Override]
371360 public function setLastRun (IJob $ job ): void {
372361 $ query = $ this ->connection ->getQueryBuilder ();
373362 $ query ->update ('jobs ' )
@@ -382,9 +371,7 @@ public function setLastRun(IJob $job): void {
382371 $ query ->executeStatement ();
383372 }
384373
385- /**
386- * @param int $timeTaken
387- */
374+ #[Override]
388375 public function setExecutionTime (IJob $ job , $ timeTaken ): void {
389376 $ query = $ this ->connection ->getQueryBuilder ();
390377 $ query ->update ('jobs ' )
@@ -394,11 +381,7 @@ public function setExecutionTime(IJob $job, $timeTaken): void {
394381 $ query ->executeStatement ();
395382 }
396383
397- /**
398- * Reset the $job so it executes on the next trigger
399- *
400- * @since 23.0.0
401- */
384+ #[Override]
402385 public function resetBackgroundJob (IJob $ job ): void {
403386 $ query = $ this ->connection ->getQueryBuilder ();
404387 $ query ->update ('jobs ' )
@@ -408,6 +391,7 @@ public function resetBackgroundJob(IJob $job): void {
408391 $ query ->executeStatement ();
409392 }
410393
394+ #[Override]
411395 public function hasReservedJob (?string $ className = null ): bool {
412396 $ query = $ this ->connection ->getQueryBuilder ();
413397 $ query ->select ('* ' )
@@ -430,6 +414,7 @@ public function hasReservedJob(?string $className = null): bool {
430414 }
431415 }
432416
417+ #[Override]
433418 public function countByClass (): array {
434419 $ query = $ this ->connection ->getQueryBuilder ();
435420 $ query ->select ('class ' )
@@ -444,7 +429,7 @@ public function countByClass(): array {
444429
445430 while (($ row = $ result ->fetch ()) !== false ) {
446431 /**
447- * @var array{count:int, class:class-string} $row
432+ * @var array{count:int, class:class-string<IJob> } $row
448433 */
449434 $ jobs [] = $ row ;
450435 }
0 commit comments