-
Notifications
You must be signed in to change notification settings - Fork 180
Fix performance issue: Cache background sync job queries and skip on … #3823
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
Open
devbodaghe
wants to merge
4
commits into
main
Choose a base branch
from
fix/background-sync-performance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d208274
Fix performance issue: Cache background sync job queries and skip on …
2ce48a5
Fix tests: get_jobs() has frontend guard only, not caching
2121542
Fix performance issue: Cache background sync job queries and skip on …
0c295c0
Fix performance issue: Cache background sync job queries and skip on …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,12 @@ abstract class BackgroundJobHandler extends AsyncRequest { | |
| /** @var string debug message, used by the system status tool */ | ||
| protected $debug_message; | ||
|
|
||
| /** @var string transient key for caching queue empty status */ | ||
| protected $queue_empty_cache_key; | ||
|
|
||
| /** @var string transient key for caching sync in progress status */ | ||
| protected $sync_in_progress_cache_key; | ||
|
|
||
|
|
||
| /** | ||
| * Initiate new background job handler | ||
|
|
@@ -66,8 +72,10 @@ abstract class BackgroundJobHandler extends AsyncRequest { | |
| */ | ||
| public function __construct() { | ||
| parent::__construct(); | ||
| $this->cron_hook_identifier = $this->identifier . '_cron'; | ||
| $this->cron_interval_identifier = $this->identifier . '_cron_interval'; | ||
| $this->cron_hook_identifier = $this->identifier . '_cron'; | ||
| $this->cron_interval_identifier = $this->identifier . '_cron_interval'; | ||
| $this->queue_empty_cache_key = $this->identifier . '_queue_empty'; | ||
| $this->sync_in_progress_cache_key = $this->identifier . '_sync_in_progress'; | ||
| $this->add_hooks(); | ||
| } | ||
|
|
||
|
|
@@ -165,10 +173,26 @@ public function maybe_handle() { | |
| /** | ||
| * Check whether job queue is empty or not | ||
| * | ||
| * Uses transient caching to avoid expensive database queries on every request. | ||
| * The cache is invalidated when jobs are created, updated, or completed. | ||
| * | ||
| * @since 4.4.0 | ||
| * @return bool True if queue is empty, false otherwise | ||
| */ | ||
| protected function is_queue_empty() { | ||
| // Skip expensive query on frontend - only needed in admin/ajax/cron/process contexts. | ||
| // The method runs if ANY of these is true: is_admin(), wp_doing_ajax(), wp_doing_cron(), or is_process_request(). | ||
| // On pure frontend requests (none of those conditions), we return true to skip the expensive query. | ||
| if ( ! is_admin() && ! wp_doing_ajax() && ! wp_doing_cron() && ! $this->is_process_request() ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the is_process_request method? I cannot find the implementation of it |
||
| return true; // Assume empty on frontend to avoid query | ||
| } | ||
|
|
||
| // Check cache first | ||
| $cached = get_transient( $this->queue_empty_cache_key ); | ||
| if ( false !== $cached ) { | ||
| return 'empty' === $cached; | ||
| } | ||
|
|
||
| global $wpdb; | ||
|
|
||
| $key = $this->identifier . '_job_%'; | ||
|
|
@@ -189,7 +213,41 @@ protected function is_queue_empty() { | |
| ) | ||
| ); | ||
|
|
||
| return intval( $count ) === 0; | ||
| $is_empty = intval( $count ) === 0; | ||
|
|
||
| // Cache the result for 1 hour as a safety net - it will be invalidated when job status changes | ||
| set_transient( $this->queue_empty_cache_key, $is_empty ? 'empty' : 'not_empty', HOUR_IN_SECONDS ); | ||
|
|
||
| return $is_empty; | ||
| } | ||
|
|
||
| /** | ||
| * Invalidate the queue empty cache. | ||
| * | ||
| * Should be called when job status changes (create, update, complete, fail, delete). | ||
| * | ||
| * @since 3.5.0 | ||
| */ | ||
| protected function invalidate_queue_cache() { | ||
| delete_transient( $this->queue_empty_cache_key ); | ||
| delete_transient( $this->sync_in_progress_cache_key ); | ||
| // Also clear the is_sync_in_progress cache used by Sync class | ||
| delete_transient( 'wc_facebook_sync_in_progress' ); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Check whether the current request is a background process request. | ||
| * | ||
| * Checks if the request action matches this handler's identifier, | ||
| * indicating it's an actual background processing request. | ||
| * | ||
| * @since 3.5.0 | ||
| * @return bool True if this is a background process request, false otherwise | ||
| */ | ||
| protected function is_process_request() { | ||
| // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified in maybe_handle() | ||
| return isset( $_REQUEST['action'] ) && $_REQUEST['action'] === $this->identifier; | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -396,6 +454,9 @@ public function create_job( $attrs ) { | |
| ] | ||
| ); | ||
|
|
||
| // Invalidate cache since a new job was created | ||
| $this->invalidate_queue_cache(); | ||
|
|
||
| $job = new \stdClass(); | ||
|
|
||
| foreach ( $attrs as $key => $value ) { | ||
|
|
@@ -480,6 +541,9 @@ public function get_job( $id = null ) { | |
| /** | ||
| * Gets jobs. | ||
| * | ||
| * Uses transient caching for common queries (like checking for processing jobs) | ||
| * to avoid expensive database queries on every request. | ||
| * | ||
| * @since 4.4.2 | ||
| * | ||
| * @param array $args { | ||
|
|
@@ -488,6 +552,7 @@ public function get_job( $id = null ) { | |
| * @type string|array $status Job status(es) to include | ||
| * @type string $order ASC or DESC. Defaults to DESC | ||
| * @type string $orderby Field to order by. Defaults to option_id | ||
| * @type bool $use_cache Whether to use caching. Defaults to true. | ||
| * } | ||
| * @return \stdClass[]|object[]|null Found jobs or null if none found | ||
| */ | ||
|
|
@@ -502,6 +567,13 @@ public function get_jobs( $args = [] ) { | |
| ] | ||
| ); | ||
|
|
||
| // Skip expensive query on frontend - only needed in admin/ajax/cron contexts. | ||
| // The method runs if ANY of these is true: is_admin(), wp_doing_ajax(), or wp_doing_cron(). | ||
| // On pure frontend requests (none of those conditions), we return null to skip the expensive query. | ||
| if ( ! is_admin() && ! wp_doing_ajax() && ! wp_doing_cron() ) { | ||
| return null; // Return no jobs on frontend to avoid query | ||
| } | ||
|
|
||
| $replacements = [ $this->identifier . '_job_%' ]; | ||
| $status_query = ''; | ||
|
|
||
|
|
@@ -615,7 +687,8 @@ public function process_job( $job, $items_per_batch = null ) { | |
| $job->status = 'processing'; | ||
| $job->started_processing_at = current_time( 'mysql' ); | ||
|
|
||
| $job = $this->update_job( $job ); | ||
| // Invalidate cache when status changes to processing | ||
| $job = $this->update_job( $job, true ); | ||
| } | ||
|
|
||
| $data_key = $this->data_key; | ||
|
|
@@ -684,9 +757,11 @@ public function process_job( $job, $items_per_batch = null ) { | |
| * @since 4.4.0 | ||
| * | ||
| * @param \stdClass|object|string $job Job instance or ID | ||
| * @param bool $invalidate_cache Whether to invalidate the queue cache. Defaults to false | ||
| * to avoid cache thrashing during progress updates. | ||
| * @return \stdClass|object|false on failure | ||
| */ | ||
| public function update_job( $job ) { | ||
| public function update_job( $job, $invalidate_cache = false ) { | ||
| if ( is_string( $job ) ) { | ||
| $job = $this->get_job( $job ); | ||
| } | ||
|
|
@@ -695,6 +770,13 @@ public function update_job( $job ) { | |
| } | ||
| $job->updated_at = current_time( 'mysql' ); | ||
| $this->update_job_option( $job ); | ||
|
|
||
| // Only invalidate cache when explicitly requested (e.g., status changes) | ||
| // to avoid cache thrashing during frequent progress updates | ||
| if ( $invalidate_cache ) { | ||
| $this->invalidate_queue_cache(); | ||
| } | ||
|
|
||
| /** | ||
| * Runs when a job is updated. | ||
| * | ||
|
|
@@ -725,6 +807,10 @@ public function complete_job( $job ) { | |
| $job->status = 'completed'; | ||
| $job->completed_at = current_time( 'mysql' ); | ||
| $this->update_job_option( $job ); | ||
|
|
||
| // Invalidate cache since job status changed | ||
| $this->invalidate_queue_cache(); | ||
|
|
||
| /** | ||
| * Runs when a job is completed. | ||
| * | ||
|
|
@@ -763,6 +849,10 @@ public function fail_job( $job, $reason = '' ) { | |
| $job->failure_reason = $reason; | ||
| } | ||
| $this->update_job_option( $job ); | ||
|
|
||
| // Invalidate cache since job status changed | ||
| $this->invalidate_queue_cache(); | ||
|
|
||
| /** | ||
| * Runs when a job is failed. | ||
| * | ||
|
|
@@ -792,6 +882,10 @@ public function delete_job( $job ) { | |
| return false; | ||
| } | ||
| $wpdb->delete( $wpdb->options, [ 'option_name' => "{$this->identifier}_job_{$job->id}" ] ); | ||
|
|
||
| // Invalidate cache since a job was deleted | ||
| $this->invalidate_queue_cache(); | ||
|
|
||
| /** | ||
| * Runs after a job is deleted. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the only case where the method should run is:
if it's an admin user, making an ajax call, through cron?