Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout wrapper cron sentry #204

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading