From a444fe4aa66924dccfd0aa36800ef3c2c04a05a7 Mon Sep 17 00:00:00 2001 From: Jacek Pyziak Date: Thu, 19 Feb 2026 00:30:48 +0100 Subject: [PATCH] feat: Add dynamic client batch processing and configuration for cron products URLs --- autoload/controls/class.Cron.php | 94 +++++++++++++++++++++++++++++++- config.php | 1 + 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/autoload/controls/class.Cron.php b/autoload/controls/class.Cron.php index 7ba573e..e1501d6 100644 --- a/autoload/controls/class.Cron.php +++ b/autoload/controls/class.Cron.php @@ -220,7 +220,7 @@ class Cron static public function cron_products_urls() { - global $mdb; + global $mdb, $settings; self::touch_cron_invocation( __FUNCTION__ ); $api = new \services\GoogleAdsApi(); @@ -237,10 +237,31 @@ class Cron $debug_mode = (int) \S::get( 'debug' ) === 1; if ( $batch_limit <= 0 ) { - $batch_limit = 300; + $batch_limit = (int) ( $settings['cron_products_urls_limit_per_client'] ?? 100 ); + } + if ( $batch_limit <= 0 ) + { + $batch_limit = 100; } $batch_limit = min( 1000, $batch_limit ); + $clients_per_run_default = (int) ( $settings['cron_products_urls_clients_per_run'] ?? ( $settings['cron_products_clients_per_run'] ?? 1 ) ); + if ( $clients_per_run_default <= 0 ) + { + $clients_per_run_default = 1; + } + + $clients_per_run = (int) \S::get( 'clients_per_run' ); + if ( $clients_per_run <= 0 ) + { + $clients_per_run = (int) self::get_setting_value( 'cron_products_urls_clients_per_run', $clients_per_run_default ); + } + if ( $clients_per_run <= 0 ) + { + $clients_per_run = $clients_per_run_default; + } + $clients_per_run = min( 20, $clients_per_run ); + $where = "deleted = 0 AND google_merchant_account_id IS NOT NULL AND google_merchant_account_id <> ''"; if ( $client_id > 0 ) { @@ -261,6 +282,17 @@ class Cron exit; } + $total_clients_available = count( $clients ); + if ( $client_id <= 0 ) + { + $last_client_cursor = (int) self::get_setting_value( 'cron_products_urls_last_client_id', 0 ); + $clients = self::pick_clients_batch_by_cursor( $clients, $clients_per_run, $last_client_cursor ); + } + else + { + $clients_per_run = 1; + } + $checked_products = 0; $updated_urls = 0; $unresolved_products = 0; @@ -361,9 +393,21 @@ class Cron $details[] = $detail_row; } + if ( $client_id <= 0 && !empty( $clients ) ) + { + $last_client = end( $clients ); + $last_client_id = (int) ( $last_client['id'] ?? 0 ); + if ( $last_client_id > 0 ) + { + self::set_setting_value( 'cron_products_urls_last_client_id', (string) $last_client_id ); + } + } + echo json_encode( [ 'result' => empty( $errors ) ? 'Synchronizacja URL produktow zakonczona.' : 'Synchronizacja URL produktow zakonczona z bledami.', + 'total_clients_available' => $total_clients_available, 'processed_clients' => $processed_clients, + 'clients_per_run' => $clients_per_run, 'checked_products' => $checked_products, 'updated_urls' => $updated_urls, 'unresolved_products' => $unresolved_products, @@ -3044,6 +3088,52 @@ class Cron return 0; } + static private function pick_clients_batch_by_cursor( $clients, $limit, $cursor_client_id = 0 ) + { + $clients = is_array( $clients ) ? array_values( $clients ) : []; + if ( empty( $clients ) ) + { + return []; + } + + $limit = max( 1, (int) $limit ); + $total = count( $clients ); + if ( $limit >= $total ) + { + return $clients; + } + + $start_index = 0; + $cursor_client_id = (int) $cursor_client_id; + if ( $cursor_client_id > 0 ) + { + $found_next = false; + foreach ( $clients as $idx => $client ) + { + $current_id = (int) ( $client['id'] ?? 0 ); + if ( $current_id > $cursor_client_id ) + { + $start_index = $idx; + $found_next = true; + break; + } + } + + if ( !$found_next ) + { + $start_index = 0; + } + } + + $batch = []; + for ( $i = 0; $i < $limit; $i++ ) + { + $batch[] = $clients[( $start_index + $i ) % $total]; + } + + return $batch; + } + static private function get_conversion_window_days() { $request_value = (int) \S::get( 'conversion_window_days' ); diff --git a/config.php b/config.php index 444bb8a..319a2e2 100644 --- a/config.php +++ b/config.php @@ -12,3 +12,4 @@ $settings['email_password'] = 'ProjectPro2025!'; $settings['cron_products_clients_per_run'] = 1; $settings['cron_campaigns_clients_per_run'] = 1; +$settings['cron_products_urls_limit_per_client'] = 10;