Skip to content
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

#2058 Twilio status override, Celery scheduler #2088

Merged
merged 38 commits into from
Nov 4, 2024

Conversation

mchlwellman
Copy link

@mchlwellman mchlwellman commented Oct 31, 2024

Description

This PR adds a celery beat task to update Twilio messages that are older than hour, and not in a final state. The celery task is executed every 5 minutes, and pulls back at most 500 notifications. If a 429 is encountered, the task stops. Any other exceptions are ignored.

issue #2058

How Has This Been Tested?

Screenshot 2024-11-04 at 12 20 35 PM - Updated the notification status to be 'sending' in psql Screenshot 2024-11-04 at 12 17 19 PM - Waited for the job to pick up the notification. The job queries for notifications older than an hour that are not in a final state. Screenshot 2024-11-04 at 1 19 55 PM - Notification is updated with correct status Screenshot 2024-11-04 at 1 21 00 PM

Checklist

  • I have assigned myself to this PR
  • PR has an appropriate title: #9999 - What the thing does
  • PR has a detailed description, including links to specific documentation
  • I have added the appropriate labels to the PR.
  • I did not remove any parts of the template, such as checkboxes even if they are not used
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to any documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works. Testing guidelines
  • I have ensured the latest main is merged into my branch and all checks are green prior to review
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • The ticket was moved into the DEV test column when I began testing this change

@mchlwellman mchlwellman self-assigned this Oct 31, 2024
@mchlwellman mchlwellman linked an issue Oct 31, 2024 that may be closed by this pull request
6 tasks
@mchlwellman mchlwellman marked this pull request as ready for review November 4, 2024 18:22
@mchlwellman mchlwellman requested a review from a team as a code owner November 4, 2024 18:22
.where(Notification.notification_type == 'sms')
.where(Notification.sent_by == 'twilio')
.where(~Notification.status.in_(NOTIFICATION_STATUS_TYPES_COMPLETED))
.where(Notification.created_at < one_hour_ago)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be greater than one_hour_ago?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the variable one_hour_ago is one hour in the past, so we're looking for things less than that. Greater than would be newer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't insist that you make a change for this, but I think you can put all the clauses in one "where" rather than have a separate "where" for each clause.

)
def test__get_notifications_datefilter(sample_notification, minute_offset, expected):
"""Test that _get_notifications() returns either a list with the test notification, or an empty list, depending
on the parametrized minute_offset. If the notification was created more than one hour ago, it is not returned."""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we wanted to only fetch notification created earlier than one hour ago?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, that comment is backwards.

(180, True),
],
)
def test__get_notifications_datefilter(sample_notification, minute_offset, expected):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is an extra underscore here, please remove

.where(Notification.notification_type == 'sms')
.where(Notification.sent_by == 'twilio')
.where(~Notification.status.in_(NOTIFICATION_STATUS_TYPES_COMPLETED))
.where(Notification.created_at < one_hour_ago)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't insist that you make a change for this, but I think you can put all the clauses in one "where" rather than have a separate "where" for each clause.

.order_by(Notification.created_at)
.limit(current_app.config['TWILIO_STATUS_PAGE_SIZE'])
)
return db.session.execute(query).scalars().all()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works:

Suggested change
return db.session.execute(query).scalars().all()
return db.session.scalars(stmt).all()


current_app.logger.info('Getting notifications to update status')
one_hour_ago = datetime.now(timezone.utc) - timedelta(hours=1)
query = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided earlier that we would name all queries "stmt" (statement) for consistency.

Suggested change
query = (
stmt = (

current_app.logger.info('Getting notifications to update status')
one_hour_ago = datetime.now(timezone.utc) - timedelta(hours=1)
query = (
select([Notification])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
select([Notification])
select(Notification)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have these changes implemented and deployed

@mchlwellman mchlwellman merged commit eae4afb into main Nov 4, 2024
13 checks passed
@mchlwellman mchlwellman deleted the 2058-twilio-status-update-celery branch November 4, 2024 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants