From 1cc1d2b4efdc86af397137dd4d9479022a73085d Mon Sep 17 00:00:00 2001 From: Simon Hamery Date: Fri, 4 Oct 2024 15:44:37 +0200 Subject: [PATCH] Ajout wrapper cron sentry (#204) * feat: ajoute un wrapper pour capturer les erreurs des crons sur sentry * feat: add sentry-cli * fix: indentation * fix: sentry-cli install * fix: sentry_wrapper_cron path * fix: sentry wrapper fonctionnel --- roles/bootstrap/tasks/install_sentry_cli.yaml | 5 +++ roles/bootstrap/tasks/main.yaml | 4 +++ roles/bootstrap/tasks/setup_cron.yaml | 12 ++++--- scripts/sentry_wrapper_cron.sh | 34 +++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 roles/bootstrap/tasks/install_sentry_cli.yaml create mode 100755 scripts/sentry_wrapper_cron.sh diff --git a/roles/bootstrap/tasks/install_sentry_cli.yaml b/roles/bootstrap/tasks/install_sentry_cli.yaml new file mode 100644 index 00000000..47da610a --- /dev/null +++ b/roles/bootstrap/tasks/install_sentry_cli.yaml @@ -0,0 +1,5 @@ +--- +- name: Install @sentry/cli using npm + community.general.npm: + name: '@sentry/cli' + global: true diff --git a/roles/bootstrap/tasks/main.yaml b/roles/bootstrap/tasks/main.yaml index 0fd36cde..586e5e9a 100644 --- a/roles/bootstrap/tasks/main.yaml +++ b/roles/bootstrap/tasks/main.yaml @@ -11,6 +11,10 @@ - name: Install nodeJS 18.x ansible.builtin.include_tasks: install_node.yaml +## setup sentry-cli +- name: Install sentry-cli + ansible.builtin.include_tasks: install_sentry_cli.yaml + ## Setup pm2 - name: Install and setup pm2 ansible.builtin.include_tasks: install_pm2.yaml diff --git a/roles/bootstrap/tasks/setup_cron.yaml b/roles/bootstrap/tasks/setup_cron.yaml index 7a2bdc81..e0d0e9c4 100644 --- a/roles/bootstrap/tasks/setup_cron.yaml +++ b/roles/bootstrap/tasks/setup_cron.yaml @@ -2,13 +2,15 @@ - name: Set cron env variables ansible.builtin.set_fact: envvar_prefix: NODE_ENV=production MONGODB_URL=mongodb://127.0.0.1/db_{{ item.name }} + env_file_path: "{{ repository_folder }}/.env" + wrapper_script_path: "/opt/mes-aides/scripts/sentry_wrapper_cron.sh" - name: Add a cron job for stats generation become_user: "{{ server_user_name }}" ansible.builtin.cron: name: generate stats for {{ item.name }} minute: "23" hour: "2" - job: ({{ envvar_prefix }} /usr/bin/node {{ repository_folder }}/dist-server/backend/lib/stats) + job: ({{ envvar_prefix }} {{ wrapper_script_path }} {{ env_file_path }} /usr/bin/node {{ repository_folder }}/dist-server/backend/lib/stats) - name: Add a cron job to send initial survey emails become_user: "{{ server_user_name }}" ansible.builtin.cron: @@ -17,7 +19,7 @@ hour: "4" job: >- (cd {{ repository_folder }} && - {{ envvar_prefix }} /usr/bin/node + {{ envvar_prefix }} {{ wrapper_script_path }} {{ env_file_path }} /usr/bin/node ./dist-server/tools/email-sending-tool.js send initial-survey --multiple 1000 >> /var/log/{{ server_user_name }}/{{ item.name }}_emails.log 2>&1) - name: Add a cron job to send initial survey by sms @@ -28,7 +30,7 @@ hour: "17" job: >- (cd {{ repository_folder }} && - {{ envvar_prefix }} /usr/bin/node + {{ envvar_prefix }} {{ wrapper_script_path }} {{ env_file_path }} /usr/bin/node ./dist-server/tools/sms-sending-tool.js send initial-survey --multiple 100 >> /var/log/{{ server_user_name }}/{{ item.name }}_sms.log 2>&1) - name: Add a cron job to anonymize Simulation and Followup data collections @@ -37,7 +39,7 @@ name: anonymize simulation and followup data collections for {{ item.name }} minute: "0" hour: "5" - job: (cd {{ repository_folder }} && {{ envvar_prefix }} npm run tools:cleaner) + job: (cd {{ repository_folder }} && {{ envvar_prefix }} {{ wrapper_script_path }} {{ env_file_path }} npm run tools:cleaner) - name: Create cron job to execute generate_mongo_stats.sh monthly become_user: "{{ server_user_name }}" ansible.builtin.cron: @@ -45,4 +47,4 @@ minute: "0" hour: "0" day: "1" - job: (cd {{ repository_folder }} && {{ envvar_prefix }} npm run tools:generate-mongo-stats) + job: (cd {{ repository_folder }} && {{ envvar_prefix }} {{ wrapper_script_path }} {{ env_file_path }} npm run tools:generate-mongo-stats) diff --git a/scripts/sentry_wrapper_cron.sh b/scripts/sentry_wrapper_cron.sh new file mode 100755 index 00000000..1d157df3 --- /dev/null +++ b/scripts/sentry_wrapper_cron.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# This script is executing the command passed as 2nd argument +# and send an event to Sentry if the command fails. +# The .env file path is passed as the first argument. + + +ENV_FILE_PATH=$1 +shift # Remove the first argument +if [ -z "$ENV_FILE_PATH" ]; then + echo "Error: .env file path is missing" + exit 1 +fi +source $ENV_FILE_PATH + +if [ -z "$SENTRY_CRON_DSN" ]; then + echo "Error: SENTRY_CRON_DSN is not set in .env file" + exit 1 +fi + +echo "SENTRY_CRON_DSN is set to: $SENTRY_CRON_DSN" + +# The wrapped command is executed and the result status is stored +COMMAND="$@" +OUTPUT=$(eval "$COMMAND" 2>&1) +STATUS=$? + +if [ $STATUS -ne 0 ]; then + echo "Cron job failed: $COMMAND" + echo "Error output: $OUTPUT" + SENTRY_DSN="$SENTRY_CRON_DSN" sentry-cli send-event -m "Cron job failed: $COMMAND | Error: $OUTPUT" +fi + +exit $STATUS