Adds basic health checks

This commit is contained in:
Gabriel Matte 2024-12-06 21:01:23 -05:00
parent 8eab2d3a05
commit 5c75347887
7 changed files with 71 additions and 6 deletions

View file

@ -14,4 +14,7 @@ RUN npm run build
EXPOSE 5173
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD curl -f http://localhost:5173 || exit 1
CMD [ "npm", "run", "preview" ]

View file

@ -12,7 +12,7 @@ services:
networks:
- quiz_network
restart: always
backend:
build:
context: ./server
@ -33,7 +33,8 @@ services:
FRONTEND_URL: "http://localhost:5173"
#QUIZROOM_IMAGE: ghcr.io/ets-cfuhrman-pfe/evaluetonsavoir-quizroom:latest
depends_on:
- mongo
mongo:
condition: service_healthy
networks:
- quiz_network
restart: always
@ -45,8 +46,6 @@ services:
container_name: quizroom
ports:
- "4500:4500"
depends_on:
- backend
networks:
- quiz_network
restart: always
@ -59,8 +58,10 @@ services:
ports:
- "80:80"
depends_on:
- backend
- frontend
frontend:
condition: service_healthy
backend:
condition: service_healthy
networks:
- quiz_network
restart: always
@ -70,6 +71,12 @@ services:
# - FRONTEND_PORT=5173
# - BACKEND_HOST=backend
# - BACKEND_PORT=3000
healthcheck:
test: ["CMD-SHELL", "wget --spider http://0.0.0.0:${PORT}/health || exit 1"]
interval: 5s
timeout: 10s
start_period: 5s
retries: 6
mongo:
image: mongo
@ -82,6 +89,12 @@ services:
networks:
- quiz_network
restart: always
healthcheck:
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
interval: 10s
timeout: 5s
retries: 3
start_period: 20s
watchtower:
image: containrrr/watchtower

View file

@ -9,6 +9,7 @@ FROM alpine:3.19
# Install gettext for envsubst and other dependencies
RUN apk add --no-cache \
gettext \
curl \
nginx-mod-http-js \
nginx-mod-http-keyval \
pcre2 \
@ -80,5 +81,8 @@ RUN chmod +x /entrypoint.sh && \
# Switch to nginx user
USER nginx
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget -q --spider http://0.0.0.0:${PORT}/health || exit 1
# Start Nginx using entrypoint script
ENTRYPOINT ["/entrypoint.sh"]

View file

@ -19,6 +19,26 @@ server {
listen ${PORT};
set $proxy_target "";
location /health {
access_log off;
add_header Content-Type text/plain;
return 200 'healthy';
}
location /backend-health {
proxy_pass http://backend/health;
proxy_http_version 1.1;
proxy_set_header Host $host;
access_log off;
}
location /frontend-health {
proxy_pass http://frontend;
proxy_http_version 1.1;
proxy_set_header Host $host;
access_log off;
}
location /api {
proxy_pass http://backend;

View file

@ -10,4 +10,7 @@ COPY ./ .
EXPOSE 4400
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:${PORT}/health || exit 1
CMD ["npm", "run", "start"]

View file

@ -47,6 +47,7 @@ const folderRouter = require('./routers/folders.js');
const quizRouter = require('./routers/quiz.js');
const imagesRouter = require('./routers/images.js');
const roomRouter = require('./routers/rooms.js');
const healthRouter = require('./routers/health.js');
// Setup environment
dotenv.config();
@ -71,6 +72,7 @@ app.use('/api/folder', folderRouter);
app.use('/api/quiz', quizRouter);
app.use('/api/image', imagesRouter);
app.use('/api/room', roomRouter);
app.use('/health', healthRouter);
app.use(errorHandler);

20
server/routers/health.js Normal file
View file

@ -0,0 +1,20 @@
const express = require('express');
const router = express.Router();
router.get('/', async (req, res) => {
try {
const dbStatus = await require('../config/db.js').getConnection() ? 'connected' : 'disconnected';
res.json({
status: 'healthy',
timestamp: new Date(),
db: dbStatus
});
} catch (error) {
res.status(500).json({
status: 'unhealthy',
error: error.message
});
}
});
module.exports = router;