All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.dropwizard.util.health.ProcessGrepHealthCheck Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package org.kiwiproject.dropwizard.util.health;

import static org.kiwiproject.metrics.health.HealthCheckResults.newHealthyResult;
import static org.kiwiproject.metrics.health.HealthCheckResults.newUnhealthyResult;

import com.codahale.metrics.health.HealthCheck;
import org.kiwiproject.base.process.ProcessHelper;

import java.util.List;

/**
 * A health check that checks if we are able to find processes using pgrep, specifically via
 * {@link ProcessHelper#pgrep(String)}. This check tries to pgrep any java processes. Since we must be inside a java
 * process, we should always find at least one.
 */
public class ProcessGrepHealthCheck extends HealthCheck {

    /**
     * A default name that can be used when registering this health check.
     */
    @SuppressWarnings("unused")
    public static final String DEFAULT_NAME = "Process Grep";

    private final ProcessHelper processes;

    public ProcessGrepHealthCheck(ProcessHelper processes) {
        this.processes = processes;
    }

    @Override
    protected Result check() {
        try {
            var javaProcesses = processes.pgrep("java");
            return resultBasedOnReturnedProcesses(javaProcesses);
        } catch (Exception e) {
            return newUnhealthyResult("Failed to find java processes: " + e);
        }
    }

    private static Result resultBasedOnReturnedProcesses(List javaProcesses) {
        if (javaProcesses.isEmpty()) {
            return newUnhealthyResult("No java processes found (even though this app is a java processes)");
        }

        return newHealthyResult();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy