Search in sources :

Example 16 with FullSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.FullSpec in project Signal-Android by signalapp.

the class JobController method submitJobWithExistingDependencies.

@WorkerThread
synchronized void submitJobWithExistingDependencies(@NonNull Job job, @NonNull Collection<String> dependsOn, @Nullable String dependsOnQueue) {
    List<List<Job>> chain = Collections.singletonList(Collections.singletonList(job));
    if (chainExceedsMaximumInstances(chain)) {
        jobTracker.onStateChange(job, JobTracker.JobState.IGNORED);
        Log.w(TAG, JobLogger.format(job, "Already at the max instance count. Factory limit: " + job.getParameters().getMaxInstancesForFactory() + ", Queue limit: " + job.getParameters().getMaxInstancesForQueue() + ". Skipping."));
        return;
    }
    Set<String> allDependsOn = new HashSet<>(dependsOn);
    Set<String> aliveDependsOn = Stream.of(dependsOn).filter(id -> jobStorage.getJobSpec(id) != null).collect(Collectors.toSet());
    if (dependsOnQueue != null) {
        List<String> inQueue = Stream.of(jobStorage.getJobsInQueue(dependsOnQueue)).map(JobSpec::getId).toList();
        allDependsOn.addAll(inQueue);
        aliveDependsOn.addAll(inQueue);
    }
    if (jobTracker.haveAnyFailed(allDependsOn)) {
        Log.w(TAG, "This job depends on a job that failed! Failing this job immediately.");
        List<Job> dependents = onFailure(job);
        job.setContext(application);
        job.onFailure();
        Stream.of(dependents).forEach(Job::onFailure);
        return;
    }
    FullSpec fullSpec = buildFullSpec(job, aliveDependsOn);
    jobStorage.insertJobs(Collections.singletonList(fullSpec));
    scheduleJobs(Collections.singletonList(job));
    triggerOnSubmit(chain);
    notifyAll();
}
Also used : JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) Collectors(com.annimon.stream.Collectors) Stream(com.annimon.stream.Stream) NonNull(androidx.annotation.NonNull) Collection(java.util.Collection) WorkerThread(androidx.annotation.WorkerThread) Set(java.util.Set) Debouncer(org.thoughtcrime.securesms.util.Debouncer) HashMap(java.util.HashMap) ConstraintSpec(org.thoughtcrime.securesms.jobmanager.persistence.ConstraintSpec) DependencySpec(org.thoughtcrime.securesms.jobmanager.persistence.DependencySpec) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Log(org.signal.core.util.logging.Log) List(java.util.List) Nullable(androidx.annotation.Nullable) Application(android.app.Application) FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) Map(java.util.Map) JobStorage(org.thoughtcrime.securesms.jobmanager.persistence.JobStorage) LinkedList(java.util.LinkedList) Collections(java.util.Collections) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) HashSet(java.util.HashSet) WorkerThread(androidx.annotation.WorkerThread)

Example 17 with FullSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.FullSpec in project Signal-Android by signalapp.

the class FastJobStorageTest method fixedDataDatabase.

private JobDatabase fixedDataDatabase(List<FullSpec> fullSpecs) {
    JobDatabase database = mock(JobDatabase.class);
    when(database.getAllJobSpecs()).thenReturn(Stream.of(fullSpecs).map(FullSpec::getJobSpec).toList());
    when(database.getAllConstraintSpecs()).thenReturn(Stream.of(fullSpecs).map(FullSpec::getConstraintSpecs).flatMap(Stream::of).toList());
    when(database.getAllDependencySpecs()).thenReturn(Stream.of(fullSpecs).map(FullSpec::getDependencySpecs).flatMap(Stream::of).toList());
    return database;
}
Also used : JobDatabase(org.thoughtcrime.securesms.database.JobDatabase) FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) Stream(com.annimon.stream.Stream)

Example 18 with FullSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.FullSpec in project Signal-Android by signalapp.

the class FastJobStorageTest method getPendingJobsWithNoDependenciesInCreatedOrder_noneWhenEarlierItemInQueueInRunning.

@Test
public void getPendingJobsWithNoDependenciesInCreatedOrder_noneWhenEarlierItemInQueueInRunning() {
    FullSpec fullSpec1 = new FullSpec(new JobSpec("1", "f1", "q", 0, 0, 0, 0, -1, EMPTY_DATA, null, true, false), Collections.emptyList(), Collections.emptyList());
    FullSpec fullSpec2 = new FullSpec(new JobSpec("2", "f2", "q", 0, 0, 0, 0, -1, EMPTY_DATA, null, false, false), Collections.emptyList(), Collections.emptyList());
    FastJobStorage subject = new FastJobStorage(fixedDataDatabase(Arrays.asList(fullSpec1, fullSpec2)));
    subject.init();
    assertEquals(0, subject.getPendingJobsWithNoDependenciesInCreatedOrder(1).size());
}
Also used : FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) Test(org.junit.Test)

Example 19 with FullSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.FullSpec in project Signal-Android by signalapp.

the class FastJobStorageTest method getPendingJobsWithNoDependenciesInCreatedOrder_onlyReturnFirstEligibleMigrationJob.

@Test
public void getPendingJobsWithNoDependenciesInCreatedOrder_onlyReturnFirstEligibleMigrationJob() {
    FullSpec migrationSpec1 = new FullSpec(new JobSpec("1", "f1", Job.Parameters.MIGRATION_QUEUE_KEY, 0, 0, 0, 0, -1, EMPTY_DATA, null, false, false), Collections.emptyList(), Collections.emptyList());
    FullSpec migrationSpec2 = new FullSpec(new JobSpec("2", "f2", Job.Parameters.MIGRATION_QUEUE_KEY, 5, 0, 0, 0, -1, EMPTY_DATA, null, false, false), Collections.emptyList(), Collections.emptyList());
    FastJobStorage subject = new FastJobStorage(fixedDataDatabase(Arrays.asList(migrationSpec1, migrationSpec2)));
    subject.init();
    List<JobSpec> jobs = subject.getPendingJobsWithNoDependenciesInCreatedOrder(10);
    assertEquals(1, jobs.size());
    assertEquals("1", jobs.get(0).getId());
}
Also used : FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) Test(org.junit.Test)

Example 20 with FullSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.FullSpec in project Signal-Android by signalapp.

the class FastJobStorageTest method getPendingJobsWithNoDependenciesInCreatedOrder_runningMigrationBlocksLaterMigrationJobs.

@Test
public void getPendingJobsWithNoDependenciesInCreatedOrder_runningMigrationBlocksLaterMigrationJobs() {
    FullSpec migrationSpec1 = new FullSpec(new JobSpec("1", "f1", Job.Parameters.MIGRATION_QUEUE_KEY, 0, 0, 0, 0, -1, EMPTY_DATA, null, true, false), Collections.emptyList(), Collections.emptyList());
    FullSpec migrationSpec2 = new FullSpec(new JobSpec("2", "f2", Job.Parameters.MIGRATION_QUEUE_KEY, 5, 0, 0, 0, -1, EMPTY_DATA, null, false, false), Collections.emptyList(), Collections.emptyList());
    FastJobStorage subject = new FastJobStorage(fixedDataDatabase(Arrays.asList(migrationSpec1, migrationSpec2)));
    subject.init();
    List<JobSpec> jobs = subject.getPendingJobsWithNoDependenciesInCreatedOrder(10);
    assertEquals(0, jobs.size());
}
Also used : FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) Test(org.junit.Test)

Aggregations

FullSpec (org.thoughtcrime.securesms.jobmanager.persistence.FullSpec)42 JobSpec (org.thoughtcrime.securesms.jobmanager.persistence.JobSpec)38 Test (org.junit.Test)32 Stream (com.annimon.stream.Stream)6 DependencySpec (org.thoughtcrime.securesms.jobmanager.persistence.DependencySpec)6 Application (android.app.Application)4 NonNull (androidx.annotation.NonNull)4 Nullable (androidx.annotation.Nullable)4 WorkerThread (androidx.annotation.WorkerThread)4 Collectors (com.annimon.stream.Collectors)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4 Log (org.signal.core.util.logging.Log)4