• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-wings / #75

19 Jun 2024 12:29AM UTC coverage: 62.762% (+0.1%) from 62.666%
#75

push

trydofor
💥 tinytask schema refactor #256

30 of 31 new or added lines in 4 files covered. (96.77%)

172 existing lines in 6 files now uncovered.

11987 of 19099 relevant lines covered (62.76%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.54
/wings/slardar/src/main/java/pro/fessional/wings/slardar/monitor/MonitorTask.java
1
package pro.fessional.wings.slardar.monitor;
2

3
import lombok.Getter;
4
import lombok.Setter;
5
import lombok.extern.slf4j.Slf4j;
6
import org.springframework.beans.factory.InitializingBean;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.scheduling.annotation.Scheduled;
9
import pro.fessional.wings.silencer.spring.help.ApplicationContextHelper;
10
import pro.fessional.wings.slardar.context.Now;
11

12
import java.lang.management.ManagementFactory;
13
import java.util.Collections;
14
import java.util.LinkedHashMap;
15
import java.util.List;
16
import java.util.Map;
17

18
/**
19
 * @author trydofor
20
 * @since 2021-07-14
21
 */
22
@Slf4j
1✔
23
@Setter @Getter
24
public class MonitorTask implements InitializingBean {
1✔
25

26
    @Setter(onMethod_ = { @Autowired })
1✔
27
    private List<WarnMetric> warnMetrics = Collections.emptyList();
1✔
28

29
    @Setter(onMethod_ = { @Autowired })
1✔
30
    private List<WarnReport> warnReports = Collections.emptyList();
1✔
31

32
    @Setter(onMethod_ = { @Autowired(required = false) })
1✔
33
    private List<WarnFilter> warnFilters = Collections.emptyList();
1✔
34

35
    private String applicationName = null;
1✔
36
    private boolean hookSelf = true;
1✔
37

38
    @Scheduled(cron = "${wings.slardar.monitor.cron}")
39
    public void run() {
UNCOV
40
        log.info("MonitorTask started");
×
UNCOV
41
        Map<String, List<WarnMetric.Warn>> warns = new LinkedHashMap<>();
×
UNCOV
42
        metric(warns);
×
UNCOV
43
        filter(warns);
×
UNCOV
44
        report(warns);
×
UNCOV
45
    }
×
46

47
    public void metric(Map<String, List<WarnMetric.Warn>> warns) {
UNCOV
48
        for (WarnMetric metric : warnMetrics) {
×
UNCOV
49
            final String nm = metric.getKey();
×
50
            try {
UNCOV
51
                final List<WarnMetric.Warn> wn = metric.check();
×
UNCOV
52
                final int sz = wn.size();
×
UNCOV
53
                if (sz > 0) {
×
UNCOV
54
                    warns.put(nm, wn);
×
55
                }
UNCOV
56
                log.debug("check {} warns by {}", sz, nm);
×
57
            }
58
            catch (Exception e) {
×
59
                log.warn("failed to metric, name=" + nm, e);
×
UNCOV
60
            }
×
UNCOV
61
        }
×
UNCOV
62
    }
×
63

64
    private void filter(Map<String, List<WarnMetric.Warn>> warns) {
UNCOV
65
        for (WarnFilter filter : warnFilters) {
×
UNCOV
66
            filter.filter(warns);
×
UNCOV
67
        }
×
UNCOV
68
    }
×
69

70
    public void report(Map<String, List<WarnMetric.Warn>> warns) {
71
        if (warnReports.isEmpty()) return;
1✔
72

73
        final String app = applicationName != null ? applicationName : ApplicationContextHelper.getApplicationName();
1✔
74
        if (app.isBlank()) {
1✔
75
            log.warn("the app name of report should NOT blank");
1✔
76
        }
77

78
        String jvm = ManagementFactory.getRuntimeMXBean().getName();
1✔
79
        if (jvm != null) jvm = jvm.replace("@", "_");
1✔
80

81
        for (WarnReport report : warnReports) {
1✔
82
            final String rpt = report.getClass().getName();
1✔
83
            try {
84
                log.debug("check {} warns by {}", warns.size(), rpt);
1✔
85
                final WarnReport.Sts sts = report.report(app, jvm, warns);
1✔
86
                if (sts == WarnReport.Sts.Fail) {
1✔
87
                    log.warn("failed to report={}", rpt);
1✔
88
                }
89
                else {
90
                    log.info("report={}, status={}", rpt, sts);
1✔
91
                }
92
            }
93
            catch (Exception e) {
×
94
                log.warn("failed to report, name=" + rpt, e);
×
95
            }
1✔
96
        }
1✔
97
    }
1✔
98

99
    @Override
100
    public void afterPropertiesSet() {
101
        if (hookSelf) {
1✔
102
            reportHook("started");
1✔
103
            Runtime.getRuntime().addShutdownHook(new Thread(() -> reportHook("shutting")));
1✔
104
        }
105
    }
1✔
106

107
    private void reportHook(String key) {
108
        try {
109
            WarnMetric.Warn wn = new WarnMetric.Warn();
1✔
110
            wn.setType(WarnMetric.Type.Text);
1✔
111
            wn.setKey(key);
1✔
112
            wn.setRule("time");
1✔
113
            wn.setWarn(Now.zonedDateTime().toString());
1✔
114
            List<WarnMetric.Warn> ws = Collections.singletonList(wn);
1✔
115
            final Map<String, List<WarnMetric.Warn>> warns = Collections.singletonMap("wings.slardar.monitor.hook", ws);
1✔
116
            report(warns);
1✔
117
        }
118
        catch (Exception e) {
×
119
            // ignore
120
        }
1✔
121
    }
1✔
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc