TinyHealth.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements. See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership. The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License. You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied. See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package net.morimekta.tiny.server.http;

  20. import com.fasterxml.jackson.annotation.JsonProperty;

  21. import java.util.Objects;

  22. /**
  23.  * Handle health and readiness for Tiny Server.
  24.  */
  25. public final class TinyHealth {
  26.     /**
  27.      * Interface for checking the healthiness of a service.
  28.      */
  29.     @FunctionalInterface
  30.     public interface HealthCheck {
  31.         /**
  32.          * @return The healthiness result of the service.
  33.          */
  34.         Result healthCheck();
  35.     }

  36.     /**
  37.      * Interface for checking the readiness of a service.
  38.      */
  39.     @FunctionalInterface
  40.     public interface ReadyCheck {
  41.         /**
  42.          * @return The readiness result of the service.
  43.          */
  44.         Result readyCheck();
  45.     }

  46.     /**
  47.      * The actual healthiness status.
  48.      */
  49.     public enum Status {
  50.         /** Service is OK. */
  51.         OK,
  52.         /** Service is unhealthy. */
  53.         UNHEALTHY,
  54.         /** Error getting status. */
  55.         ERROR
  56.     }

  57.     /**
  58.      * @param message Healthy service message. Optional.
  59.      * @return The OK result.
  60.      */
  61.     public static Result ok(String message) {
  62.         return new Result(Status.OK, String.valueOf(message));
  63.     }

  64.     /**
  65.      * @param message The unhealthy service message. Required.
  66.      * @return The UNHEALTHY service.
  67.      */
  68.     public static Result unhealthy(String message) {
  69.         return new Result(Status.UNHEALTHY, message);
  70.     }

  71.     /**
  72.      * The healthiness result.
  73.      */
  74.     public final static class Result {
  75.         private final Status status;
  76.         private final String message;

  77.         /**
  78.          * @param status  The result status.
  79.          * @param message The result status message.
  80.          */
  81.         public Result(Status status, String message) {
  82.             this.status = Objects.requireNonNull(status, "status == null");
  83.             this.message = Objects.requireNonNull(message, "message == null");
  84.         }

  85.         /**
  86.          * @return The healthiness status.
  87.          */
  88.         @JsonProperty("status")
  89.         public Status getStatus() {
  90.             return status;
  91.         }

  92.         /**
  93.          * @return The healthiness status message.
  94.          */
  95.         @JsonProperty("message")
  96.         public String getMessage() {
  97.             return message;
  98.         }

  99.         @Override
  100.         public String toString() {
  101.             return "Result{" +
  102.                    "status=" + status +
  103.                    ", message='" + message + '\'' +
  104.                    '}';
  105.         }

  106.         @Override
  107.         public boolean equals(Object o) {
  108.             if (this == o) {
  109.                 return true;
  110.             }
  111.             if (o == null || getClass() != o.getClass()) {
  112.                 return false;
  113.             }
  114.             Result result = (Result) o;
  115.             return status == result.status && message.equals(result.message);
  116.         }

  117.         @Override
  118.         public int hashCode() {
  119.             return Objects.hash(status, message);
  120.         }
  121.     }

  122.     // ----- PRIVATE -----
  123.     private TinyHealth() {}
  124. }