-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lowering retry period for high priority emails to 25 seconds (#2031)
* Decoupled scan malware code + lowering retry period for high priority emails * Extract common email retry handling logic into its own function * Cleaned up import * Forgot to provide default value to optional fn arg * Fixed test import * Isolated retry task param builder in a class * Cleaned up import * Fixed moved refs * Trying a different strategy to fix circular import * Fixing another bad import ref * Introducing celery utils module instead of using celery root one * Cover edge cases + modified tests * Formatting * Sort imports * Make notification_process_type param optional * Fixed edge case when template not associated with notification obj * Fixing params order * Fixing regression tests * More tests * Added null protection against a potential NPE * Formatting * Fix imports --------- Co-authored-by: Steve Astels <[email protected]>
- Loading branch information
1 parent
643ff13
commit 68f2d3c
Showing
6 changed files
with
130 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Any, Dict, Optional | ||
|
||
from flask import current_app | ||
|
||
from app import config, models | ||
|
||
# Default retry periods for sending notifications. | ||
RETRY_DEFAULT = 300 | ||
RETRY_HIGH = 25 | ||
|
||
|
||
class CeleryParams(object): | ||
# Important to load from the object and not the module to avoid | ||
# circular imports, back and forth between the app and celery modules. | ||
|
||
RETRY_PERIODS = { | ||
models.BULK: RETRY_DEFAULT, | ||
models.NORMAL: RETRY_DEFAULT, | ||
models.PRIORITY: RETRY_HIGH, | ||
None: RETRY_HIGH, # In case we cannot identify the priority, treat it as high. | ||
} | ||
|
||
@staticmethod | ||
def retry(notification_process_type: Optional[str] = None, countdown: Optional[int] = None) -> Dict[str, Any]: | ||
""" | ||
Build task params for the sending parameter retry tasks. | ||
If the notification is a high priority SMS, set the retry policy to retry every 25 seconds | ||
else fall back to the default retry policy of retrying every 5 minutes. | ||
Provide an override parameter for cases the calling task wants to override the retry policy. | ||
""" | ||
params: dict[str, Any] = {"queue": config.QueueNames.RETRY} | ||
if current_app.config["FF_CELERY_CUSTOM_TASK_PARAMS"] is False: | ||
return params | ||
|
||
if countdown is not None: | ||
params["countdown"] = countdown | ||
else: | ||
# Overring the retry policy is only supported for SMS for now; | ||
# email support coming later. | ||
params["countdown"] = CeleryParams.RETRY_PERIODS[notification_process_type] | ||
return params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +0,0 @@ | ||
from typing import Any, Dict | ||
|
||
from flask import current_app | ||
|
||
from app.config import QueueNames | ||
from app.models import BULK, NORMAL, PRIORITY, SMS_TYPE | ||
|
||
# Default retry periods for sending notifications. | ||
RETRY_DEFAULT = 300 | ||
RETRY_HIGH = 25 | ||
|
||
RETRY_PERIODS = { | ||
BULK: RETRY_DEFAULT, | ||
NORMAL: RETRY_DEFAULT, | ||
PRIORITY: RETRY_HIGH, | ||
} | ||
|
||
|
||
def build_retry_task_params(notification_type: str, notification_process_type: str) -> Dict[str, Any]: | ||
""" | ||
Build task params for the sending parameter retry tasks. | ||
If the notification is a high priority SMS, set the retry policy to retry every 25 seconds | ||
else fall back to the default retry policy of retrying every 5 minutes. | ||
""" | ||
params: dict[str, Any] = {"queue": QueueNames.RETRY} | ||
if current_app.config["FF_CELERY_CUSTOM_TASK_PARAMS"] is False: | ||
return params | ||
|
||
# Overring the retry policy is only supported for SMS for now; | ||
# email support coming later. | ||
if notification_type == SMS_TYPE: | ||
params["countdown"] = RETRY_PERIODS[notification_process_type] | ||
else: | ||
params["countdown"] = RETRY_DEFAULT | ||
return params | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters