Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xsender.resources.health;

import io.github.project.openubl.xsender.resources.health.storage.StorageProvider;
import io.github.project.openubl.xsender.resources.health.storage.StorageProviderLiteral;
import io.github.project.openubl.xsender.resources.health.storage.StorageReadinessCheck;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
import org.eclipse.microprofile.health.Readiness;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import java.lang.annotation.Annotation;

@Readiness
@ApplicationScoped
public class StorageReadinessHealthCheck implements HealthCheck {

@ConfigProperty(name = "openubl.storage.type")
private String storageType;

@Inject
@Any
Instance<StorageReadinessCheck> storageReadinessChecks;

@Override
public HealthCheckResponse call() {
StorageProvider.Type providerType = StorageProvider.Type.valueOf(storageType.toUpperCase());
Annotation annotation = new StorageProviderLiteral(providerType);

StorageReadinessCheck readinessCheck = storageReadinessChecks.select(annotation).get();
boolean healthy = readinessCheck.isHealthy();

HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Storage connection health check");
if (healthy) {
responseBuilder.up();
} else {
responseBuilder.down();
}
return responseBuilder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xsender.resources.health.storage;

import javax.inject.Qualifier;
import java.lang.annotation.*;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@Documented
public @interface StorageProvider {
Type value();

enum Type {
S3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xsender.resources.health;
package io.github.project.openubl.xsender.resources.health.storage;

import io.github.project.openubl.xsender.models.jpa.CompanyRepository;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Readiness;
import javax.enterprise.util.AnnotationLiteral;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
public class StorageProviderLiteral extends AnnotationLiteral<StorageProvider> implements StorageProvider {

@Readiness
@ApplicationScoped
public class BasicReadinessHealthCheck implements HealthCheck {
private final StorageProvider.Type providerType;

@Inject
CompanyRepository companyRepository;
public StorageProviderLiteral(Type providerType) {
this.providerType = providerType;
}

@Override
public HealthCheckResponse call() {
// Not doing anything with entity since it just checks DB readiness
return HealthCheckResponse.up("Server readiness running");
public Type value() {
return providerType;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xsender.resources.health.storage;

public interface StorageReadinessCheck {

boolean isHealthy();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Eclipse Public License - v 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-2.0/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.xsender.resources.health.storage.impl;

import io.github.project.openubl.xsender.resources.health.storage.StorageProvider;
import io.github.project.openubl.xsender.resources.health.storage.StorageReadinessCheck;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

@ApplicationScoped
@StorageProvider(StorageProvider.Type.S3)
public class S3ReadinessCheck implements StorageReadinessCheck {

@ConfigProperty(name = "openubl.storage.s3.health.url")
private String s3HostHealthCheckUrl;

private final HttpClient client = HttpClient.newHttpClient();

@Override
public boolean isHealthy() {
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(s3HostHealthCheckUrl))
.GET()
.build();
HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
return response.statusCode() == 200;
} catch (URISyntaxException | IOException | InterruptedException e) {
return false;
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mp.messaging.incoming.verify-ticket-sunat-incoming.value.deserializer=org.apache
# Storage
openubl.storage.filesystem.folder=target/xsender-server-storage

openubl.storage.s3.health.url=http://localhost:9000/minio/health/live
openubl.storage.s3.host=http://localhost:9000
openubl.storage.s3.region=us-east-1
openubl.storage.s3.bucket=project-openubl
Expand Down