Skip to content

Commit

Permalink
add healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed May 8, 2024
1 parent 0c1f00c commit 0e5a44d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
79 changes: 79 additions & 0 deletions healthcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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) {

Check failure on line 38 in healthcheck.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Missing space before function parentheses
console.log(message)
if (shouldSendEmail) await sendMail(message)
if (shouldPostToWebHook) await postToWebHook(message)
}

async function postToWebHook(text) {

Check failure on line 44 in healthcheck.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Missing space before function parentheses
await axios
.post(process.env.HEALTH_CHECK_WEB_HOOK, { text })
.catch((error) => {
console.error(error)
})
}

async function sendMail(message) {

Check failure on line 52 in healthcheck.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Missing space before function parentheses
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)
}
}
21 changes: 20 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import errorHandler from './middleware/errorHandler.js'
import errorLogger from './middleware/errorLogger.js'
import invalidPathHandler from './middleware/invalidPathHandler.js'
import verifyAuthHeader from './verifyAuthHeader.js'
import { getConfig } from './config.js'
import { getConfig, defaultTenantName } from './config.js'
import { getUnsignedVC } from './test-fixtures/vc.js'

function IssuingException (code, message, error = null) {
this.code = code
Expand All @@ -27,6 +28,24 @@ export async function build (opts = {}) {
app.use(express.urlencoded({ extended: false }))
app.use(cors())

app.get('/healthz', async function (req, res) {
try {
const { data } = await axios.post(
`${req.protocol}://${req.headers.host}/instance/${defaultTenantName}/credentials/issue`,
getUnsignedVC()
)
if (!data.proof)
throw new SigningException(503, 'issuer-coordinator healthz failed')

Check failure on line 38 in src/app.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Expected { after 'if' condition

Check failure on line 38 in src/app.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

'SigningException' is not defined
} catch (e) {
console.log(`exception in healthz: ${JSON.stringify(e)}`)
return res.status(503).json({
error: `issuer-coordinator healthz check failed with error: ${e}`,
healthy: false
})
}
res.send({ message: 'issuer-coordinator server status: ok.', healthy: true })
})

app.get('/', async function (req, res, next) {
if (enableStatusService) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let CONFIG
const defaultPort = 4005
const defaultTenantName = 'test'
export const defaultTenantName = 'test'
const demoTenantName = 'testing'
const randomTenantName = 'random'
const randtomTenantToken = 'UNPROTECTED'
Expand Down

0 comments on commit 0e5a44d

Please sign in to comment.