| Line 28... |
Line 28... |
| 28 |
private static final Logger log = LoggerFactory.getLogger(CronJobMonitorAspect.class);
|
28 |
private static final Logger log = LoggerFactory.getLogger(CronJobMonitorAspect.class);
|
| 29 |
|
29 |
|
| 30 |
@Autowired
|
30 |
@Autowired
|
| 31 |
private MeterRegistry meterRegistry;
|
31 |
private MeterRegistry meterRegistry;
|
| 32 |
|
32 |
|
| - |
|
33 |
private static final int NOT_RUN_YET = -1;
|
| - |
|
34 |
|
| 33 |
private final ConcurrentMap<String, AtomicInteger> lastStatus = new ConcurrentHashMap<>();
|
35 |
private final ConcurrentMap<String, AtomicInteger> lastStatus = new ConcurrentHashMap<>();
|
| 34 |
|
36 |
|
| 35 |
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
|
37 |
@Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
|
| 36 |
public Object monitorCronJob(ProceedingJoinPoint joinPoint) throws Throwable {
|
38 |
public Object monitorCronJob(ProceedingJoinPoint joinPoint) throws Throwable {
|
| 37 |
String methodName = joinPoint.getSignature().getName();
|
39 |
String methodName = joinPoint.getSignature().getName();
|
| - |
|
40 |
// -1, not 0. The gauge is registered when a job STARTS, and 0 means
|
| - |
|
41 |
// "failed" -- so a long-running job reported failure for the whole of its
|
| - |
|
42 |
// first execution after every restart, which is indistinguishable from a
|
| - |
|
43 |
// real failure. That is what put ten jobs on the board as FAILED after the
|
| - |
|
44 |
// 2026-08-31 restart. CronJobFailing matches == 0, so -1 is excluded until
|
| - |
|
45 |
// the job has actually finished once.
|
| 38 |
lastStatus.computeIfAbsent(methodName, m -> {
|
46 |
lastStatus.computeIfAbsent(methodName, m -> {
|
| 39 |
AtomicInteger gauge = new AtomicInteger(0);
|
47 |
AtomicInteger gauge = new AtomicInteger(NOT_RUN_YET);
|
| 40 |
Gauge.builder("cron_job_last_status", gauge, AtomicInteger::get)
|
48 |
Gauge.builder("cron_job_last_status", gauge, AtomicInteger::get)
|
| 41 |
.description("1=success, 0=failure")
|
49 |
.description("1=success, 0=failure, -1=not finished a run yet")
|
| 42 |
.tag("method", m)
|
50 |
.tag("method", m)
|
| 43 |
.register(meterRegistry);
|
51 |
.register(meterRegistry);
|
| 44 |
return gauge;
|
52 |
return gauge;
|
| 45 |
});
|
53 |
});
|
| 46 |
|
54 |
|