Skip to content

Commit

Permalink
Merge pull request #1398 from lukeckk/development
Browse files Browse the repository at this point in the history
Script to check website status and send email to users if website is down
  • Loading branch information
huss authored Dec 10, 2024
2 parents d6be2d5 + 08c24c9 commit fa5e515
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"start": "node ./src/bin/www",
"start:dev": "nodemon --legacy-watch --inspect=0.0.0.0 ./src/bin/www",
"checkWebsiteStatus": "node src/server/services/checkWebsiteStatus.js",
"webpack:dev": "webpack watch --color --progress --mode development",
"webpack:build": "webpack build --node-env production",
"webpack": "webpack build --color --progress --mode development",
Expand Down
23 changes: 23 additions & 0 deletions src/scripts/checkWebsiteStatusCron.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
# *
# * This Source Code Form is subject to the terms of the Mozilla Public
# * License, v. 2.0. If a copy of the MPL was not distributed with this
# * file, You can obtain one at http://mozilla.org/MPL/2.0/.
# *

# This check the website status at hour level.
# This should be copied to /etc/ or /etc/cron.hourly/ or /etc/cron.daily and the copy renamed so that its function will be clear to admins.

# Check if a URL is provided as an argument
if [ -z "$1" ]; then
echo "Error: No URL provided. Usage: $0 URL-to-check"
exit 1
fi

URL=$1

# The absolute path the project root directory (OED)
cd '/example/path/to/project/OED'

# The following line should NOT need to be edited except by devs or if you have an old system with only docker-compose.
docker compose run --rm web npm run --silent checkWebsiteStatus -- $URL &>> /dev/null &
23 changes: 23 additions & 0 deletions src/server/services/checkWebsiteStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { log } = require('../log');
const WEBSITE_URL = process.argv[2];

async function checkWebsite() {
try {
const response = await fetch(WEBSITE_URL, { method: 'HEAD' });

if (!response.ok) {
const errorMessage = `The server at ${WEBSITE_URL} is down.`;
// Log the error using Logger class
log.error(errorMessage);
}
} catch (error) {
const errorMessage = `Failed to reach ${WEBSITE_URL}. Error: ${error.message}`;
log.error(errorMessage, error);
}
}

checkWebsite();

0 comments on commit fa5e515

Please sign in to comment.