@certik/skynet 0.8.15 → 0.8.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -1
- package/monitor.js +36 -60
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/monitor.js
CHANGED
|
@@ -30,69 +30,44 @@ function sortErrors(errors) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
async function getMostRecentJobLaunch(name) {
|
|
33
|
-
|
|
33
|
+
try {
|
|
34
|
+
const jobsRes = await fetch(`http://localhost:4646/v1/jobs?prefix=${name}`);
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
if (!jobsRes.ok) {
|
|
37
|
+
console.log(`[MONITOR] request local nomad API failed`);
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const jobs = await jobsRes.json();
|
|
42
|
-
|
|
43
|
-
if (jobs.length === 0) {
|
|
44
|
-
console.log(`[MONITOR] did not see any jobs prefixed with ${name}`);
|
|
45
|
-
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
48
41
|
|
|
49
|
-
|
|
50
|
-
// filter out monitor job
|
|
51
|
-
return job.ID.indexOf("-monitor") === -1 && job.Status === "dead";
|
|
52
|
-
});
|
|
42
|
+
const jobs = await jobsRes.json();
|
|
53
43
|
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
if (jobs.length === 0) {
|
|
45
|
+
console.log(`[MONITOR] did not see any jobs prefixed with ${name}`);
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
59
49
|
|
|
60
|
-
|
|
50
|
+
const recentFinishedJob = jobs.reverse().find((job) => {
|
|
51
|
+
// filter out monitor job
|
|
52
|
+
return job.ID.indexOf("-monitor") === -1 && job.Status === "dead";
|
|
53
|
+
});
|
|
61
54
|
|
|
62
|
-
|
|
63
|
-
|
|
55
|
+
if (!recentFinishedJob) {
|
|
56
|
+
console.log(`[MONITOR] did not see any dead jobs`);
|
|
64
57
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return [];
|
|
68
|
-
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
69
60
|
|
|
70
|
-
|
|
71
|
-
const groups = Object.keys(summary);
|
|
61
|
+
console.log("[MONITOR]", "most recent job info", recentFinishedJob.ID, recentFinishedJob.JobSummary.Summary);
|
|
72
62
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
type: ERROR_LEVEL.CRITICAL,
|
|
78
|
-
message: `The most recent launch returned non-zero exit value, please investigate`,
|
|
79
|
-
},
|
|
80
|
-
];
|
|
81
|
-
}
|
|
63
|
+
return recentFinishedJob;
|
|
64
|
+
} catch (getJobError) {
|
|
65
|
+
console.log("[MONITOR]", "cannot get most recent job", getJobError.message);
|
|
66
|
+
return null;
|
|
82
67
|
}
|
|
83
|
-
|
|
84
|
-
return [];
|
|
85
68
|
}
|
|
86
69
|
|
|
87
|
-
function createMonitor({
|
|
88
|
-
binaryName,
|
|
89
|
-
name,
|
|
90
|
-
type = "stateless",
|
|
91
|
-
mode = false,
|
|
92
|
-
selector = {},
|
|
93
|
-
check,
|
|
94
|
-
maxRetry = 2,
|
|
95
|
-
}) {
|
|
70
|
+
function createMonitor({ binaryName, name, type = "stateless", mode = false, selector = {}, check, maxRetry = 2 }) {
|
|
96
71
|
function monitor() {
|
|
97
72
|
if (!binaryName) {
|
|
98
73
|
binaryName = getBinaryName();
|
|
@@ -169,25 +144,26 @@ ${
|
|
|
169
144
|
|
|
170
145
|
const jobName = getJobName(name, selectorFlags, mode);
|
|
171
146
|
const mostRecentJob = await getMostRecentJobLaunch(jobName);
|
|
172
|
-
const recentJobFailures = checkMostRecentJobFailures(mostRecentJob);
|
|
173
|
-
|
|
174
|
-
result = result.concat(recentJobFailures);
|
|
175
147
|
|
|
176
148
|
if (result.length > 0) {
|
|
177
149
|
console.log("Found Errors", result);
|
|
178
150
|
|
|
179
151
|
if (production) {
|
|
180
|
-
|
|
152
|
+
const nomadAddr = process.env.SKYNET_NOMAD_PRODUCTION_ADDR;
|
|
153
|
+
|
|
154
|
+
// alert on opsgenie
|
|
181
155
|
await postGenieMessage(
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
156
|
+
`Failed Service Check: ${jobName}`,
|
|
157
|
+
`<p><b>Service:</b><a href="${nomadAddr}/ui/jobs/${jobName}" target="_blank">${jobName}</a></p><p><b>Issues</b></p><ul>${sortErrors(
|
|
158
|
+
result
|
|
159
|
+
)
|
|
160
|
+
.map((m) => `<li><b>${m.type}:</b> ${m.message}</li>`)
|
|
161
|
+
.join("")}</ul>`,
|
|
186
162
|
verbose
|
|
187
163
|
);
|
|
164
|
+
} else {
|
|
165
|
+
console.log("skip sending messages to opsgenie in dev env");
|
|
188
166
|
}
|
|
189
|
-
|
|
190
|
-
throw new Error(`failed due to critical errors`);
|
|
191
167
|
}
|
|
192
168
|
|
|
193
169
|
console.log(`[MONITOR] check successfully in ${Date.now() - startTime}ms`);
|