From f2951e2741aba9855ceecadbc7f03c56b63057d8 Mon Sep 17 00:00:00 2001 From: songtianlun Date: Sat, 1 Feb 2025 15:01:36 +0800 Subject: [PATCH] feat: optimize batch weather art generation - Introduce BATCH_SIZE constant to limit processed cities - Shuffle and limit eligible cities processing to enhance worker efficiency This update improves the performance of the BatchGenerateWeatherArtsWorker by ensuring that only a set number of cities are processed within the allotted time, reducing the risk of timeouts and making the overall system more responsive. --- app/workers/batch_generate_weather_arts_worker.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/workers/batch_generate_weather_arts_worker.rb b/app/workers/batch_generate_weather_arts_worker.rb index 5d7f16d..0d00b97 100644 --- a/app/workers/batch_generate_weather_arts_worker.rb +++ b/app/workers/batch_generate_weather_arts_worker.rb @@ -1,19 +1,16 @@ class BatchGenerateWeatherArtsWorker include Sidekiq::Worker - GENERATION_INTERVAL = 24.hours MAX_DURATION = 50.minutes SLEEP_DURATION = 120.seconds + BATCH_SIZE = 20 def perform(*args) start_time = Time.current - - cities_to_process = get_eligible_cities - + cities_to_process = get_eligible_cities.shuffle.take(BATCH_SIZE) cities_to_process.each do |city| break if Time.current - start_time > MAX_DURATION Rails.logger.info "Generating weather art for #{city.name}" - GenerateWeatherArtWorker.perform_async(city.id) sleep SLEEP_DURATION end @@ -23,7 +20,6 @@ class BatchGenerateWeatherArtsWorker def get_eligible_cities cutoff_time = Time.current - GENERATION_INTERVAL - City.active .joins("LEFT JOIN ( SELECT city_id, MAX(created_at) as last_generation_time