blob: d4d60723cbed5fd83ec04d5ab7a4ee204019056a (
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
|
// 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 class JobMetrics {
private final BiConsumer<String, Long> metricConsumer;
private final ConcurrentHashMap<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);
}
}
}
|