forked from uwcirg/helloworld-confidential-client-sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
robustly handle failed callbacks from Twilio (#73)
* refactor custom exceptions for use throughout the project. * on failed twilio SID lookup, push the callback request to REDIS for a delayed 2nd and 3rd attempt. * Update isacc_messaging/api/views.py Co-authored-by: Ivan Cvitkovic <ivanc@uw.edu> --------- Co-authored-by: Ivan Cvitkovic <ivanc@uw.edu>
- Loading branch information
Showing
4 changed files
with
105 additions
and
12 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
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,10 @@ | ||
"""Module to define exceptions used by isacc_messaging.""" | ||
|
||
class IsaccTwilioSIDnotFound(Exception): | ||
"""Raised when Twilio calls with SID that can't be found""" | ||
pass | ||
|
||
|
||
class IsaccRequestRetriesExhausted(Exception): | ||
"""Raised when max retries have been tried w/o success""" | ||
pass |
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,44 @@ | ||
"""Functions used for robust handling of failed requests | ||
Basic model: a request fails. Rather than give up, push the request | ||
to a job queue and try again from a consumer. | ||
""" | ||
import json | ||
import redis | ||
from flask import current_app | ||
|
||
from isacc_messaging.exceptions import IsaccRequestRetriesExhausted | ||
|
||
|
||
def serialize_request(req, attempt_count=1, max_retries=3): | ||
"""Given a request object, returns a serialized form | ||
:param req: The request object | ||
:param attempt_count: Increment from previous failure on each call | ||
:param max_retries: Maximum number of retries before giving up | ||
Need a serialized form of the request to push into a job queue. | ||
This also maintains and enforces the number of attempts doesn't | ||
exceed the maximum. | ||
""" | ||
serialized_form = json.dumps({ | ||
"method": req.method, | ||
"url": req.url, | ||
"headers": dict(req.headers), | ||
"body": req.get_data(as_text=True), | ||
"attempt_count": attempt_count, | ||
"max_retries": max_retries | ||
}) | ||
if attempt_count > max_retries: | ||
raise IsaccRequestRetriesExhausted(serialized_form) | ||
return serialized_form | ||
|
||
def queue_request(serialized_request): | ||
redis_client = redis.StrictRedis.from_url(current_app.config.get("REQUEST_CACHE_URL")) | ||
redis_client.lpush("http_request_queue", serialized_request) | ||
|
||
|
||
def pop_request(): | ||
redis_client = redis.StrictRedis.from_url(current_app.config.get("REQUEST_CACHE_URL")) | ||
return redis_client.rpop("http_request_queue") | ||
|