Skip to content

Commit

Permalink
feat(alert): add wrapper on cron command to send alert on mattermost …
Browse files Browse the repository at this point in the history
…in case of failure
  • Loading branch information
hlecuyer committed Aug 29, 2024
1 parent 78a7747 commit f7c2c2d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ RUN useradd --no-log-init -g gunicorn gunicorn
# Copy venv with compiled dependencies
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 ["execute_and_notify.sh", "docker-entrypoint.sh", "pyproject.toml", "alembic.ini", "/srv/"]
COPY --chown=gunicorn:gunicorn src /srv/src
COPY --chown=gunicorn:gunicorn tests /srv/tests


RUN chmod +x docker-entrypoint.sh
RUN chmod +x execute_and_notify.sh

USER gunicorn
EXPOSE 8000
Expand Down
4 changes: 2 additions & 2 deletions api/cron.json
Original file line number Diff line number Diff line change
@@ -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' && ./execute_and_notify.sh data-inclusion-api load_inclusion_data",
"size": "XL"
},
{
"command": "30 * * * * TQDM_DISABLE=1 test $ENV != 'prod' && 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 * * * ./execute_and_notify.sh vacuumdb --full --analyze --verbose --table api__structures --table api__services $DATABASE_URL",
"size": "S"
}
]
Expand Down
14 changes: 14 additions & 0 deletions api/execute_and_notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
set -u

"$@" &
command_pid=$!
wait $command_pid

exit_code=$?
if [ $exit_code -ne 0 ] && [ "$ENV" = "prod" ]; then
echo "Error with exit code \`$exit_code\` in command \`$*\`, sending message to Mattermost"
curl -i -X POST -H "Content-Type: application/json" -d "{\"text\": \"Error with exit code \`$exit_code\` in command \`$*\`: \nLogs: \`scalingo --region osc-secnum-fr1 --app data-inclusion-api-prod logs --lines 1000 -F $CONTAINER -f\`\"}" $MATTERMOST_HOOK
fi

exit $exit_code

0 comments on commit f7c2c2d

Please sign in to comment.