From a9d35596edec773c13eb770e579d959fe18b3035 Mon Sep 17 00:00:00 2001 From: Hugo Lecuyer Date: Wed, 28 Aug 2024 17:30:04 +0200 Subject: [PATCH] wip --- api/Dockerfile | 4 ++++ api/cron.json | 4 ++++ api/run_management_command.sh | 25 +++++++++++++++++++++++++ api/test.py | 21 +++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 api/run_management_command.sh create mode 100644 api/test.py 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..e737fc438 100644 --- a/api/cron.json +++ b/api/cron.json @@ -11,6 +11,10 @@ { "command": "0 4 * * * vacuumdb --full --analyze --verbose --table api__structures --table api__services $DATABASE_URL", "size": "S" + }, + { + "command": "*/10 * * * * sh run_management_command.sh python test.py 1", + "size": "S" } ] } diff --git a/api/run_management_command.sh b/api/run_management_command.sh new file mode 100644 index 000000000..4d8b7b441 --- /dev/null +++ b/api/run_management_command.sh @@ -0,0 +1,25 @@ +#!/bin/sh +set -u + +# Execute the command and capture standard output and error output +"$@" & +command_pid=$! # Capture the PID of the previous command + +wait $command_pid + +exit_code=$? # Capture the exit code of the previous command + + +# Print the exit code of the Python script +echo "The exit code of the command is: $exit_code and pid = $command_pid" + +# Check if the exit code indicates an error (not equal to 0) +if [ $exit_code -ne 0 ] && [ "$ENV" = "staging" ]; then + # Execute the wget 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_codega run # Run the script with the command to execute as an argument diff --git a/api/test.py b/api/test.py new file mode 100644 index 000000000..4282a09aa --- /dev/null +++ b/api/test.py @@ -0,0 +1,21 @@ +import sys + + +def main(): + if len(sys.argv) != 2: + print("Usage: python script.py <0|1>") + sys.exit(1) + + arg = sys.argv[1] + + if arg == "1": + raise ValueError("An error occurred because the argument is 1") + elif arg == "0": + pass + else: + print("Invalid argument. Please use 0 or 1.") + sys.exit(1) + + +if __name__ == "__main__": + main()