Purpose
This procedure is designed for using timeout in resending SMS flow.
Key Point
In order to remove expired attempts records are saved in Redis db with expire parameter.
Input Parameters
MAX_ATTEMPTS_COUNT - specifies the maximum number of retries before resend is rejected;
SEND_TIMEOUT -specifies a number of seconds to wait to retry sending after an error condition ;(attempts.count() >= MAX_ATTEMPTS_COUNT);
entity_name - i.e. “medication_request”;
entity_id - i.e. medication_request_id.
Logic
Get array with all attempts which start with “{entity_name}:{entity_id}” from Redis db using script:
[redis] KEYS $"{entity_name}:{entity_id}_*"
Get attempts.count();
If attempts.count() < MAX_ATTEMPTS_COUNT
generate attempt_id using structure: {entity_name}:{entity_id}_{datetime.now()},
(i.e. "medication_request:b075f148-7f93-4fc2-b2ec-2d81b19a9b7_2022-12-22T10:30:45.255687")save attempt to Redis db using attempt_id as key and datetime.now() as value using script:
[redis] set attempt_id $"{datetime.now()}"
set timeout to attempt with SEND_TIMEOUT parameter using script:
[redis] expire key SEND_TIMEOUT
return 200 (ok).20
Else (attempts.count() >= MAX_ATTEMPTS_COUNT)
do not save attempt;
return error 429 ("Sending SMS timeout. Try later. Next attempt will be available at <attempts.oldest.value + SEND_TIMEOUT>").