Search in sources :

Example 1 with JanitorException

use of bio.terra.cloudres.common.JanitorException in project terra-cloud-resource-lib by DataBiosphere.

the class CleanupRecorder method publish.

private static void publish(CloudResourceUid resource, @Nullable ResourceMetadata metadata, ClientConfig clientConfig) {
    CleanupConfig cleanupConfig = clientConfig.getCleanupConfig().get();
    if (publisher == null) {
        // Provide a new publisher if not present.
        TopicName topicName = TopicName.of(cleanupConfig.janitorProjectId(), cleanupConfig.janitorTopicName());
        try {
            providePublisher(Publisher.newBuilder(topicName).setCredentialsProvider(FixedCredentialsProvider.create(cleanupConfig.credentials())).build());
        } catch (IOException e) {
            throw new JanitorException("Failed to initialize Janitor pubsub publisher.", e);
        }
    }
    ObjectMapper objectMapper = new ObjectMapper().registerModule(new Jdk8Module()).registerModule(new JavaTimeModule()).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    OffsetDateTime now = OffsetDateTime.now(clock);
    CreateResourceRequestBody body = new CreateResourceRequestBody().resourceUid(resource).resourceMetadata(metadata).creation(now).expiration(now.plus(cleanupConfig.timeToLive())).putLabelsItem("client", clientConfig.getClientName()).putLabelsItem("cleanupId", cleanupConfig.cleanupId());
    ByteString data;
    try {
        data = ByteString.copyFromUtf8(objectMapper.writeValueAsString(body));
    } catch (IOException e) {
        throw new JanitorException(String.format("Failed to serialize CreateResourceRequestBody: [%s]", body), e);
    }
    ApiFuture<String> messageIdFuture = publisher.publish(PubsubMessage.newBuilder().setData(data).build());
    try {
        String messageId = messageIdFuture.get();
        logger.debug("Publish message to Janitor track resource " + messageId);
    } catch (InterruptedException | ExecutionException e) {
        throw new JanitorException(String.format("Failed to publish message: [%s] ", data.toString()), e);
    }
}
Also used : CreateResourceRequestBody(bio.terra.janitor.model.CreateResourceRequestBody) ByteString(com.google.protobuf.ByteString) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) IOException(java.io.IOException) ByteString(com.google.protobuf.ByteString) TopicName(com.google.pubsub.v1.TopicName) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) OffsetDateTime(java.time.OffsetDateTime) JanitorException(bio.terra.cloudres.common.JanitorException) ExecutionException(java.util.concurrent.ExecutionException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JanitorException (bio.terra.cloudres.common.JanitorException)1 CreateResourceRequestBody (bio.terra.janitor.model.CreateResourceRequestBody)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 ByteString (com.google.protobuf.ByteString)1 TopicName (com.google.pubsub.v1.TopicName)1 IOException (java.io.IOException)1 OffsetDateTime (java.time.OffsetDateTime)1 ExecutionException (java.util.concurrent.ExecutionException)1