From 2e6e1248ec04fb4360f333485104b2e14158df51 Mon Sep 17 00:00:00 2001 From: datorik Date: Fri, 11 Sep 2026 20:30:39 +0300 Subject: [PATCH 1/2] Udp.Code.Synchronize with cloud --- lib/CleantalkSP/SpbctWP/Sync.php | 61 ++++++++++++-------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/lib/CleantalkSP/SpbctWP/Sync.php b/lib/CleantalkSP/SpbctWP/Sync.php index 58dd25553..73f1255f6 100644 --- a/lib/CleantalkSP/SpbctWP/Sync.php +++ b/lib/CleantalkSP/SpbctWP/Sync.php @@ -91,32 +91,17 @@ public static function formatStatus($progress) */ public static function run() { - global $spbc; - - $progress = self::getProgress(); - if ( ! empty($progress['in_progress']) ) { - self::saveProgress(self::getDefaultProgress()); - } + self::initializeProgress(); - $account_is_ok = false; + do { + self::runCurrentStep(); + $progress = self::getProgress(); + } while ( ! empty($progress['in_progress']) ); - self::stepAccessKeyCheck($account_is_ok); - self::stepSecfwUpdate($account_is_ok); - self::stepSignaturesUpdate(); - self::stepSettingsExclusions(); - self::stepAdjustEnv(); - self::stepVulnerabilityCheck(); - self::stepAnalysisLogUpdate($account_is_ok); - self::stepPscCacheWarm(); - - $out = array( - 'success' => true, - 'reload' => self::needsReloadPage($spbc), + return array( + 'success' => ! empty($progress['success']), + 'reload' => ! empty($progress['reload']), ); - - self::stepFinalize(); - - return $out; } /** @@ -193,12 +178,13 @@ public static function runCurrentStep() self::stepPscCacheWarm(); break; case 'finalize': + $reload = self::needsReloadPage($spbc); self::stepFinalize(); $progress = self::getProgress(); $progress['in_progress'] = false; $progress['success'] = true; - $progress['reload'] = self::needsReloadPage($spbc); + $progress['reload'] = $reload; $progress['percent'] = 100; $progress['message'] = self::STEPS['finalize']; $progress['last_step_at'] = time(); @@ -276,20 +262,7 @@ public static function startBackground() return self::formatStatus(self::getProgress()); } - $steps = array_keys(self::STEPS); - $labels = self::STEPS; - - $progress = array( - 'in_progress' => true, - 'step_index' => 0, - 'account_is_ok' => false, - 'message' => $labels[ $steps[0] ], - 'percent' => 0, - 'reload' => false, - 'success' => false, - 'last_step_at' => time(), - ); - self::saveProgress($progress); + self::initializeProgress(); self::runCurrentStep(); @@ -301,6 +274,18 @@ public static function startBackground() return self::formatStatus(self::getProgress()); } + /** + * Initialize progress for synchronous and background sync. + */ + private static function initializeProgress() + { + $progress = self::getDefaultProgress(); + $progress['in_progress'] = true; + $progress['message'] = self::STEPS['access_key_check']; + $progress['last_step_at'] = time(); + self::saveProgress($progress); + } + /** * Remote call handler: run the next sync step. */ From d8e756a6b076a5633d389cc3e4006a2c1881c2f2 Mon Sep 17 00:00:00 2001 From: datorik Date: Sun, 13 Sep 2026 21:41:46 +0300 Subject: [PATCH 2/2] Udp.Code.Synchronize with cloud --- lib/CleantalkSP/SpbctWP/Sync.php | 34 +++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/CleantalkSP/SpbctWP/Sync.php b/lib/CleantalkSP/SpbctWP/Sync.php index 73f1255f6..4c73fcba1 100644 --- a/lib/CleantalkSP/SpbctWP/Sync.php +++ b/lib/CleantalkSP/SpbctWP/Sync.php @@ -25,6 +25,14 @@ class Sync 'finalize' => 'Sync end...', ); + private const MODE_SYNC = 'sync'; + private const MODE_BACKGROUND = 'background'; + + /** + * Seconds without step progress after which a synchronous sync is considered dead. + */ + private const SYNC_MODE_STALE_TIMEOUT = 300; + /** * @return array */ @@ -32,6 +40,7 @@ private static function getDefaultProgress() { return array( 'in_progress' => false, + 'mode' => '', 'step_index' => 0, 'account_is_ok' => false, 'message' => '', @@ -91,7 +100,8 @@ public static function formatStatus($progress) */ public static function run() { - self::initializeProgress(); + // Sync mode turns queued background continuations and status polling into no-ops. + self::initializeProgress(self::MODE_SYNC); do { self::runCurrentStep(); @@ -227,6 +237,16 @@ public static function maybeResume() return; } + if ( $progress['mode'] === self::MODE_SYNC ) { + // Synchronous sync is running in another process. Take it over only if that process has died. + if ( time() - (int) $progress['last_step_at'] < self::SYNC_MODE_STALE_TIMEOUT ) { + return; + } + + $progress['mode'] = self::MODE_BACKGROUND; + self::saveProgress($progress); + } + $steps = array_keys(self::STEPS); if ( (int) $progress['step_index'] >= count($steps) ) { return; @@ -262,7 +282,7 @@ public static function startBackground() return self::formatStatus(self::getProgress()); } - self::initializeProgress(); + self::initializeProgress(self::MODE_BACKGROUND); self::runCurrentStep(); @@ -276,11 +296,14 @@ public static function startBackground() /** * Initialize progress for synchronous and background sync. + * + * @param string $mode self::MODE_SYNC or self::MODE_BACKGROUND */ - private static function initializeProgress() + private static function initializeProgress($mode) { $progress = self::getDefaultProgress(); $progress['in_progress'] = true; + $progress['mode'] = $mode; $progress['message'] = self::STEPS['access_key_check']; $progress['last_step_at'] = time(); self::saveProgress($progress); @@ -291,6 +314,11 @@ private static function initializeProgress() */ public static function continueSync() { + $progress = self::getProgress(); + if ( $progress['mode'] === self::MODE_SYNC ) { + return; + } + self::runCurrentStep(); $progress = self::getProgress();