- Created BatchGenerateWeatherArtsJob to process eligible cities and generate weather art. - Introduced GenerateWeatherArtJob for generating weather art and image attachment. - Added AiService for obtaining prompts and generating images with OpenAI API. - Implemented WeatherService to fetch current weather data from the QWeather API. - Updated Gemfile with necessary gems (whenever, ruby-openai, httparty, down, aws-sdk-s3). This commit introduces a system to create and store weather art images for various cities based on current weather conditions, leveraging external APIs for data and image generation.
35 lines
808 B
Ruby
35 lines
808 B
Ruby
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
|