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

Clear cache after a scheduler update is queued #37399

Merged
merged 22 commits into from
May 21, 2024

Conversation

zaerl
Copy link
Contributor

@zaerl zaerl commented May 15, 2024

Fixes: Automattic/wp-calypso#90627
Context: p1715613932876249-slack-C01A60HCGUA

Proposed changes:

  • Force a cache clear the first ten seconds after a schedule creation, so that scheduled events at the same second will obtain a fresh cache and not previous value
  • jetpack_scheduled_update_updated is run only on successful scheduled event creation

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

Testing instructions:

  • Add this branch to Jetpack Beta
  • Quickly add/create/modify a scheduled update. Better when the clock is near second zero (08:00:57 -> 08:01:03, etc.)
  • Ensure everything is ok

Copy link
Contributor

github-actions bot commented May 15, 2024

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team Review" label and ask someone from your team review the code. Once reviewed, it can then be merged.
If you need an extra review from someone familiar with the codebase, you can update the labels from "[Status] Needs Team Review" to "[Status] Needs Review", and in that case Jetpack Approvers will do a final review of your PR.


Mu Wpcom plugin:

  • Next scheduled release: June 4, 2024.
  • Scheduled code freeze: May 27, 2024.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

Copy link
Contributor

github-actions bot commented May 15, 2024

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WordPress.com Simple site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin, and enable the add/scheduled-updates-cache-clear branch.

    • For jetpack-mu-wpcom changes, also add define( 'JETPACK_MU_WPCOM_LOAD_VIA_BETA_PLUGIN', true ); to your wp-config.php file.
  • To test on Simple, run the following command on your sandbox:

    bin/jetpack-downloader test jetpack-mu-wpcom-plugin add/scheduled-updates-cache-clear
    

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@zaerl zaerl changed the title Add/scheduled updates cache clear Clear cache after a scheduler update is queued May 15, 2024
@zaerl zaerl force-pushed the add/scheduled-updates-cache-clear branch 2 times, most recently from 037a89b to 8ab11a1 Compare May 17, 2024 11:23
@zaerl zaerl marked this pull request as ready for review May 17, 2024 14:42
@zaerl zaerl requested a review from a team May 17, 2024 14:49
@zaerl zaerl requested a review from obenland May 17, 2024 16:29
@obenland
Copy link
Member

@ouikhuan @zaerl I don't know if Scheduled Updates is the best place for this, but after reading through the thread (p1715925157297969-slack-C01A60HCGUA), I do wonder if clearing cron option cache on pre_schedule_event for all schedules would be the most straight forward way of solving this? We should also see if we can limit it to the cron entry only, and if not, know why not.

Based on your testing it looks like updating cron array with stale data is not just a problem with ActionScheduler but with any event that gets scheduled in close timely proximity to one another?

@ouikhuan
Copy link
Contributor

@ouikhuan @zaerl I don't know if Scheduled Updates is the best place for this, but after reading through the thread (p1715925157297969-slack-C01A60HCGUA), I do wonder if clearing cron option cache on pre_schedule_event for all schedules would be the most straight forward way of solving this?

Yeah, I agree. This seems to be a more general problem that happens whenever two schedule events run at the same second, and might be better to place this fix somewhere else.

We should also see if we can limit it to the cron entry only, and if not, know why not.

I think we could try to limit it to cron only, explain here.

Based on your testing it looks like updating cron array with stale data is not just a problem with ActionScheduler but with any event that gets scheduled in close timely proximity to one another?

That's correct. It can also be some other events like jetpack_sync_full_cron too.

Copy link
Contributor

@ouikhuan ouikhuan left a comment

Choose a reason for hiding this comment

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

@zaerl , I think we'll also need to add the "lock" when we try to delete a schedule, since it might also happens when another schedule event is triggered, what do you think?
Here's a scenario:

  1. wp_unschedule_event runs -> the cron get removed.
  2. At same second another schedule event triggered ( can be jetpack_sync_full_cron )
  3. The cron value gets overwritten again.

@zaerl
Copy link
Contributor Author

zaerl commented May 20, 2024

@obenland @ouikhuan

  1. Removed the create_scheduled_update/delete_scheduled_update functions. Not needed anymore.
  2. Changed the cache cleaning method.
  3. delete_item now deletes the cache only once time, so it's not deleted multiple times during the same call.
  4. The pre_schedule_event_clear_cron_cache now checks if the event differs from the scheduled updates event.
  5. Multiple unit tests were added that counted how many times a transient was added.
create_item()
    wp_schedule_event > pre_schedule_event_clear_cron_cache
update_item()
    delete_item()
        wp_unschedule_event
    create_item()
        wp_schedule_event > pre_schedule_event_clear_cron_cache
delete_item()
    wp_unschedule_event > pre_schedule_event_clear_cron_cache

@zaerl zaerl requested review from obenland and ouikhuan May 20, 2024 12:18
@ouikhuan
Copy link
Contributor

@obenland where do you think it's the best for us to place this fix? Should we put it on the Scheduled Updates package as an initial and run for a while to make sure we don't bring in any side effects and move it to a more general place later? Or should we move it to a more general place directly? If so, where do you think is better? Jetpack plugin or wpcomsh?

@obenland
Copy link
Member

where do you think it's the best for us to place this fix?

Yeah, that's a good question.

With the transient it should probably live in scheduled updates. If we removed that transient, it might make sense to put in wpcomsh with review from others. Ultimately it sounds like a consideration for Core?

@ouikhuan
Copy link
Contributor

With the transient it should probably live in scheduled updates. If we removed that transient, it might make sense to put in wpcomsh with review from others. Ultimately it sounds like a consideration for Core?

Yes, I think we'll need to have transient so it only clears the cache when it has to. And since this is more like a general issue for the schedule event system I think it'll be nice to consider adding this to Core eventually.

Copy link
Contributor

@ouikhuan ouikhuan left a comment

Choose a reason for hiding this comment

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

Tested several times with other schedule events running at the same second and it looks great. Thanks for handling all the feedback!

@zaerl zaerl removed the request for review from obenland May 21, 2024 09:47
@zaerl zaerl enabled auto-merge (squash) May 21, 2024 09:47
@zaerl zaerl merged commit 4aab87f into trunk May 21, 2024
54 checks passed
@zaerl zaerl deleted the add/scheduled-updates-cache-clear branch May 21, 2024 09:48
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.

Test random Scheduled Updates bug in production site
3 participants