Search in sources :

Example 1 with JaxbServiceHealth

use of org.opencastproject.serviceregistry.api.JaxbServiceHealth in project opencast by opencast.

the class ServiceRegistryEndpoint method getHealthStatus.

@GET
@Path("health.xml")
@Produces(MediaType.APPLICATION_XML)
@RestQuery(name = "health", description = "Checks the status of the registered services", returnDescription = "Returns NO_CONTENT if services are in a proper state", restParameters = { @RestParameter(name = "serviceType", isRequired = false, type = Type.STRING, description = "The service type identifier"), @RestParameter(name = "host", isRequired = false, type = Type.STRING, description = "The host, including the http(s) protocol") }, reponses = { @RestResponse(responseCode = SC_OK, description = "Service states returned"), @RestResponse(responseCode = SC_NOT_FOUND, description = "No service of that type on that host is registered."), @RestResponse(responseCode = SC_SERVICE_UNAVAILABLE, description = "An error has occurred during stats processing") })
public Response getHealthStatus(@QueryParam("serviceType") String serviceType, @QueryParam("host") String host) throws NotFoundException {
    try {
        List<ServiceRegistration> services = null;
        if (isNotBlank(serviceType) && isNotBlank(host)) {
            // This is a request for one specific service. Return it, or SC_NOT_FOUND if not found
            ServiceRegistration reg = serviceRegistry.getServiceRegistration(serviceType, host);
            if (reg == null)
                throw new NotFoundException();
            services = new LinkedList<ServiceRegistration>();
            services.add(reg);
        } else if (isBlank(serviceType) && isBlank(host)) {
            // This is a request for all service registrations
            services = serviceRegistry.getServiceRegistrations();
        } else if (isNotBlank(serviceType)) {
            // This is a request for all service registrations of a particular type
            services = serviceRegistry.getServiceRegistrationsByType(serviceType);
        } else if (isNotBlank(host)) {
            // This is a request for all service registrations of a particular host
            services = serviceRegistry.getServiceRegistrationsByHost(host);
        }
        int healthy = 0;
        int warning = 0;
        int error = 0;
        for (ServiceRegistration reg : services) {
            if (ServiceState.NORMAL == reg.getServiceState()) {
                healthy++;
            } else if (ServiceState.WARNING == reg.getServiceState()) {
                warning++;
            } else if (ServiceState.ERROR == reg.getServiceState()) {
                error++;
            } else {
                error++;
            }
        }
        JaxbServiceHealth stats = new JaxbServiceHealth(healthy, warning, error);
        return Response.ok(stats).build();
    } catch (ServiceRegistryException e) {
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JaxbServiceHealth(org.opencastproject.serviceregistry.api.JaxbServiceHealth) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) JaxbServiceRegistration(org.opencastproject.serviceregistry.api.JaxbServiceRegistration) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Aggregations

GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JaxbServiceHealth (org.opencastproject.serviceregistry.api.JaxbServiceHealth)1 JaxbServiceRegistration (org.opencastproject.serviceregistry.api.JaxbServiceRegistration)1 ServiceRegistration (org.opencastproject.serviceregistry.api.ServiceRegistration)1 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)1 NotFoundException (org.opencastproject.util.NotFoundException)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1