Skip to content

feat: scope immediate notification email batching per user#38866

Open
AhtishamShahid wants to merge 1 commit into
masterfrom
ahtisham/per-user-email-batching-66
Open

feat: scope immediate notification email batching per user#38866
AhtishamShahid wants to merge 1 commit into
masterfrom
ahtisham/per-user-email-batching-66

Conversation

@AhtishamShahid

@AhtishamShahid AhtishamShahid commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Description

Immediate-email notification batching was scoped by user and course_id. A learner enrolled in
several active courses (e.g. an instructor) therefore received a separate buffered digest per course:
an immediate notification in Course A, followed by one in Course B within the buffer window, was treated
as a brand-new "first" notification for Course B and sent immediately, producing multiple emails.

Notification preferences are now account-level (configured by type/app in Account Settings, not per
course), so batching should feel account-level too. This change scopes the immediate-email buffer by
user only
:

  1. The first eligible notification for a user is sent immediately.
  2. Additional eligible immediate notifications within NOTIFICATION_IMMEDIATE_EMAIL_BUFFER_MINUTES are
    grouped into one buffered digest, regardless of which enrolled course generated them.

What changed

openedx/core/djangoapps/notifications/email/tasks.py — dropped the course_id=course_key filter from
the three buffer queries:

  • decide_email_action — the "recent email / existing buffer" check now spans all of the user's courses.
  • schedule_digest_buffer — the last_sent lookup is no longer course-scoped, so the digest window
    starts at the user's most recent send.
  • send_buffered_digest — pools all of the user's scheduled notifications; per-notification course names
    are already resolved in create_email_digest_context, so multi-course digests render unchanged (no
    template changes needed).

course_key is intentionally retained in send_buffered_digest's signature so digest tasks enqueued
before this change still deserialize during rollout.

Supporting information

Resolves openedx/tutor-contrib-platform-notifications#66

Testing instructions

Automated (openedx/core/djangoapps/notifications/email/tests/test_tasks.py):

  • Inverted the two tests that asserted per-course independence
    (test_recent_email_in_different_course_schedules_buffer, test_multiple_courses_share_buffer) to
    assert the shared per-user buffer.
  • Added test_digest_collects_notifications_across_courses, asserting a single digest pools scheduled
    notifications from multiple courses.
  • Full suite: pytest openedx/core/djangoapps/notifications/email/tests/test_tasks.py93 passed.

Manual: with NOTIFICATION_IMMEDIATE_EMAIL_BUFFER_MINUTES set, an immediate notification in one course
followed by immediate notifications in a different course within the window produces a single buffered
digest email covering both courses (verified end-to-end on a Verawood dev stack).

This PR was created using @claude

Immediate-email batching was scoped by user AND course_id, so a learner
enrolled in multiple active courses received a separate buffered digest
per course. Notification preferences are now account-level, so batching
should feel account-level from the learner's perspective too.

Scope the buffer by user only in decide_email_action,
schedule_digest_buffer and send_buffered_digest: the first eligible
notification is sent immediately, and additional immediate notifications
within NOTIFICATION_IMMEDIATE_EMAIL_BUFFER_MINUTES are grouped into a
single digest regardless of which course generated them. The digest
already resolves per-notification course names, so multi-course digests
render unchanged.

course_key is retained in send_buffered_digest's signature so digest
tasks enqueued before the change still deserialize.

Closes openedx/tutor-contrib-platform-notifications#66
Copilot AI review requested due to automatic review settings July 9, 2026 11:00
@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Jul 9, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @AhtishamShahid!

This repository is currently maintained by @openedx/wg-maintenance-openedx-platform.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR changes immediate-email notification batching to be per user (instead of per user + course), so that multiple immediate notifications across different courses within the buffer window are combined into a single buffered digest email.

Changes:

  • Removed course_id=course_key scoping from the immediate-email buffer decision, last-sent lookup, and buffered-digest collection queries.
  • Updated/improved docstrings/comments to reflect the new per-user batching semantics while retaining course_key in task signatures for rollout compatibility.
  • Updated and added tests to assert cross-course buffering and digest pooling behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
openedx/core/djangoapps/notifications/email/tasks.py Drops course scoping from immediate-email buffering/digest queries to batch across all courses per user.
openedx/core/djangoapps/notifications/email/tests/test_tasks.py Updates expectations for shared per-user buffering and adds coverage for cross-course digest collection.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 547 to 555
buffer_minutes = get_buffer_minutes()
buffer_threshold = datetime.now() - timedelta(minutes=buffer_minutes)

# Use select_for_update to prevent race conditions
# Use select_for_update to prevent race conditions.
# Scoped by user only so all of the user's courses share one buffer window.
recent_notifications = Notification.objects.select_for_update().filter(
user=user,
course_id=course_key,
created__gte=buffer_threshold
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

Make email batching per-user only instead of per-user and per-course

3 participants