-
Notifications
You must be signed in to change notification settings - Fork 19
Dehydrate timeout_notifications nightly task
#2877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
| convert_local_timezone_to_utc, | ||
| convert_utc_to_local_timezone, | ||
| ) | ||
| from sqlalchemy import asc, desc, func | ||
| from sqlalchemy import asc, desc, func, update | ||
| from sqlalchemy.dialects.postgresql import insert | ||
| from sqlalchemy.orm import defer, joinedload | ||
| from sqlalchemy.orm.exc import NoResultFound | ||
|
|
@@ -48,7 +48,10 @@ | |
| ScheduledNotification, | ||
| Service, | ||
| ServiceDataRetention, | ||
| Template, | ||
| compute_formatted_status, | ||
| ) | ||
| from app.types import NotificationCallbackData | ||
| from app.utils import escape_special_characters | ||
|
|
||
|
|
||
|
|
@@ -238,24 +241,24 @@ def _update_notification_status( | |
|
|
||
| @transactional | ||
| def _update_notification_statuses(updates): | ||
| for update in updates: | ||
| notification = update.get("notification") | ||
| bounce_response = update.get("bounce_response") | ||
| provider_response = update.get("provider_response") | ||
| feedback_reason = update.get("feedback_reason") | ||
| for u in updates: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was colliding with the |
||
| notification = u.get("notification") | ||
| bounce_response = u.get("bounce_response") | ||
| provider_response = u.get("provider_response") | ||
| feedback_reason = u.get("feedback_reason") | ||
|
|
||
| final_status = _decide_permanent_temporary_failure(current_status=notification.status, status=update.get("new_status")) | ||
| final_status = _decide_permanent_temporary_failure(current_status=notification.status, status=u.get("new_status")) | ||
| notification.status = final_status | ||
| if provider_response: | ||
| notification.provider_response = update.get("provider_response") | ||
| notification.provider_response = u.get("provider_response") | ||
| if bounce_response: | ||
| notification.feedback_type = bounce_response.get("feedback_type") | ||
| notification.feedback_subtype = bounce_response.get("feedback_subtype") | ||
| notification.ses_feedback_id = bounce_response.get("ses_feedback_id") | ||
| notification.ses_feedback_date = bounce_response.get("ses_feedback_date") | ||
| if feedback_reason: | ||
| notification.feedback_reason = feedback_reason | ||
| update_notification_statuses([update.get("notification") for update in updates]) | ||
| update_notification_statuses([u.get("notification") for u in updates]) | ||
|
|
||
|
|
||
| @transactional | ||
|
|
@@ -554,17 +557,63 @@ def dao_delete_notifications_by_id(notification_id): | |
|
|
||
|
|
||
| def _timeout_notifications(current_statuses, new_status, timeout_start, updated_at): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add batching to this task in the future if we're still seeing issues with performance here. |
||
| notifications = Notification.query.filter( | ||
| Notification.created_at < timeout_start, | ||
| Notification.status.in_(current_statuses), | ||
| Notification.notification_type != LETTER_TYPE, | ||
| ).all() | ||
| Notification.query.filter( | ||
| Notification.created_at < timeout_start, | ||
| Notification.status.in_(current_statuses), | ||
| Notification.notification_type != LETTER_TYPE, | ||
| ).update({"status": new_status, "updated_at": updated_at}, synchronize_session=False) | ||
| return notifications | ||
| # Uses Postgres UPDATE … RETURNING as a CTE so the bulk status transition | ||
| # and the callback data fetch happen in a single atomic round-trip. | ||
| # This avoids materialising a potentially large ID list in Python | ||
| n = Notification.__table__ | ||
| t = Template.__table__ | ||
|
|
||
| update_cte = ( | ||
| update(n) | ||
| .where( | ||
| n.c.created_at < timeout_start, | ||
| n.c.status.in_(current_statuses), | ||
| n.c.notification_type != LETTER_TYPE, | ||
| ) | ||
| .values({n.c.status: new_status, n.c.updated_at: updated_at}) | ||
| .returning( | ||
| n.c.id, | ||
| n.c.service_id, | ||
| n.c["to"], | ||
| n.c.notification_type, | ||
| n.c.client_reference, | ||
| n.c.provider_response, | ||
| n.c.updated_at, | ||
| n.c.created_at, | ||
| n.c.sent_at, | ||
| n.c.feedback_subtype, | ||
| n.c.feedback_reason, | ||
| n.c.template_id, | ||
| ) | ||
| .cte("updated") | ||
| ) | ||
|
|
||
| stmt = update_cte.join(t, t.c.id == update_cte.c.template_id).select() | ||
|
|
||
| rows = db.session.execute(stmt).fetchall() | ||
|
|
||
| # Lightweight dataclass with pre-computed formatted_status | ||
| return [ | ||
| NotificationCallbackData( | ||
| id=str(row.id), | ||
| service_id=str(row.service_id), | ||
| to=row._mapping["to"], | ||
| status=new_status, | ||
| formatted_status=compute_formatted_status( | ||
| row.template_type, | ||
| new_status, | ||
| row.feedback_subtype, | ||
| row.feedback_reason, | ||
| ), | ||
| notification_type=row.notification_type, | ||
| client_reference=row.client_reference, | ||
| provider_response=row.provider_response, | ||
| created_at=row.created_at, | ||
| updated_at=row.updated_at, | ||
| sent_at=row.sent_at, | ||
| ) | ||
| for row in rows | ||
| ] | ||
|
|
||
|
|
||
| def dao_timeout_notifications(timeout_period_in_seconds): | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.