improve send_email_with_rate_control to use on any day range
This commit is contained in:
parent
e4c4797cdb
commit
85b87bbacb
|
@ -243,26 +243,28 @@ def send_email_with_rate_control(
|
||||||
subject,
|
subject,
|
||||||
plaintext,
|
plaintext,
|
||||||
html=None,
|
html=None,
|
||||||
max_alert_24h=MAX_ALERT_24H,
|
max_nb_alert=MAX_ALERT_24H,
|
||||||
|
nb_day=1,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Same as send_email with rate control over alert_type.
|
"""Same as send_email with rate control over alert_type.
|
||||||
For now no more than _MAX_ALERT_24h alert can be sent in the last 24h
|
Make sure no more than `max_nb_alert` emails are sent over the period of `nb_day` days
|
||||||
|
|
||||||
Return true if the email is sent, otherwise False
|
Return true if the email is sent, otherwise False
|
||||||
"""
|
"""
|
||||||
to_email = to_email.lower().strip()
|
to_email = to_email.lower().strip()
|
||||||
one_day_ago = arrow.now().shift(days=-1)
|
min_dt = arrow.now().shift(days=-1 * nb_day)
|
||||||
nb_alert = (
|
nb_alert = (
|
||||||
SentAlert.query.filter_by(alert_type=alert_type, to_email=to_email)
|
SentAlert.query.filter_by(alert_type=alert_type, to_email=to_email)
|
||||||
.filter(SentAlert.created_at > one_day_ago)
|
.filter(SentAlert.created_at > min_dt)
|
||||||
.count()
|
.count()
|
||||||
)
|
)
|
||||||
|
|
||||||
if nb_alert >= max_alert_24h:
|
if nb_alert >= max_nb_alert:
|
||||||
LOG.warning(
|
LOG.warning(
|
||||||
"%s emails were sent to %s in the last 24h, alert type %s",
|
"%s emails were sent to %s in the last %s days, alert type %s",
|
||||||
nb_alert,
|
nb_alert,
|
||||||
to_email,
|
to_email,
|
||||||
|
nb_day,
|
||||||
alert_type,
|
alert_type,
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -523,7 +523,7 @@ async def forward_email_to_mailbox(
|
||||||
mailbox=mailbox,
|
mailbox=mailbox,
|
||||||
mailbox_url=mailbox_url,
|
mailbox_url=mailbox_url,
|
||||||
),
|
),
|
||||||
max_alert_24h=1,
|
max_nb_alert=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
# retry later
|
# retry later
|
||||||
|
|
Loading…
Reference in New Issue