aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/JobMetrics.java
blob: 483057a828d83bfb551a73aa4ff54a39c54ac296 (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent.maintenance;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

/**
 * Tracks and forwards maintenance job metrics.
 *
 * @author mpolden
 */
public class JobMetrics {

    private final BiConsumer<String, Long> metricConsumer;

    private final Map<String, Long> incompleteRuns = new ConcurrentHashMap<>();

    public JobMetrics(BiConsumer<String, Long> metricConsumer) {
        this.metricConsumer = metricConsumer;
    }

    /** Record a run for given job */
    public void recordRunOf(String job) {
        incompleteRuns.merge(job, 1L, Long::sum);
    }

    /** Record successful run of given job */
    public void recordSuccessOf(String job) {
        incompleteRuns.put(job, 0L);
    }

    /** Forward metrics for given job to metric consumer */
    public void forward(String job) {
        Long incompleteRuns = this.incompleteRuns.get(job);
        if (incompleteRuns != null) {
            metricConsumer.accept(job, incompleteRuns);
        }
    }

}