diff --git a/Dockerfile b/Dockerfile index 5c84d1c..6aa19c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,4 +15,4 @@ ENV FLASK_APP=isacc_messaging.app:create_app() \ EXPOSE "${PORT}" -CMD gunicorn --bind "0.0.0.0:${PORT:-8000}" ${FLASK_APP} +CMD flask upgrade && gunicorn --bind "0.0.0.0:${PORT:-8000}" ${FLASK_APP} diff --git a/isacc_messaging/api/isacc_record_creator.py b/isacc_messaging/api/isacc_record_creator.py index e72f5ea..162d11f 100644 --- a/isacc_messaging/api/isacc_record_creator.py +++ b/isacc_messaging/api/isacc_record_creator.py @@ -342,22 +342,18 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]: 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, - # update the extension used to track next-outgoing-message time patient = resolve_reference(cr.recipient[0].reference) - patient.mark_next_outgoing() # Do not interact with CR if the patient is inactive or sending date is past the cutoff if not patient.active or cr.occurrenceDateTime.date < cutoff: cr.status = "revoked" cr.persist() - revoked_reason = "" + revoked_reason = "Past the cutoff" if not patient.active: revoked_reason = "Recipient is not active" - else: - revoked_reason = "Past the cutoff" cr.report_cr_status(status_reason=revoked_reason) errors.append({'id': cr.id, 'error': revoked_reason}) + patient.mark_next_outgoing() # update given state change continue # Otherwise, create a communication @@ -384,6 +380,7 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]: extra={"Updated Communication": stopped_comm}, level='debug' ) + patient.mark_next_outgoing() # update given state change continue # Otherwise, update according to the feedback from the dispatch @@ -419,6 +416,7 @@ def execute_requests(self) -> Tuple[List[dict], List[dict]]: extra={"resource": f"Communication/{comm.id}", "statusReason": e}, level='exception' ) + patient.mark_next_outgoing() # update given state change # Flooding system on occasions such as a holiday message to all, # leads to an overwhelmed system. Restrict the flood by processing diff --git a/migrations/versions/recalculate-next-outgoing-times.py b/migrations/versions/recalculate-next-outgoing-times.py new file mode 100644 index 0000000..3a016c2 --- /dev/null +++ b/migrations/versions/recalculate-next-outgoing-times.py @@ -0,0 +1,24 @@ +# Migration script generated for recalculate-next-outgoing-times +revision = '17f3b60d-0777-4d0e-bb32-82e670b573a7' +down_revision = 'None' + +import logging +from isacc_messaging.models.fhir import next_in_bundle +from isacc_messaging.models.isacc_patient import IsaccPatient as Patient + +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + + +def upgrade(): + # iterate over active patients; confirm/set next-outgoing extension + active_patients = Patient.active_patients() + for json_patient in next_in_bundle(active_patients): + patient = Patient(json_patient) + patient.mark_next_outgoing(persist_on_change=True) + logging.info('corrected next-outgoing-times') + + +def downgrade(): + # Nothing to undo + logging.info('downgraded') +