Skip to content

Commit

Permalink
fix(server): healthcheck working
Browse files Browse the repository at this point in the history
  • Loading branch information
clabroche committed Feb 13, 2024
1 parent 1ec84c2 commit 13d2960
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
9 changes: 7 additions & 2 deletions common/express/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,16 @@ module.exports = {
path: pathfs.resolve(baseUrl, 'logs-express'),
});
let accessLogger = (req, res, next) => next();
if (process.env.NODE_ENV === 'DEV') {
if (process.env.NODE_ENV === 'DEV' && process.env.DEBUG === 'stack-monitor') {
console.log('Dev mode detected, enable access logger');
accessLogger = pinoHttp.default({
autoLogging: {
ignore: (req) => !!(req.url?.endsWith('/health')),
ignore: (req) => !!(
req.url?.endsWith('/health')
|| req.url?.endsWith('/infos')
|| req.url?.endsWith('/status')
|| req.url?.endsWith('/current-branch')
),
},
logger: logger.getConf().accessLogger,
serializers: {
Expand Down
6 changes: 5 additions & 1 deletion fronts/app/src/components/sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ export default {
onMounted(async () => {
await Stack.loadServices()
groups.value = []
Socket.socket.on('service:crash', async ({label, code, signal}) => {
Socket.socket.on('service:crash', async ({ label, code, signal }) => {
notification.next('error', `${label} has crashed with code ${code}`)
await Stack.loadServices()
});
Socket.socket.on('service:exit', async ({ label, code, signal }) => {
await Stack.loadServices()
});
Socket.socket.on('service:healthcheck:down', async ({label, code, signal}) => {
const service = await Stack.getService(label)
if(!service?.crashed) await Stack.loadServices()
Expand Down
7 changes: 6 additions & 1 deletion fronts/app/src/components/sidebarItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
:class="{active: isActive, disabled: !service.enabled, crashed: service.crashed}">
<span>
<template v-if="service.crashed"><i class="fas fa-exclamation"></i></template>
<template v-else-if="service.enabled"><spinner size="15"></spinner> </template>
<template v-else-if="service.enabled && !service.exited"><spinner size="15"></spinner> </template>
<template v-else-if="service.exited"><i class="fas fa-times warning"></i> </template>
<template v-else><i class="fas fa-square"></i></template>
{{service.label}}
</span>
Expand Down Expand Up @@ -70,5 +71,9 @@ const isActive = computed(() => {
&.crashed {
color: red;
}
.warning {
color: orange;
}
}
</style>
2 changes: 2 additions & 0 deletions fronts/app/src/models/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Service {
this.enabled = service.enabled || false;
/** @type {boolean} */
this.crashed = service.crashed || false;
/** @type {boolean} */
this.exited = service.exited || false;
/** @type {string} */
this.rootPath = service.rootPath || '';
}
Expand Down
17 changes: 14 additions & 3 deletions servers/server/models/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Service {
this.enabled = service.enabled || false;
/** @type {boolean} */
this.crashed = service.crashed || false;
/** @type {boolean} */
this.exited = service.exited || false;
/**
* @type {{
* spawnArgs?: string[],
Expand Down Expand Up @@ -333,7 +335,12 @@ class Service {
this.Stack.getStack()?.triggerOnServiceCrash(this, code);
this.crashed = true;
}
} else if (launchMessage.cmd) launchMessage.cmd.status = 'exited';
} else if (launchMessage.cmd) {
launchMessage.cmd.status = 'exited';
if (isMainProcess) {
this.exited = true;
}
}
sockets.io?.emit('service:exit', {
label: this.label, code, signal, pid: spawnProcess.pid,
});
Expand Down Expand Up @@ -364,7 +371,12 @@ class Service {
*/
async launchHealthChecker(spawnProcess) {
if (!spawnProcess.pid || !this.health?.check) return;
if (!(await psTreeAsync(spawnProcess.pid))?.length) return;
await new Promise((res) => { setTimeout(res, this.health.interval); });
if (!(await psTreeAsync(spawnProcess.pid))?.length) {
this.crashed = true;
sockets.io?.emit('service:healthcheck:down', { label: this.label, pid: spawnProcess.pid });
return;
}
let healthy;
try { healthy = await this.health.check(this); } catch (error) {
console.error(`Service health failed:${error}`);
Expand All @@ -376,7 +388,6 @@ class Service {
this.crashed = false;
sockets.io?.emit('service:healthcheck:up', { label: this.label, pid: spawnProcess.pid });
}
await new Promise((res) => { setTimeout(res, this.health.interval); });
this.launchHealthChecker(spawnProcess);
}

Expand Down

0 comments on commit 13d2960

Please sign in to comment.