Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to check website status and send email to users if website is down #1398

Merged
merged 6 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 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 Expand Up @@ -81,6 +82,7 @@
"multer": "~1.4.5-lts.1",
"ngraph.graph": "~20.0.0",
"ngraph.path": "~1.5.0",
"node-cron": "~3.0.3",
"nodemailer": "~6.9.13",
"pg": "~8.12.0",
"pg-promise": "~11.8.0",
Expand Down
16 changes: 16 additions & 0 deletions src/scripts/checkWebsiteStatusCron.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This aggregates the readings data 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"
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 &
28 changes: 28 additions & 0 deletions src/server/services/checkWebsiteStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* 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 cron = require('node-cron');
const { getConnection } = require('../db');
const { log } = require('../log');
const { logMailer } = require('../logMailer');
const LogEmail = require('../models/LogEmail');
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();