Search in sources :

Example 1 with JobAwareMonitor

use of org.datatransferproject.api.launcher.JobAwareMonitor in project data-transfer-project by google.

the class JobPollingService method tryToClaimJob.

/**
 * Claims {@link PortabilityJob} {@code jobId} and updates it with our public key in storage.
 * Returns true if the claim was successful; otherwise it returns false.
 */
private boolean tryToClaimJob(UUID jobId, WorkerKeyPair keyPair) {
    // Lookup the job so we can append to its existing properties.
    PortabilityJob existingJob = store.findJob(jobId);
    monitor.debug(() -> format("JobPollingService: tryToClaimJob: jobId: %s", existingJob));
    // Verify no transfer worker key
    if (existingJob.jobAuthorization().authPublicKey() != null) {
        monitor.debug(() -> "A public key cannot be persisted again");
        return false;
    }
    // TODO: Consider moving this check earlier in the flow
    String scheme = existingJob.jobAuthorization().encryptionScheme();
    if (publicKeySerializer == null) {
        monitor.severe(() -> format("Public key serializer not found for scheme %s processing job: %s", scheme, jobId));
        return false;
    }
    String serializedKey = publicKeySerializer.serialize(keyPair.getEncodedPublicKey());
    PortabilityJob updatedJob = existingJob.toBuilder().setAndValidateJobAuthorization(existingJob.jobAuthorization().toBuilder().setInstanceId(keyPair.getInstanceId()).setAuthPublicKey(serializedKey).setState(JobAuthorization.State.CREDS_ENCRYPTION_KEY_GENERATED).build()).build();
    // to CREDS_ENCRYPTION_KEY_GENERATED.
    try {
        store.claimJob(jobId, updatedJob);
        monitor.debug(() -> format("Stored updated job: tryToClaimJob: jobId: %s", existingJob));
    } catch (IllegalStateException | IOException e) {
        monitor.debug(() -> format("Could not claim job %s. It was probably already claimed by another transfer" + " worker. Error msg: %s", jobId, e.getMessage()), e);
        return false;
    }
    if (monitor instanceof JobAwareMonitor) {
        ((JobAwareMonitor) monitor).setJobId(jobId.toString());
    }
    JobMetadata.init(jobId, keyPair.getEncodedPrivateKey(), existingJob.transferDataType(), existingJob.exportService(), existingJob.importService(), Stopwatch.createUnstarted());
    monitor.debug(() -> format("Stored updated job: tryToClaimJob: JobMetadata initialized: %s", jobId));
    return true;
}
Also used : PortabilityJob(org.datatransferproject.spi.cloud.types.PortabilityJob) IOException(java.io.IOException) JobAwareMonitor(org.datatransferproject.api.launcher.JobAwareMonitor)

Aggregations

IOException (java.io.IOException)1 JobAwareMonitor (org.datatransferproject.api.launcher.JobAwareMonitor)1 PortabilityJob (org.datatransferproject.spi.cloud.types.PortabilityJob)1