From ebbe2c5d16b5e74949957d7bc1e8817d7d8a2a3e Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sat, 1 Aug 2020 22:16:34 +0000 Subject: [PATCH] add random delay between sending emails to prevent thread limit errors when using 3rd party smtp like O365 --- api/tacticalrmm/agents/tasks.py | 3 +++ api/tacticalrmm/checks/tasks.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/api/tacticalrmm/agents/tasks.py b/api/tacticalrmm/agents/tasks.py index a0c442a7..da466131 100644 --- a/api/tacticalrmm/agents/tasks.py +++ b/api/tacticalrmm/agents/tasks.py @@ -2,6 +2,7 @@ import os import subprocess from loguru import logger from time import sleep +import random import requests from packaging import version as pyver @@ -150,6 +151,7 @@ def uninstall_agent_task(salt_id): @app.task def agent_outage_email_task(pk): + sleep(random.randint(1, 15)) outage = AgentOutage.objects.get(pk=pk) outage.send_outage_email() outage.outage_email_sent = True @@ -158,6 +160,7 @@ def agent_outage_email_task(pk): @app.task def agent_recovery_email_task(pk): + sleep(random.randint(1, 15)) outage = AgentOutage.objects.get(pk=pk) outage.send_recovery_email() outage.recovery_email_sent = True diff --git a/api/tacticalrmm/checks/tasks.py b/api/tacticalrmm/checks/tasks.py index bff090c0..0c1e202b 100644 --- a/api/tacticalrmm/checks/tasks.py +++ b/api/tacticalrmm/checks/tasks.py @@ -1,4 +1,6 @@ import datetime as dt +import random +from time import sleep from tacticalrmm.celery import app from django.core.exceptions import ObjectDoesNotExist @@ -16,6 +18,7 @@ def handle_check_email_alert_task(pk): # first time sending email if not check.email_sent: + sleep(random.randint(1, 10)) check.send_email() check.email_sent = djangotime.now() check.save(update_fields=["email_sent"]) @@ -23,6 +26,7 @@ def handle_check_email_alert_task(pk): # send an email only if the last email sent is older than 24 hours delta = djangotime.now() - dt.timedelta(hours=24) if check.email_sent < delta: + sleep(random.randint(1, 10)) check.send_email() check.email_sent = djangotime.now() check.save(update_fields=["email_sent"])