diff --git a/Dockerfile b/Dockerfile index 40805ae..2c69380 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,5 +16,7 @@ COPY . . # Expose the port on which the Node.js application will run EXPOSE 3000 +HEALTHCHECK --start-period=5s --interval=1m --timeout=5s --retries=3 CMD node healthcheck.js || exit 1 + # Command to run the Node.js application CMD ["node", "index.js"] diff --git a/healthcheck.js b/healthcheck.js new file mode 100644 index 0000000..c04f74d --- /dev/null +++ b/healthcheck.js @@ -0,0 +1,5 @@ +require('dotenv').config(); + +fetch('http://localhost:3000/healthcheck'). + then(res => process.exit(res.ok ? 0:1)). + catch(() => process.exit(1)); \ No newline at end of file diff --git a/index.js b/index.js index a72a27d..e460d07 100644 --- a/index.js +++ b/index.js @@ -41,7 +41,7 @@ app.get('/', async (req, res) => { const result = await main(); // Return 204 on true, otherwise throw 500 error - result ? res.status(204).end() : (() => { + result ? res.sendStatus(200) : (() => { throw new Error('Main function did not return true'); })(); } catch (error) { @@ -50,6 +50,10 @@ app.get('/', async (req, res) => { } }) +app.get('/healthcheck', (req, res) => { + res.sendStatus(204); +}) + // Start the Express server const PORT = process.env.PORT || 3000; app.listen(PORT, () => {