summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/JobMetrics.java
blob: bb1bd0085ef8a71b905e3ebacb2dc184af9be084 (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
// 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.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

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

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

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

    /** Record completion of given job */
    public void recordCompletionOf(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) {
            consume(job, incompleteRuns);
        }
    }

    protected abstract void consume(String job, Long incompleteRuns);

}