-
Notifications
You must be signed in to change notification settings - Fork 10
/
healthcheck.mjs
71 lines (61 loc) · 1.95 KB
/
healthcheck.mjs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import nodemailer from 'nodemailer'
import axios from 'axios'
const serviceURL = process.env.HEALTH_CHECK_SERVICE_URL
const serviceName = process.env.HEALTH_CHECK_SERVICE_NAME
const shouldPostToWebHook = process.env.HEALTH_CHECK_WEB_HOOK
const shouldSendEmail =
process.env.HEALTH_CHECK_SMTP_HOST &&
process.env.HEALTH_CHECK_SMTP_USER &&
process.env.HEALTH_CHECK_SMTP_PASS &&
process.env.HEALTH_CHECK_EMAIL_FROM &&
process.env.HEALTH_CHECK_EMAIL_RECIPIENT
/*
This is intended to be called from the Docker HEALTHCHECK.
*/
async function callHealthz() {
try {
const response = await axios.get(serviceURL)
const body = response.data
//console.log(body)
if (body?.healthy === true) {
process.exit(0)
}
await notify(`${serviceName} is unhealthy and will restart after 3 tries. Returned messages: \n ${body.error}`)
process.exit(1)
} catch (e) {
await notify(`${serviceName} is unhealthy and will restart after 3 tries. Messages: \n ${e.response.data.error}`)
process.exit(1)
}
}
async function notify(message) {
if (shouldSendEmail) await sendMail(message)
if (shouldPostToWebHook) await postToWebHook(message)
}
async function postToWebHook(text) {
await axios
.post(process.env.HEALTH_CHECK_WEB_HOOK, { text })
.catch((error) => {
console.error(error)
})
}
async function sendMail(message) {
const messageParams = {
from: process.env.HEALTH_CHECK_EMAIL_FROM,
to: process.env.HEALTH_CHECK_EMAIL_RECIPIENT,
subject: process.env.HEALTH_CHECK_EMAIL_SUBJECT,
text: message
}
const mailTransport = {
host: process.env.HEALTH_CHECK_SMTP_HOST,
auth: {
user: process.env.HEALTH_CHECK_SMTP_USER,
pass: process.env.HEALTH_CHECK_SMTP_PASS
},
...(process.env.HEALTH_CHECK_SMTP_PORT && {
port: process.env.HEALTH_CHECK_SMTP_PORT
})
}
const transporter = nodemailer.createTransport(mailTransport)
await transporter.sendMail(messageParams)
}
callHealthz()