Skip to content

Commit

Permalink
add healthcheck.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed Jul 15, 2024
1 parent 7a12caf commit d078106
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
**/*.env
.git
.github
.husky
coverage
logs
node_modules
.dockerignore
.editorconfig
.eslintrc.cjs
.gitignore
.lintstagedrc.json
.prettierignore
.prettierrc.js
compose-test.yaml
Dockerfile
README
.env.healthcheck.testing
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,6 @@ dist
.DS_Store

# the keyv data store
src/data
src/data

compose-test.yaml
114 changes: 114 additions & 0 deletions healthcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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

axios
.get(serviceURL)
.then(async function (response) {
try {
const body = response.data
if (body.healthy === true) {
process.exit(0)
}
await notify(`${serviceName} is unhealthy: ${body.error}`)
process.exit(1)
} catch (error) {
await notify(
`${serviceName} is potentially unhealthy - error: ${JSON.stringify(error)}`
)
process.exit(1)
}
})
.catch(async (error) => {
await notify(
`${serviceName} is unhealthy and will restart after 3 tries. Error: ${error.message}`
)
process.exit(1)
})

async function notify(message) {
console.log(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)

try {
await transporter.sendMail(messageParams)
} catch (error) {
console.log('the email send error: ')
console.log(error)
}
}

//import * as http from 'node:http';
/* const options = { hostname: 'SIGNER', port: (process.env.PORT || 4006), path: '/healthz', method: 'GET' };
http
.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', async () => {
try {
const response = JSON.parse(body);
if (response.healthy === true) {
console.log('healthy response received: ', body);
await sendMail("It worked!")
process.exit(0);
}
console.log('Unhealthy response received: ', body);
await sendMail(`It worked, but with error: ${JSON.stringify(body)}`)
process.exit(1);
} catch (err) {
console.log('Error parsing JSON response body: ', err);
process.exit(1);
}
});
})
.on('error', (err) => {
console.log('Error: ', err);
process.exit(1);
})
.end(); */
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"jsonld-document-loader": "^2.0.0",
"keyv": "^4.5.2",
"keyv-file": "^0.2.0",
"morgan": "~1.9.1"
"morgan": "~1.9.1",
"nodemailer": "^6.9.14"
},
"devDependencies": {
"@eslint/js": "^9.3.0",
Expand Down

0 comments on commit d078106

Please sign in to comment.