Skip to content

Known work around Bug: Email delay-previously emails generated by examining a name had a 5-minute delay. #21835

Closed
2 of 2 issues completed

Description

@ozamani9gh

Priority 3: Email delay-previously emails generated by examining a name had a 5-minute delay. This was helpful when examining as any mistakes (forgot to put condition on, made error in examining, etc.) wouldn’t result in multiple outputs that are confusing for the client. Preference for a short delay that only generates the last email. (Frequency 100%) Time cost: approx. 20-30min/week

Sub-issues

Sub-issues

2 of 2 Issues completed
Loading 2 sub-issues

Activity

  1. added
    Names Team
    on Jun 17, 2024
  2. ozamani9gh commented on Jun 25, 2024

    @ozamani9gh
    CollaboratorAuthor

    Update from Joshua:
    • The removal of the output delay creates confusion for clients if an examiner were to catch a mistake and go back to fix it (generates multiple different results emails to clients, who then call or email in confusion which creates a cascade effect). Even a few minutes delay for output generation would help reduce this.

  3. ozamani9gh commented on Jun 27, 2024

    @ozamani9gh
    CollaboratorAuthor

    @Mihai-QuickSilverDev reach out to Joshua and get the exact delay he wants, we suggest 10 or 15 minutes.

  4. Mihai-QuickSilverDev commented on Jun 28, 2024

    @Mihai-QuickSilverDev
    Collaborator

    Message sent to Joshua on June 28, 2024, 9 AM:

    Hi Joshua,

    Regarding this topic below, could you please let us know what would be a desirable duration for this delay? We could do 5, 10, 15 minutes, whatever would best fit the Name Examiners needs.

    Update from Joshua:
    • The removal of the output delay creates confusion for clients if an examiner were to catch a mistake and go back to fix it (generates multiple different results emails to clients, who then call or email in confusion which creates a cascade effect). Even a few minutes delay for output generation would help reduce this.

    Thank you,

  5. ozamani9gh commented on Jul 2, 2024

    @ozamani9gh
    CollaboratorAuthor

    Joshua stated 5 minutes is sufficient

  6. EPortman commented on Jul 4, 2024

    @EPortman
    Contributor

    Clients are emailed about their NRs in the following scenarios:

    1. When NR goes to APPROVE, CONDITIONAL or REJECTED
    2. When a payment update is made to REAPPLY or UPGRADE
    3. When a NR is refunded
    4. When a NR updates to CONSENT_RECEIVED
  7. EPortman commented on Jul 15, 2024

    @EPortman
    Contributor

    Async Operations with Flask Framework: https://flask.palletsprojects.com/en/3.0.x/async-await/#
    Async Operations with Gunicorn Server: https://docs.gunicorn.org/en/latest/design.html

    NameX API uses the Flask Framework running on a Gunicorn WSGI Server

    Why its not possible to us async/await on each email for 5 minutes
    Flask on its own is single-threaded (blocking) and does not natively support asynchronous operations across requests - only within them (can await a database operation, but cant schedule it - it will be cancelled once the request ends since flask handles 1 request at a time).

    • In production, a Gunicorn WSGI Server allows Flask to be concurrent (multiple requests at once)
    • Gunicorn employs several instances of the app called workers that run independently of each other
      • These workers can be sync (default) or gevent (green threads)
      • sync is the default worker type and the one that is used in our API
        • In this scenario, each NR needs to wait for 5 minutes so it needs to schedule an async task that outlives itself. In the Flask Link provided, under Background Tasks, it states that this is not possible on a WSGI server (1 request-response cycle per worker).

    To get the desired behavior (non-blocking waiting for 5 minutes for each email)

    1. Change the Gunicorn worker to be of type gevent to allow for async operations

      • This would change the way the Server works - it would support asynchronous operations (have an event loop, gEVENT)
      • The section When to use Quart instead in the Flask docs seem to imply this is more of a work-around.
    2. Spawn a separate thread for async operations

      • Spawn a separate thread with an event loop to handle all asynchronous tasks.
        • This approach adds a layer of complexity - API would become multithreaded with a main sync thread and another async thread where emails tasks are scheduled.
    3. Use Celery to get the async effect

      • Celery is a separate process that acts as a task queue.
      • API would send asynchronous tasks to this separate process.
      • The recommended way, involves adding another dependency and separate running process to the api
        .
    4. Use a Cron Job to get the async effect

      • Any time an email needs to be sent, it is written to disk in the database.
      • A cron job then reads from this database every 5 minutes (or even at the end of every day) and determines what emails need to be sent.
  8. EPortman commented on Jul 15, 2024

    @EPortman
    Contributor

    A good resource for this topic: https://testdriven.io/blog/flask-async/
    image.png

  9. self-assigned this
    on May 8, 2025
  10. EPortman commented on Jun 12, 2025

    @EPortman
    Contributor

    Quality Assurance

    Note: The code changes only apply to emails that are approved, conditionally approved, or rejected. But it is probably good to confirm the others are working as expected too (before-expiry, expiry, consent-required, paid, refund, renewal, & upgrade = 7 regression tests -> 1 of each is sufficient here as none of the code changes touched the email decision tree or those types of emails in general).

    Note: An email event in this context refers to approving, conditionally approving, or rejecting an NR (triggering the email scheduling).

    Note: The debounce window is 5 minutes from the most recent email event. Therefore it is possible to keep on delaying the email for the nr if the nr is reset and another email event occurs within 5 minutes (approve, reject, approve, reject...)

    Note: If an email event occurs and then reset without another email event occuring within 5 minutes, the email will still send. Supporting this is outside the scope of this ticket and will be completed in 28997.

    Note: Testing this ticket would be a lot easier with read access to namex-emailer-pending-send-queue-test and the namex-emailer-test logs.

    For approved, conditionally approved, and rejected:

    Scenario 1 (no change)

    Email event occurs and the NR is not reset within 5 minutes
    → Should only receive one email (not within 5 minute debounce window)

    Scenario 2 (no change)

    Email event occurs and the NR is reset with < 5 min between email event and reset
    → Should only receive one email (not ideal as within debounce window, see note)

    Scenario 3 (no change)

    Email event occurs and the NR is reset with > 5 min between email event and reset
    → Should only receive one email (not within 5 minute debounce window)

    Scenario 4 (changed behavior)

    Email event occurs → NR is reset → another email event happens < 5 min later
    → Should only receive the latest email

    Scenario 5 (changed behavior)

    Multiple email events come in (all within 5 minutes of the one before it)
    → Should only receive the latest email

    Scenario 6 (no change)

    Two or more email events occur, but > 5 minutes have passed between them
    → Should receive both (or all) emails, since they are outside the debounce window

  11. ahamood1 commented on Jun 24, 2025

    @ahamood1
    • Cloud Task has been implemented for -test and -prod with the following settings:
      - instance = "namex-emailer-pending-send-queue-test"
      - max_dispatches_per_second = 5
      - max_concurrent_dispatches = 100
      - max_attempts = 3
      - max_retry_duration = "60s"
      - min_backoff = "5s"
      - max_backoff = "5s"
      - max_doublings = 0
      - sampling_ratio = 1.0
    • sa-api permissions have been update to include:
      - "roles/cloudtasks.enqueuer"
      - "roles/cloudtasks.viewer"
      - "roles/cloudtasks.taskDeleter"
  12. moraesfm commented on Jun 26, 2025

    @moraesfm

    FM-Approved1 - NR - OK
    FM-Conditionally-Approved1 DR = NR 6236665 - OK
    FM-Rejected1 - NR 2376837 - OK
    FM-PAID1 = NR 7481454
    FM-UPGRADE1 - OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

High PriorityNames Team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions