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:
@@ -11,6 +11,36 @@
|
||||
*/
|
||||
init: function() {
|
||||
this.bindEvents();
|
||||
this.checkInitialSyncStatus();
|
||||
},
|
||||
|
||||
/**
|
||||
* If a background sync is already running (e.g. triggered by cron,
|
||||
* or the admin reloaded the page mid-sync), resume polling for it.
|
||||
*/
|
||||
checkInitialSyncStatus: function() {
|
||||
var $btn = $('#wbc-manual-sync');
|
||||
var $status = $('#wbc-sync-status');
|
||||
|
||||
if (!$btn.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
$.ajax({
|
||||
url: wbc_admin.ajax_url,
|
||||
type: 'POST',
|
||||
data: {
|
||||
action: 'wbc_sync_status',
|
||||
nonce: wbc_admin.nonce
|
||||
},
|
||||
success: function(response) {
|
||||
if (response.success && response.data.status === 'running') {
|
||||
$btn.prop('disabled', true);
|
||||
$status.removeClass('success error').addClass('loading');
|
||||
WBC_Admin.pollSyncStatus($btn, $status);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -74,6 +104,10 @@
|
||||
|
||||
/**
|
||||
* Run manual sync
|
||||
*
|
||||
* The sync itself runs in the background (Action Scheduler batches),
|
||||
* so this just kicks it off and then polls for progress instead of
|
||||
* waiting on one long-lived request.
|
||||
*/
|
||||
manualSync: function(e) {
|
||||
e.preventDefault();
|
||||
@@ -92,13 +126,51 @@
|
||||
nonce: wbc_admin.nonce
|
||||
},
|
||||
success: function(response) {
|
||||
$btn.prop('disabled', false);
|
||||
$status.removeClass('loading');
|
||||
|
||||
if (response.success) {
|
||||
$status.addClass('success').text(response.data.message);
|
||||
WBC_Admin.pollSyncStatus($btn, $status);
|
||||
} else {
|
||||
$status.addClass('error').text(response.data.message || 'Sync failed');
|
||||
$btn.prop('disabled', false);
|
||||
$status.removeClass('loading').addClass('error').text((response.data && response.data.message) || 'Sync failed');
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
$btn.prop('disabled', false);
|
||||
$status.removeClass('loading').addClass('error').text('Request failed: ' + error);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Poll background sync progress until it completes
|
||||
*/
|
||||
pollSyncStatus: function($btn, $status) {
|
||||
$.ajax({
|
||||
url: wbc_admin.ajax_url,
|
||||
type: 'POST',
|
||||
data: {
|
||||
action: 'wbc_sync_status',
|
||||
nonce: wbc_admin.nonce
|
||||
},
|
||||
success: function(response) {
|
||||
if (!response.success) {
|
||||
$btn.prop('disabled', false);
|
||||
$status.removeClass('loading').addClass('error').text((response.data && response.data.message) || 'Failed to get sync status');
|
||||
return;
|
||||
}
|
||||
|
||||
var run = response.data;
|
||||
|
||||
if (run.status === 'running') {
|
||||
$status.text('Syncing... ' + (run.batches_done || 0) + ' / ' + (run.total_batches || 0) + ' batches (success: ' + (run.success || 0) + ', failed: ' + (run.failed || 0) + ', skipped: ' + (run.skipped || 0) + ')');
|
||||
setTimeout(function() {
|
||||
WBC_Admin.pollSyncStatus($btn, $status);
|
||||
}, 3000);
|
||||
} else if (run.status === 'completed') {
|
||||
$btn.prop('disabled', false);
|
||||
$status.removeClass('loading').addClass('success').text('Sync completed. Success: ' + (run.success || 0) + ', Failed: ' + (run.failed || 0) + ', Skipped: ' + (run.skipped || 0));
|
||||
} else {
|
||||
$btn.prop('disabled', false);
|
||||
$status.removeClass('loading');
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
|
||||
Reference in New Issue
Block a user