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

Merged
merged 4 commits into from
Jan 8, 2025

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
Copy link
Contributor

@dkoo dkoo left a comment

Choose a reason for hiding this comment

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

I tested the following ways to trigger syncs and all are working except for the CLI command:

  • Reader action (purchasing a new subscription, renewing an existing one) ✔️
  • Admin action (manually renewing a subscription or changing its status) ✔️
  • Admin bulk sync in Users table ✔️
  • CLI command wp newspack esp sync --user-ids=127 ❌ — in this case the CLI command fails to read the associative args whether in dry-run or live mode. The command works as expected in trunk.

Once we fix things for the CLI command, I think this will be a good performance improvement!

Copy link
Contributor

@dkoo dkoo left a comment

Choose a reason for hiding this comment

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

Working after the merge from trunk!

@github-actions github-actions bot added [Status] Approved The pull request has been reviewed and is ready to merge and removed [Status] Needs Review The issue or pull request needs to be reviewed labels Jan 8, 2025
@miguelpeixe
Copy link
Member Author

Thank you for the review, @dkoo!

@miguelpeixe miguelpeixe merged commit 510a1a0 into trunk Jan 8, 2025
8 checks passed
@miguelpeixe miguelpeixe deleted the feat/data-events-dispatch-shutdown branch January 8, 2025 22:27
Copy link

github-actions bot commented Jan 8, 2025

Hey @miguelpeixe, good job getting this PR merged! 🎉

Now, the needs-changelog label has been added to it.

Please check if this PR needs to be included in the "Upcoming Changes" and "Release Notes" doc. If it doesn't, simply remove the label.

If it does, please add an entry to our shared document, with screenshots and testing instructions if applicable, then remove the label.

Thank you! ❤️

matticbot pushed a commit that referenced this pull request Jan 10, 2025
## [5.10.7-alpha.1](v5.10.6...v5.10.7-alpha.1) (2025-01-10)

### Bug Fixes

* **cli:** verify-reader CLI command ([#3660](#3660)) ([c639af7](c639af7))
* **recaptcha:** replace alerts with generic errors ([#3627](#3627)) ([44ef2d2](44ef2d2))

### Performance Improvements

* **data-events:** queue dispatches to execute on shutdown ([#3616](#3616)) ([510a1a0](510a1a0))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 5.10.7-alpha.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Jan 14, 2025
# [5.12.0-alpha.1](v5.11.1...v5.12.0-alpha.1) (2025-01-14)

### Bug Fixes

* **cli:** verify-reader CLI command ([#3660](#3660)) ([c639af7](c639af7))
* **recaptcha:** replace alerts with generic errors ([#3627](#3627)) ([44ef2d2](44ef2d2))
* remove newspack_corrections_ids meta ([#3675](#3675)) ([dad258b](dad258b))

### Features

* **corrections:** add corrections and clarifications behind feature flag ([#3638](#3638)) ([ea745cf](ea745cf))

### Performance Improvements

* **data-events:** queue dispatches to execute on shutdown ([#3616](#3616)) ([510a1a0](510a1a0))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 5.12.0-alpha.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Jan 20, 2025
# [5.12.0](v5.11.3...v5.12.0) (2025-01-20)

### Bug Fixes

* **cli:** verify-reader CLI command ([#3660](#3660)) ([c639af7](c639af7))
* **recaptcha:** replace alerts with generic errors ([#3627](#3627)) ([44ef2d2](44ef2d2))
* remove newspack_corrections_ids meta ([#3675](#3675)) ([dad258b](dad258b))
* **wcs:** migrate-expired-subscriptions handle manual subscriptions ([#3663](#3663)) ([e0f32e8](e0f32e8))

### Features

* **corrections:** add corrections and clarifications behind feature flag ([#3638](#3638)) ([ea745cf](ea745cf))

### Performance Improvements

* **data-events:** queue dispatches to execute on shutdown ([#3616](#3616)) ([510a1a0](510a1a0))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 5.12.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released on @alpha released [Status] Approved The pull request has been reviewed and is ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants