diff --git a/package.json b/package.json index fa5f6d8de..031ad7646 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/scripts/checkWebsiteStatusCron.bash b/src/scripts/checkWebsiteStatusCron.bash new file mode 100644 index 000000000..b4e1cbb55 --- /dev/null +++ b/src/scripts/checkWebsiteStatusCron.bash @@ -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 & diff --git a/src/server/services/checkWebsiteStatus.js b/src/server/services/checkWebsiteStatus.js new file mode 100644 index 000000000..3c63ad65f --- /dev/null +++ b/src/server/services/checkWebsiteStatus.js @@ -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();