summaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/health/StateV1HealthUpdater.java
blob: 985685ebb8d331cef505f2776af4543f9575223e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.health;

import com.yahoo.vespa.applicationmodel.ServiceStatus;
import com.yahoo.vespa.applicationmodel.ServiceStatusInfo;

import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

/**
 * @author hakonhall
 */
class StateV1HealthUpdater implements HealthUpdater {

    private final String endpoint;
    private final StateV1HealthClient healthClient;

    private volatile ServiceStatusInfo serviceStatusInfo = new ServiceStatusInfo(ServiceStatus.NOT_CHECKED);

    StateV1HealthUpdater(URL url, Duration requestTimeout, Duration connectionKeepAlive) {
        this(url.toString(), new StateV1HealthClient(url, requestTimeout, connectionKeepAlive));
    }

    StateV1HealthUpdater(String endpoint, StateV1HealthClient healthClient) {
        this.endpoint = endpoint;
        this.healthClient = healthClient;
    }

    @Override
    public ServiceStatusInfo getServiceStatusInfo() {
        return serviceStatusInfo;
    }

    @Override
    public void run() {
        // Get time before fetching rather than after, to make the resulting age be an upper limit.
        Instant now = Instant.now();

        HealthInfo healthInfo;
        try {
            healthInfo = healthClient.get();
        } catch (Exception e) {
            healthInfo = HealthInfo.fromException(e);
        }

        ServiceStatus newServiceStatus = healthInfo.isHealthy() ? ServiceStatus.UP : ServiceStatus.DOWN;
        Optional<Instant> newSince = newServiceStatus == serviceStatusInfo.serviceStatus() ?
                serviceStatusInfo.since() : Optional.of(now);

        serviceStatusInfo = new ServiceStatusInfo(newServiceStatus, newSince, Optional.of(now),
                healthInfo.getErrorDescription(), Optional.of(endpoint));
    }

    @Override
    public void close() {
        healthClient.close();
    }

}