Skip to content

Commit

Permalink
Add healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed May 22, 2024
1 parent 8669a30 commit 55dc648
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
5 changes: 5 additions & 0 deletions healthcheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('dotenv').config();

fetch('http://localhost:3000/healthcheck').
then(res => process.exit(res.ok ? 0:1)).
catch(() => process.exit(1));
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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, () => {
Expand Down

0 comments on commit 55dc648

Please sign in to comment.