Skip to content

Commit

Permalink
Put in governor to restrict the number of messages sent per cron run.…
Browse files Browse the repository at this point in the history
… This is to alleviate an overwhelmed system on occasions such as a holiday message sent to all users. (#74)
  • Loading branch information
pbugni authored Nov 21, 2024
1 parent dcd1f37 commit ce46a9a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions isacc_messaging/api/isacc_record_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ def score_message(self, message):

def execute_requests(self) -> Tuple[List[dict], List[dict]]:
"""
For all due CommunicationRequests, generate SMS, create Communication resource, and update CommunicationRequest
For all due CommunicationRequests (up to throttle limit), generate SMS, create Communication resource, and update CommunicationRequest
"""
successes = []
errors = []

throttle_limit = 30 # conservative value based on heuristics from logs
now = datetime.now().astimezone()
cutoff = now - timedelta(days=2)

Expand All @@ -339,6 +339,7 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
"occurrence": f"le{now.isoformat()}",
})

sent = 0
for cr_json in next_in_bundle(result):
cr = CommunicationRequest(cr_json)
# as that message was likely the next-outgoing for the patient,
Expand Down Expand Up @@ -419,6 +420,13 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]:
level='exception'
)

# Flooding system on occasions such as a holiday message to all,
# leads to an overwhelmed system. Restrict the flood by processing
# only throttle_limit per run.
sent += 1
if sent > throttle_limit:
break

return successes, errors

def process_cr(self, cr: CommunicationRequest):
Expand Down

0 comments on commit ce46a9a

Please sign in to comment.