@@ -145,8 +145,6 @@ protected function getMaxIndexingTime(): int {
145145 }
146146
147147 protected function hasEnoughRunningJobs (): bool {
148- // Sleep a bit randomly to avoid a scenario where all jobs are started at the same time and kill themselves directly
149- sleep (rand (1 , 30 ));
150148 if (!$ this ->jobList ->hasReservedJob (static ::class)) {
151149 // short circuit to false if no jobs are running, yet
152150 return false ;
@@ -157,7 +155,8 @@ protected function hasEnoughRunningJobs(): bool {
157155 $ query = $ this ->db ->getQueryBuilder ();
158156 $ query ->select ('* ' )
159157 ->from ('jobs ' )
160- ->where ($ query ->expr ()->gt ('reserved_at ' , $ query ->createNamedParameter ($ this ->timeFactory ->getTime () - 6 * 3600 , IQueryBuilder::PARAM_INT )))
158+ ->where ($ query ->expr ()->gt ('reserved_at ' , $ query ->createNamedParameter ($ this ->timeFactory ->getTime () - $ this ->getMaxIndexingTime (), IQueryBuilder::PARAM_INT )))
159+ ->where ($ query ->expr ()->gte ('last_run ' , 'reserved_at ' )
161160 ->andWhere ($ query ->expr ()->eq ('id ' , $ query ->createNamedParameter ($ job ->getId (), IQueryBuilder::PARAM_INT )))
162161 ->setMaxResults (1 );
163162
@@ -172,7 +171,10 @@ protected function hasEnoughRunningJobs(): bool {
172171 $ this ->logger ->warning ('Querying reserved jobs failed ' , ['exception ' => $ e ]);
173172 }
174173 }
175- return $ count >= $ this ->appConfig ->getAppValueInt ('indexing_max_jobs_count ' , self ::DEFAULT_MAX_JOBS_COUNT );
174+ $ maxCount = $ this ->appConfig ->getAppValueInt ('indexing_max_jobs_count ' , self ::DEFAULT_MAX_JOBS_COUNT );
175+ // Either there are already less than the maximum, or we roll the dice according to the proportion of allowed jobs vs currently running ones
176+ // e.g. assume 8 jobs are allowed, currently there are 10 running, then we roll the dice and want to be lower than 0.8
177+ return $ count <= $ maxCount || $ maxCount / $ count > rand (0 , 10000 ) / 10000 );
176178 }
177179
178180 /**
0 commit comments