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

Feat(alerts): add alert on mattermost in case of cron failure #280

Merged

Conversation

hlecuyer
Copy link
Contributor

No description provided.

@hlecuyer hlecuyer requested a review from vmttn as a code owner August 28, 2024 15:32
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from accd390 to 9e6d584 Compare August 28, 2024 15:44
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from 9e6d584 to 1e46fc1 Compare August 28, 2024 15:52
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from 1e46fc1 to ea07e5a Compare August 28, 2024 16:04
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from ea07e5a to db7271e Compare August 28, 2024 16:11
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch 2 times, most recently from a9d3559 to 467a3f7 Compare August 29, 2024 08:26
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch 3 times, most recently from 87f9466 to 67ec49e Compare August 29, 2024 08:47
Copy link
Contributor

@vperron vperron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quelques petits commentaires ! Et perso je ne l'appellerais pas run_management_command.sh ; c'etait pour les emplois spécifiquement parce que eux ils wrappent django-admin qui lance des... "management commands" Django.

Nous on a plutot un ... execute_and_notify.sh ?

#!/bin/sh
set -u

# Execute the command and capture standard output and error output
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attention aux commentaires misleadings ou inutiles.

$@ & : on sait que ça va executer la commande en detached

... par contre je ne vois pas comment cela capturerait les sorties ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commentaires obsoletes

@@ -0,0 +1,21 @@
#!/bin/sh
set -u
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pour quoi faire ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c'est sense rendre le script plus robuste. Ca genere une erreur en cas d'utilisation de variables non definit. Pas indispensable, mais pas incompatible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi pas, mais bon a priori a part les variables d'environnement (pas sûr qu'elles soient prises par -u ? A vérifier !) on les définit toutes en interne les variables bash donc ça va.

Ca aurait pu être intéressant si par exemple on était capables de crasher si $MATTERMOST_WEBHOOK_URL est non défini mais bon, dans ce cas on aura pas de notification non plus ^^ Scalingo va juste rester silencieux comme d'hab ! Donc so far, je vois pas trop à quoi ça servira dans le fond.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Les var d'env sont prise par le -u :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je fais souvent des choses par mimetisme (peut etre une habitude perdre). Les script sh commence souvent par ca. On peut le virer pour etre un peu plus yagni. J'ai pas d'avis fort la dessus.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ca ne changera rien au schmilblick final mais bon, gardons le ça ne pose pas de souci !


wait $command_pid

exit_code=$? # Capture the exit code of the previous command
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je pense que là aussi, vu le nom de la variable, on peut se passer du commentaire ^


exit_code=$? # Capture the exit code of the previous command

# Check if the exit code indicates an error (not equal to 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Execute the curl 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :)

Je raffinerais encore un poil plus:

Error $exit_code with command : `$escaped_command`

(note les `` qui seront formatés par Mattermost)

Ensuite pour la commande de logs, je me demande pourquoi le -f ou les 1000 lignes ? Et pareil, je le mettrais bien dans des backticks pour que ça soit chouli dans Mattermost.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-f pour la navigation
1000 ligne c'est pour eviter d'en avoir trop
Ok pour les ``

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes mais:

  • le -f te donne juste la "suite" de la commande or elle est déjà crashée a priori, donc pas de nouvelles lignes n'arriveront ?
  • 1000 lignes : ok, pourquoi pas !

# Check if the exit code indicates an error (not equal to 0)
if [ $exit_code -ne 0 ] && [ "$ENV" = "prod" ]; then
# Execute the curl command in case of an error
escaped_command="in command '$@'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est pour éviter d'envoyer des trucs bizarres dans Mattermost c'est ça ? Il s'est passé des choses bizarres ? En tout cas je recommande le backtick plus que les guillemets, cf. plus bas.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai galerer avec le format, j'avais des erreur chelou de caracter non escaped, je vais essayer de les remettre

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, intéressant, je veux bien voir dans quels cas du coup le format rend mattermost pas content, c'est une bonne info

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si tu as des soucis de caractères non échappés, potentiellement je te recommande d'utilise $* au lieu de $@.

"$@" (donc mis dans une chaine de caractères) devient une liste d'arguments séparés. "$*" devient un seul paramètre, qui sont tous les arguments du script ($1, $2, ...) concaténés en un seul.

@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from 31059b9 to f7cbe37 Compare August 29, 2024 09:35
api/Dockerfile Outdated
RUN chmod +x docker-entrypoint.sh
RUN chmod +x run_management_command.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

api/cron.json Outdated
@@ -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' && sh run_management_command.sh data-inclusion-api load_inclusion_data",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops


exit_code=$?
if [ $exit_code -ne 0 ] && [ "$ENV" = "prod" ]; then
# Execute the curl command in case of an error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tjs inutile je pense :) surtout avec le print qui vient expliquer exactement ça juste en dessous

api/cron.json Outdated
"size": "XL"
},
{
"command": "30 * * * * TQDM_DISABLE=1 test $ENV != 'prod' && data-inclusion-api load_inclusion_data",
"command": "30 * * * * TQDM_DISABLE=1 test $ENV != 'prod' && sh run_management_command.sh data-inclusion-api load_inclusion_data",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pas nécessaire de le mettre en staging du coup ? puisque à l'intérieur du script on hardcode "prod" deux fois ^^

@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch 2 times, most recently from 111ac63 to d5ae3bc Compare August 29, 2024 09:43
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from d5ae3bc to bbef4b3 Compare August 29, 2024 09:45
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from bbef4b3 to 9ef3a7e Compare August 29, 2024 09:49
Copy link
Contributor

@vperron vperron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LVGTM !

(tu peux virer le sh xxxx.sh puisque a priori, tu as:

  • un shbang
  • les droits d'execution appliqués via chmod dans le Dockerfile

Vérifie quand meme que le fichier est executable dans le repository pour que Scalingo puisse le lancer sans pb

@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch 2 times, most recently from 0ca81da to c40c683 Compare August 29, 2024 12:17
@hlecuyer hlecuyer force-pushed the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch from c40c683 to 1748f10 Compare August 29, 2024 12:32
@hlecuyer hlecuyer merged commit f7c2c2d into main Aug 29, 2024
8 of 9 checks passed
@hlecuyer hlecuyer deleted the hlecuyer/feat/alerts/add-wrapper-to-send-alert-in-mattermost branch August 29, 2024 12:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants