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);
}
}
Aggregations