From cfd031546af4d643b04b13686ba11e2d2c8c792b Mon Sep 17 00:00:00 2001 From: Hugo Lecuyer Date: Wed, 28 Aug 2024 17:30:04 +0200 Subject: [PATCH] feat(alert): add wrapper on cron command to send alert on mattermost in case of failure --- api/Dockerfile | 4 ++++ api/cron.json | 6 +++--- api/run_management_command.sh | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 api/run_management_command.sh diff --git a/api/Dockerfile b/api/Dockerfile index 57ce329c4..124c09c99 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -65,6 +65,10 @@ COPY --chown=gunicorn:gunicorn --from=compile-image /srv/venv /srv/venv COPY --chown=gunicorn:gunicorn ["docker-entrypoint.sh", "pyproject.toml", "alembic.ini", "/srv/"] COPY --chown=gunicorn:gunicorn src /srv/src COPY --chown=gunicorn:gunicorn tests /srv/tests + +COPY test.py test.py +COPY run_management_command.sh run_management_command.sh + RUN chmod +x docker-entrypoint.sh USER gunicorn diff --git a/api/cron.json b/api/cron.json index b38e442cc..97dfa4dd4 100644 --- a/api/cron.json +++ b/api/cron.json @@ -1,15 +1,15 @@ { "jobs": [ { - "command": "30 * * * * TQDM_DISABLE=1 test $ENV = 'prod' && data-inclusion-api load_inclusion_data", + "command": "30 * * * * TQDM_DISABLE=1 test $ENV = 'prod' && sh run_management_command.sh data-inclusion-api load_inclusion_data", "size": "XL" }, { - "command": "30 * * * * TQDM_DISABLE=1 test $ENV != 'prod' && data-inclusion-api load_inclusion_data", + "command": "30 * * * * TQDM_DISABLE=1 test $ENV != 'prod' && sh run_management_command.sh data-inclusion-api load_inclusion_data", "size": "S" }, { - "command": "0 4 * * * vacuumdb --full --analyze --verbose --table api__structures --table api__services $DATABASE_URL", + "command": "0 4 * * * sh run_management_command.sh vacuumdb --full --analyze --verbose --table api__structures --table api__services $DATABASE_URL", "size": "S" } ] diff --git a/api/run_management_command.sh b/api/run_management_command.sh new file mode 100644 index 000000000..3d20f19ce --- /dev/null +++ b/api/run_management_command.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -u + +# Execute the command +"$@" & + +wait $command_pid + +exit_code=$? # Capture the exit code of the previous command + +# Check if the exit code indicates an error (not equal to 0) +if [ $exit_code -ne 0 ] && [ "$ENV" = "prod" ]; then + # Execute the curl command in case of an error + escaped_command="in command '$@'" + echo "Error with exit code $exit_code $escaped_command, sending message to Mattermost" + curl -i -X POST -H "Content-Type: application/json" -d "{\"text\": \"Error with exit code $exit_code $escaped_command: \nLogs: scalingo --region osc-secnum-fr1 --app data-inclusion-api-prod logs --lines 1000 -F $CONTAINER -f\"}" $MATTERMOST_HOOK +fi + +# Return the exit code of the initial command +exit $exit_code