songtianlun
ce5d09b621
- Add menu labels and parents for AdminUser, City, Country, Region, WeatherArt, Ahoy::Event, and Ahoy::Visit. - Introduce a new page for managing Sidekiq jobs, providing functionality to execute or delete scheduled jobs. - Adjust batch job for generating weather art by using Sidekiq for improved performance. - Implement clean-up worker for old Ahoy data and functionalities for refreshing the sitemap. These changes enhance the administration interface by providing better organization and management tools for backend entities. The addition of Sidekiq jobs management further improves system maintenance capabilities.
69 lines
1.5 KiB
Ruby
69 lines
1.5 KiB
Ruby
class GenerateWeatherArtWorker
|
|
include Sidekiq::Worker
|
|
|
|
def perform(city_id)
|
|
@city = City.find(city_id)
|
|
|
|
weather_data = fetch_weather_data
|
|
return unless weather_data
|
|
|
|
prompt = generate_prompt(weather_data)
|
|
return unless prompt
|
|
|
|
image_url = generate_image(prompt)
|
|
return unless image_url
|
|
|
|
create_weather_art(weather_data, prompt, image_url)
|
|
rescue StandardError => e
|
|
Rails.logger.error "Error generating weather art for city #{city_id}: #{e.message}"
|
|
Rails.logger.error e.backtrace.join("\n")
|
|
end
|
|
|
|
private
|
|
|
|
attr_reader :city
|
|
|
|
def fetch_weather_data
|
|
WeatherService.new.get_weather(city.latitude, city.longitude)
|
|
end
|
|
|
|
def generate_prompt(weather_data)
|
|
AiService.new.generate_prompt(city, weather_data)
|
|
end
|
|
|
|
def generate_image(prompt)
|
|
AiService.new.generate_image(prompt)
|
|
end
|
|
|
|
def create_weather_art(weather_data, prompt, image_url)
|
|
tempfile = nil
|
|
|
|
ActiveRecord::Base.transaction do
|
|
weather_art = city.weather_arts.create!(
|
|
weather_date: Date.today,
|
|
prompt: prompt,
|
|
**weather_data
|
|
)
|
|
|
|
tempfile = Down.download(image_url)
|
|
|
|
weather_art.image.attach(
|
|
io: File.open(tempfile.path),
|
|
filename: generate_filename,
|
|
content_type: "image/png"
|
|
)
|
|
|
|
weather_art
|
|
end
|
|
ensure
|
|
if tempfile
|
|
tempfile.close
|
|
tempfile.unlink
|
|
end
|
|
end
|
|
|
|
def generate_filename
|
|
"#{city.country.name}-#{city.name.parameterize}-#{Time.current.strftime('%Y%m%d-%H%M%S')}.png"
|
|
end
|
|
end
|