fix: run product sync in background batches to avoid 502 on large catalogs
Manual and scheduled sync previously processed every WooCommerce product inline in a single PHP request/AJAX call, with several sequential BC API calls per product. At real catalog sizes this exceeds PHP's max_execution_time and the proxy's read timeout, surfacing as a 502 Bad Gateway on "Sync Now". Adds WBC_Batch_Sync, which queues the catalog in chunks of 25 via Action Scheduler (bundled with WooCommerce) and processes them asynchronously. The manual sync AJAX call now returns immediately after queuing, and the admin UI polls for progress instead of waiting on one long request. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -42,17 +42,20 @@ class WBC_Cron {
|
||||
|
||||
/**
|
||||
* Run scheduled product sync
|
||||
*
|
||||
* Queues background batches via WBC_Batch_Sync rather than running the
|
||||
* whole catalog inline - WP-Cron events run under the same PHP time/memory
|
||||
* limits as any other request and would otherwise time out at scale.
|
||||
*/
|
||||
public function run_scheduled_sync() {
|
||||
WBC_Logger::info( 'Cron', 'Starting scheduled product sync' );
|
||||
|
||||
$product_sync = new WBC_Product_Sync();
|
||||
$result = $product_sync->run_sync();
|
||||
$result = WBC_Batch_Sync::queue_full_sync();
|
||||
|
||||
if ( isset( $result['success'] ) && $result['success'] ) {
|
||||
WBC_Logger::info( 'Cron', 'Scheduled product sync completed', $result );
|
||||
WBC_Logger::info( 'Cron', 'Scheduled product sync queued', $result );
|
||||
} else {
|
||||
WBC_Logger::error( 'Cron', 'Scheduled product sync failed', $result );
|
||||
WBC_Logger::error( 'Cron', 'Scheduled product sync failed to queue', $result );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,23 +140,6 @@ class WBC_Cron {
|
||||
update_option( 'wbc_last_sync_time', current_time( 'mysql' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Run sync now (manual trigger)
|
||||
*
|
||||
* @return array Sync result.
|
||||
*/
|
||||
public static function run_sync_now() {
|
||||
WBC_Logger::info( 'Cron', 'Manual sync triggered' );
|
||||
|
||||
$product_sync = new WBC_Product_Sync();
|
||||
$result = $product_sync->run_sync();
|
||||
|
||||
// Update last sync time
|
||||
self::update_last_sync_time();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all scheduled events
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user