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

fix: avoid creating merge fields if we can't get merge fields #1729

Open
wants to merge 7 commits into
base: release
Choose a base branch
from

Conversation

dkoo
Copy link
Contributor

@dkoo dkoo commented Dec 18, 2024

All Submissions:

Changes proposed in this Pull Request:

When preparing a contact's data sync, we compare the contact's meta fields with the existing merge fields in the audience so that we can use the fields that exist and create the ones that don't yet exist. However, if we can't get info about the existing merge fields via the Mailchimp API for any reason, we should stop the sync to avoid potentially creating duplicate merge fields.

This PR also uses the validate method to catch any potential error-like responses from the Mailchimp API.

Coupled with Automattic/newspack-plugin#3639, this PR will also schedule a retry sync after five minutes if we fail to fetch merge fields, up to a maximum of five times.

Addresses: 1200550061930446-as-1208959720314117

How to test the changes in this Pull Request:

  1. Smoke test syncing new and existing contacts with Mailchimp and confirm that contact metadata gets synced correctly to existing merge fields, and that merge fields that don't yet exist get created.

NOTE: leaving the below in place for the record, but it's no longer part of this PR.

Click to see additional testing steps (no longer part of this PR)
  1. Temporarily add a line $response = []; here to simulate a non-error response from the Mailchimp API that nonetheless fails to return merge field data.
  2. Trigger a contact sync to Mailchimp via any means.
  3. Confirm that the sync now fails with an error, without creating any new merge fields.
  4. Using WP CLI, run wp cron event list and confirm there's a non-recurring newspack_scheduled_esp_sync job scheduled for 5 minutes in the future.
  5. Wait 5 minutes or run wp cron event run newspack_scheduled_esp_sync to trigger the job immediately. Repeat this 5 times.
  6. After 5 failed syncs, confirm that a Failed to sync contact <email address> after 5 retries is logged, and that there's no more newspack_scheduled_esp_sync job scheduled.
  7. Remove the temporary error line added in step 2 and resync the contact.
  8. Using WP CLI, run Newspack\Reader_Activation\Sync::schedule_sync( $user_id, 'Testing scheduled sync', 30 ); and confirm that the scheduled sync happens successfully after 30 seconds.

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?

@dkoo dkoo self-assigned this Dec 18, 2024
@dkoo dkoo requested a review from a team as a code owner December 18, 2024 16:47
Copy link

codecov bot commented Dec 18, 2024

Codecov Report

Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Project coverage is 23.27%. Comparing base (ad092ca) to head (91c4834).
Report is 15 commits behind head on release.

Files with missing lines Patch % Lines
...mailchimp/class-newspack-newsletters-mailchimp.php 92.30% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             release    #1729      +/-   ##
=============================================
+ Coverage      23.07%   23.27%   +0.20%     
+ Complexity      2713     2709       -4     
=============================================
  Files             49       49              
  Lines          10761    10748      -13     
=============================================
+ Hits            2483     2502      +19     
+ Misses          8278     8246      -32     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dkoo dkoo marked this pull request as draft December 18, 2024 21:50
@dkoo dkoo changed the title fix: get merge fields from cache instead of API fix: avoid creating merge fields if we can't get merge fields Dec 18, 2024
@dkoo dkoo marked this pull request as ready for review December 19, 2024 00:27
@leogermani
Copy link
Contributor

This PR also uses the validate method to catch any potential error-like responses from the Mailchimp API.

Good catch! That's why this error was never being logged!

I couldn't get it all to work, here are a few things I saw:

  • When the error is triggered, it doesn't stop the sync from happening, which results in the contact missing the merge fields
  • When I run wp cron event run newspack_scheduled_esp_sync, it succeeds, but I don't see the actual sync going through
  • Another sync is not scheduled (I'm running the two branches as expected)

@dkoo
Copy link
Contributor Author

dkoo commented Dec 20, 2024

@leogermani I think 345229b should stop the sync by throwing an exception if we encounter a merge fields error. Note that since we're using the newspack_log action, the errors won't be logged directly to debug.log, but to the Mailchimp log file in your server's /tmp directory.

@dkoo
Copy link
Contributor Author

dkoo commented Dec 20, 2024

When I run wp cron event run newspack_scheduled_esp_sync, it succeeds, but I don't see the actual sync going through

This might be the case if you still have the temp error being triggered by the $response = []; line. If it's still there then the scheduled syncs will continue failing with an error (logged in the local /tmp logfile, not debug.log). On my site once I remove that line the next scheduled sync will succeed.

@leogermani
Copy link
Contributor

When I run wp cron event run newspack_scheduled_esp_sync, it succeeds, but I don't see the actual sync going through

This might be the case if you still have the temp error being triggered by the $response = []; line. If it's still there then the scheduled syncs will continue failing with an error (logged in the local /tmp logfile, not debug.log). On my site once I remove that line the next scheduled sync will succeed.

Right, it's expected to fail, but also to schedule a new retry until it reaches 5 attempts. It didn't schedule the second retry

)
);
}
$result = $this->validate( $mc->put( "lists/$list_id/members/$member_hash", $update_payload ) );
Copy link
Contributor Author

@dkoo dkoo Dec 20, 2024

Choose a reason for hiding this comment

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

@leogermani I think this change is still safe because it should result in the same behavior and error code—the removed code was mainly duplicating some of the checks in the validate() method. Happy to revert this too if you prefer.

@dkoo
Copy link
Contributor Author

dkoo commented Dec 20, 2024

@leogermani I've scaled back the changes in this PR to include only the additional validation and error logging, so this should be ready for another review.

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

Successfully merging this pull request may close these issues.

2 participants