-
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
base: main
Are you sure you want to change the base?
Conversation
📦 Latest Plugin BuildBuilt at: 2026-01-26T17:37:13.669Z Download: Click here to download the plugin To download: Click the link above → Scroll to bottom → Download "facebook-for-woocommerce" artifact |
| $is_empty = intval( $count ) === 0; | ||
|
|
||
| // Cache the result indefinitely - it will be invalidated when job status changes | ||
| set_transient( $this->queue_empty_cache_key, $is_empty ? 'empty' : 'not_empty', 0 ); |
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.
I'd put something like 1h for the expiration, just in case.
| */ | ||
| protected function is_queue_empty() { | ||
| // Skip expensive query on frontend - only needed in admin/ajax/cron contexts | ||
| if ( ! is_admin() && ! wp_doing_ajax() && ! wp_doing_cron() && ! $this->is_process_request() ) { |
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?
| */ | ||
| protected function is_queue_empty() { | ||
| // Skip expensive query on frontend - only needed in admin/ajax/cron contexts | ||
| if ( ! is_admin() && ! wp_doing_ajax() && ! wp_doing_cron() && ! $this->is_process_request() ) { |
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.
What is the is_process_request method? I cannot find the implementation of it
| * Tests focus on the is_sync_in_progress() method and its behavior | ||
| * with caching and frontend guards. | ||
| */ | ||
| class SyncTest extends AbstractWPUnitTestWithOptionIsolationAndSafeFiltering { |
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.
Could you also test the happy-path ( when a sync is in progress ), using an E2E test?
…frontend - Add transient-based caching to is_queue_empty() and get_jobs() methods - Add frontend guards to prevent expensive queries on frontend requests - Cache persists until explicitly invalidated (no time-based expiry) - Invalidate cache when jobs are created, completed, failed, or deleted - Update DebugTools to clear cache when cleaning up old sync options - Add comprehensive unit tests for caching and frontend guard logic This fixes the slow SQL query (SELECT ... LIKE '%status:processing%') that was running on every frontend request, causing 1.5+ second delays.
The caching for sync status is implemented at the higher level in is_sync_in_progress() in Sync.php, not in get_jobs() directly. Updated tests to reflect the actual implementation.
f4dfbd1 to
0c295c0
Compare
|
@devbodaghe has imported this pull request. If you are a Meta employee, you can view this in D91487986. |
Description
This PR fixes a severe performance issue where an expensive SQL query was running on every frontend page load, causing 1.5+ second delays and timeout errors.
The Problem:
The query
SELECT option_value FROM wp_options WHERE option_name LIKE 'wc_facebook_background_product_sync_job_%' AND (option_value LIKE '%"status":"processing"%')was executing on every request because:LIKEclauses cannot use indexes efficiently, causing full table scansThe Solution:
is_queue_empty()andget_jobs()now return early on frontend requests (when not in admin, AJAX, or cron context)Impact: This single query accounted for ~25% of total page load time on affected stores.
Type of change
Checklist
Changelog entry
Fix: Resolved performance issue where background sync job queries caused slow page loads on frontend requests.
Test Plan
Automated Tests
Run the new unit tests:
./vendor/bin/phpunit --filter BackgroundJobHandlerTest
./vendor/bin/phpunit --filter SyncTest
./vendor/bin/phpunit --filter DebugToolsTestResults: 46 tests, 98 assertions - all passing
Manual Testing
Frontend Performance Test:
wc_facebook_background_product_sync_job_%query no longer appearsAdmin Functionality Test:
Cache Invalidation Test:
get_transient('wc_facebook_background_product_sync_queue_empty')Debug Tools Test:
Screenshots
Before
Query Monitor showing the expensive query on every frontend request:
SELECT option_value FROM wp_options WHERE option_name LIKE 'wc_facebook_background_product_sync_job_%' AND (option_value LIKE '%"status":"processing"%')After