class BatchGenerateWeatherArtsJob < ApplicationJob queue_as :default def perform(*args) start_time = Time.current max_duration = 50.minutes cities_to_process = get_eligible_cities cities_to_process.each do |city| break if Time.current - start_time > max_duration GenerateWeatherArtJob.perform_now(city) sleep 1.minute # 确保不超过API限制 end end private def get_eligible_cities City.active .where(active: true) .where("last_weather_fetch IS NULL OR last_weather_fetch < ?", Date.today) # .select { |city| early_morning_in_timezone?(city.timezone) } end # def early_morning_in_timezone?(timezone) # return false if timezone.blank? # time = Time.current.in_time_zone(timezone) # time.hour == 2 # end end