blob: 4c05d46d782976d8bd1bdadcd7238cdf5df151f1 (
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.time.Clock;
import java.time.Instant;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
/**
* Tracks and forwards maintenance job metrics.
*
* @author mpolden
*/
public class JobMetrics {
private final Clock clock;
private final BiConsumer<String, Instant> metricConsumer;
private final Map<String, Instant> successfulRuns = new ConcurrentHashMap<>();
public JobMetrics(Clock clock, BiConsumer<String, Instant> metricConsumer) {
this.clock = Objects.requireNonNull(clock);
this.metricConsumer = metricConsumer;
}
/** Record successful run of given job */
public void recordSuccessOf(String job) {
successfulRuns.put(job, clock.instant());
}
/** Forward metrics for given job to metric consumer */
public void forward(String job) {
Instant lastSuccess = successfulRuns.get(job);
if (lastSuccess != null) {
metricConsumer.accept(job, lastSuccess);
}
}
}
|