Search in sources :

Example 16 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by WhisperSystems.

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 17 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by WhisperSystems.

the class FastJobStorageTest method getPendingJobsWithNoDependenciesInCreatedOrder_onlyMigrationJobWithAppropriateNextRunTime.

@Test
public void getPendingJobsWithNoDependenciesInCreatedOrder_onlyMigrationJobWithAppropriateNextRunTime() {
    FullSpec migrationSpec1 = new FullSpec(new JobSpec("1", "f1", Job.Parameters.MIGRATION_QUEUE_KEY, 0, 999, 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);
    assertTrue(jobs.isEmpty());
}
Also used : FullSpec(org.thoughtcrime.securesms.jobmanager.persistence.FullSpec) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) Test(org.junit.Test)

Example 18 with JobSpec

use of org.thoughtcrime.securesms.jobmanager.persistence.JobSpec in project Signal-Android by WhisperSystems.

the class JobMigrator method migrate.

/**
 * @return The version that has been migrated to.
 */
int migrate(@NonNull JobStorage jobStorage, @NonNull Data.Serializer dataSerializer) {
    List<JobSpec> jobSpecs = jobStorage.getAllJobSpecs();
    for (int i = lastSeenVersion; i < currentVersion; i++) {
        Log.i(TAG, "Migrating from " + i + " to " + (i + 1));
        ListIterator<JobSpec> iter = jobSpecs.listIterator();
        JobMigration migration = migrations.get(i + 1);
        assert migration != null;
        while (iter.hasNext()) {
            JobSpec jobSpec = iter.next();
            Data data = dataSerializer.deserialize(jobSpec.getSerializedData());
            JobData originalJobData = new JobData(jobSpec.getFactoryKey(), jobSpec.getQueueKey(), data);
            JobData updatedJobData = migration.migrate(originalJobData);
            JobSpec updatedJobSpec = new JobSpec(jobSpec.getId(), updatedJobData.getFactoryKey(), updatedJobData.getQueueKey(), jobSpec.getCreateTime(), jobSpec.getNextRunAttemptTime(), jobSpec.getRunAttempt(), jobSpec.getMaxAttempts(), jobSpec.getLifespan(), dataSerializer.serialize(updatedJobData.getData()), jobSpec.getSerializedInputData(), jobSpec.isRunning(), jobSpec.isMemoryOnly());
            iter.set(updatedJobSpec);
        }
    }
    jobStorage.updateJobs(jobSpecs);
    return currentVersion;
}
Also used : JobData(org.thoughtcrime.securesms.jobmanager.JobMigration.JobData) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) JobData(org.thoughtcrime.securesms.jobmanager.JobMigration.JobData) SuppressLint(android.annotation.SuppressLint)

Example 19 with JobSpec

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

the class FastJobStorage method deleteJobs.

@Override
public synchronized void deleteJobs(@NonNull List<String> jobIds) {
    List<String> durableIds = new ArrayList<>(jobIds.size());
    for (String id : jobIds) {
        JobSpec job = getJobById(id);
        if (job == null || !job.isMemoryOnly()) {
            durableIds.add(id);
        }
    }
    if (durableIds.size() > 0) {
        jobDatabase.deleteJobs(durableIds);
    }
    Set<String> deleteIds = new HashSet<>(jobIds);
    Iterator<JobSpec> jobIter = jobs.iterator();
    while (jobIter.hasNext()) {
        if (deleteIds.contains(jobIter.next().getId())) {
            jobIter.remove();
        }
    }
    for (String jobId : jobIds) {
        constraintsByJobId.remove(jobId);
        dependenciesByJobId.remove(jobId);
        for (Map.Entry<String, List<DependencySpec>> entry : dependenciesByJobId.entrySet()) {
            Iterator<DependencySpec> depedencyIter = entry.getValue().iterator();
            while (depedencyIter.hasNext()) {
                if (depedencyIter.next().getDependsOnJobId().equals(jobId)) {
                    depedencyIter.remove();
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) DependencySpec(org.thoughtcrime.securesms.jobmanager.persistence.DependencySpec) JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 20 with JobSpec

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

the class JobMigratorTest method simpleJobStorage.

private static JobStorage simpleJobStorage() {
    JobStorage jobStorage = mock(JobStorage.class);
    when(jobStorage.getAllJobSpecs()).thenReturn(new ArrayList<>(Collections.singletonList(new JobSpec("1", "f1", null, 1, 1, 1, 1, 1, "", null, false, false))));
    return jobStorage;
}
Also used : JobSpec(org.thoughtcrime.securesms.jobmanager.persistence.JobSpec) JobStorage(org.thoughtcrime.securesms.jobmanager.persistence.JobStorage)

Aggregations

JobSpec (org.thoughtcrime.securesms.jobmanager.persistence.JobSpec)66 FullSpec (org.thoughtcrime.securesms.jobmanager.persistence.FullSpec)38 Test (org.junit.Test)36 WorkerThread (androidx.annotation.WorkerThread)10 DependencySpec (org.thoughtcrime.securesms.jobmanager.persistence.DependencySpec)10 ArrayList (java.util.ArrayList)8 LinkedList (java.util.LinkedList)8 ConstraintSpec (org.thoughtcrime.securesms.jobmanager.persistence.ConstraintSpec)8 Nullable (androidx.annotation.Nullable)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Map (java.util.Map)6 JobStorage (org.thoughtcrime.securesms.jobmanager.persistence.JobStorage)6 Application (android.app.Application)4 NonNull (androidx.annotation.NonNull)4 Collectors (com.annimon.stream.Collectors)4 Stream (com.annimon.stream.Stream)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4