Adapt maxInstancesForQueue to only consider instances of the same job.

Currently the maxInstancesForQueue limit checks the count of all jobs in a
given queue. If there are already too many jobs, the new job is discarded.

However this is not the expected behavior for the two jobs where it's used:
GroupCallPeekWorkerJob and AutomaticSessionResetJob
For both the expected behavior is that there aren't too many jobs of them
started, but that there will be at least one instance of them started.
Both of them use the same queue as the PushProcessMessageJob and the MarkerJob.
Those two jobs are often in the queue at the same time, effectively preventing
the GroupCallPeekWorkerJob and AutomaticSessionResetJob from being enqueued.
This commit is contained in:
AsamK
2021-02-06 16:54:52 +01:00
committed by Cody Henthorne
parent 53dc5bab43
commit 8f51bdcb78
5 changed files with 11 additions and 9 deletions

View File

@@ -407,8 +407,8 @@ public abstract class Job {
/**
* Specify the maximum number of instances you'd want of this job at any given time, as
* determined by the job's queue key. If enqueueing this job would put it over that limit,
* it will be ignored.
* determined by the job's factory key and queue key. If enqueueing this job would put it over
* that limit, it will be ignored.
*
* This property is ignored if the job is submitted as part of a {@link JobManager.Chain}, or
* if the job has no queue key.

View File

@@ -314,7 +314,7 @@ class JobController {
boolean exceedsQueue = solo.getParameters().getQueue() != null &&
solo.getParameters().getMaxInstancesForQueue() != Job.Parameters.UNLIMITED &&
jobStorage.getJobCountForQueue(solo.getParameters().getQueue()) >= solo.getParameters().getMaxInstancesForQueue();
jobStorage.getJobCountForFactoryAndQueue(solo.getFactoryKey(), solo.getParameters().getQueue()) >= solo.getParameters().getMaxInstancesForQueue();
if (exceedsQueue) {
return true;

View File

@@ -30,7 +30,7 @@ public interface JobStorage {
int getJobCountForFactory(@NonNull String factoryKey);
@WorkerThread
int getJobCountForQueue(@NonNull String queueKey);
int getJobCountForFactoryAndQueue(@NonNull String factoryKey, @NonNull String queueKey);
@WorkerThread
void updateJobRunningState(@NonNull String id, boolean isRunning);

View File

@@ -167,9 +167,10 @@ public class FastJobStorage implements JobStorage {
}
@Override
public synchronized int getJobCountForQueue(@NonNull String queueKey) {
public synchronized int getJobCountForFactoryAndQueue(@NonNull String factoryKey, @NonNull String queueKey) {
return (int) Stream.of(jobs)
.filter(j -> queueKey.equals(j.getQueueKey()))
.filter(j -> factoryKey.equals(j.getFactoryKey()) &&
queueKey.equals(j.getQueueKey()))
.count();
}