Skip to content

Commit

Permalink
Ajout wrapper cron sentry (production) (#198)
Browse files Browse the repository at this point in the history
* chore: upgrade version github actions (cd.yml) (#201)

downgrade python github action to v4

fix cd

* 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
  • Loading branch information
Shamzic authored Nov 20, 2024
1 parent 55560e9 commit 293718f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions roles/bootstrap/tasks/install_sentry_cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Install @sentry/cli using npm
community.general.npm:
name: '@sentry/cli'
global: true
4 changes: 4 additions & 0 deletions roles/bootstrap/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions roles/bootstrap/tasks/setup_cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -37,12 +39,12 @@
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:
name: Execute generate_mongo_stats.sh monthly {{ item.name }}
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)
34 changes: 34 additions & 0 deletions scripts/sentry_wrapper_cron.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 293718f

Please sign in to comment.