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

perf(data-events): queue dispatches to execute on shutdown #3616

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

miguelpeixe
Copy link
Member

@miguelpeixe miguelpeixe commented Dec 11, 2024

All Submissions:

Changes proposed in this Pull Request:

p1733922470397489-slack-C055SRT7WN4

Depending on the setup, a site can execute 5 dispatches when a reader registers:

  • membership_status_active
  • membership_saved
  • reader_logged_in
  • reader_registered
  • network_user_updated

The list grows if the registration happens during a subscription purchase.

Other actions on the site can also dispatch multiple events.

Each dispatch runs a wp_remote_post(). Even though it's a non-blocking execution with a timeout of 0.01, calling this function for every dispatch is unnecessary and costs performance.

This PR proposes queueing the dispatches to be executed in a single wp_remote_post() called on the shutdown hook. This hook runs right before PHP shuts down execution, including on fatal errors.

How to test the changes in this Pull Request:

  1. Make sure you have RAS configured and NEWSPACK_LOG_LEVEL set to at least 2 on wp-config.php
  2. Add a webhook endpoint (https://webhook.site/ is your friend) assigned to the actions listed above
  3. On a fresh session, register a new reader
  4. Confirm you get the following log: (actions may vary according to your setup, but you should get at least reader_registered and reader_logged_in in the same run
Dispatching actions: "membership_status_active, membership_saved, reader_logged_in, reader_registered, network_user_updated".
  1. Confirm you get the logs for handler execution as well:
[Newspack\Data_Events::handle]: Executing global action handlers for "membership_status_active".
[Newspack\Data_Events::handle]: Executing action handlers for "membership_status_active".
[Newspack\Data_Events::handle]: Executing global action handlers for "membership_saved".
[Newspack\Data_Events::handle]: Executing action handlers for "membership_saved".
[Newspack\Data_Events::handle]: Executing global action handlers for "reader_logged_in".
[Newspack\Data_Events::handle]: Executing action handlers for "reader_logged_in".
[Newspack\Data_Events::handle]: Executing global action handlers for "reader_registered".
[Newspack\Data_Events::handle]: Executing action handlers for "reader_registered".
[Newspack\Data_Events::handle]: Executing global action handlers for "network_user_updated".
[Newspack\Data_Events::handle]: Executing action handlers for "network_user_updated".
  1. Wait until the webhook requests are processed and confirm every request reached your endpoint with the expected data

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@miguelpeixe miguelpeixe requested a review from a team as a code owner December 11, 2024 13:05
@miguelpeixe miguelpeixe self-assigned this Dec 11, 2024
@miguelpeixe miguelpeixe added the [Status] Needs Review The issue or pull request needs to be reviewed label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Status] Needs Review The issue or pull request needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant