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

Calculate job percent complete only when the job has rows in the notification table #1664

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

whabanks
Copy link
Contributor

@whabanks whabanks commented Sep 12, 2023

Summary | Résumé

This PR adds a defensive check to get_job_partials to prevent a DivisionByZeroError that occurred when attempting to view a scheduled job. This issue was uncovered when investigating an alarm

Test instructions | Instructions pour tester la modification

  1. Schedule a bulk send via the api
  2. Navigate to your service's dashboard and click the link to the job under the Scheduled Jobs section
  3. Note that the page loads without any errors

… complete

- Display 0% complete if the job is scheduled instead of calclating to avoid a division by zero exception
@whabanks whabanks marked this pull request as draft September 12, 2023 20:47
@github-actions
Copy link

@@ -429,7 +429,9 @@ def get_job_partials(job, template):
"notifications_header": render_template(
"partials/jobs/notifications_header.html",
notifications=list(add_preview_of_content_to_notifications(notifications["notifications"])),
percentage_complete=(job["notifications_requested"] / job["notification_count"] * 100),
Copy link
Collaborator

@sastels sastels Sep 12, 2023

Choose a reason for hiding this comment

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

So if a job is still in a scheduled state then the job["notification_count"] is zero? I would have thought that would be the (eventual) total # of notifications?

Copy link
Member

Choose a reason for hiding this comment

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

I tend to agree that the eventual # of notifications should be displayed on this page, but it looks like the job stats come from here in API and at a glance they don't seem to include scheduled notifications. I think that change might be a separate PR for a related bug (i.e. that scheduled notifications aren't part of the stats)

In terms of a guard against this page crashing, this change seems to fit the bill - I'm not sure we could do much more on the admin side if we're given a 0 for the notification_count. Although maybe the check should be if job["notification_count"] is 0 rather than if the job is scheduled?

Out of scope for this PR, but for consistency sake, do scheduled notifications similarly count against the daily limit I wonder?

Copy link
Contributor Author

@whabanks whabanks Sep 13, 2023

Choose a reason for hiding this comment

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

Good point regarding the bug where scheduled notifications aren't part of the stats. When I tested locally and in staging the notifications for scheduled jobs only get created once the scheduled_for time has elapsed. So until the send occurs it seems we won't be able to include those notifications in the stats. It looks like this is the expected behaviour - I came across a test that indicates no stats should be returned if a job has no associated rows in the notifications table.

That said, checking the notification_count instead of if the job is scheduled makes more sense as eventually there will be notifications available in the DB for the percent complete calculation. I appreciate the discussion it's sparked a few ideas to flesh out unit tests in this area of the code! ❤️

To answer your question about scheduled notifications counting against the daily limit, I think the answer is yes. When we create a job we increment the daily count in Redis.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking in the jobs table for a job I just created on staging and scheduled for 48 hours in the future:
Screenshot 2023-09-21 at 4 36 11 PM

notification_count is the number of notifications that will be eventually sent. @whabanks if you are able to schedule a job and you see a notification_count of 0 that would be a bug. I think we still need to figure out how to reproduce that before we make any code changes.

@whabanks whabanks changed the title Calculate job percent complete only when the job is not scheduled Calculate job percent complete only when the job has rows in the notification table Sep 13, 2023
@smcmurtry
Copy link
Contributor

@whabanks do you know how to reproduce the original bug? I have not been able to reproduce it so I'm not sure how to test if this PR fixes the bug.

@whabanks
Copy link
Contributor Author

whabanks commented Sep 14, 2023

@whabanks do you know how to reproduce the original bug? I have not been able to reproduce it so I'm not sure how to test if this PR fixes the bug.

I tested and debugged this with the following steps:

  1. Via the API, schedule a bulk job for at least 1 day in the future
  2. Query the notifications in the notification table by the job id
  3. Note no notifications in the DB yet for that job
  4. Navigate to the Dashboard and click on the pending scheduled job under the Scheduled Jobs section
  5. Should result in the 500 error.

@smcmurtry
Copy link
Contributor

I followed those steps on Staging and I still wasn't able to reproduce the bug:

  1. via the API, I scheduled a bulk send for 48 hours in the future, for one of my services on Staging
  2. Querying the notifications table for the job id returns 0 rows (as expected, since the notifications aren't added until they are about to be sent)
  3. clicked on the job on my service dashboard
  4. page looks fine:
Screenshot 2023-09-21 at 4 29 37 PM

@smcmurtry smcmurtry closed this Sep 21, 2023
@smcmurtry smcmurtry reopened this Sep 21, 2023
@smcmurtry
Copy link
Contributor

I clicked the wrong button and closed this by accident! Sorry.

@github-actions
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants