hensei-api/lib/post_deployment/image_downloader.rb
Justin Edmund d71b78e5f8
Update post-deploy script and Rails credentials (#155)
* Fresh credentials.yml.enc

* Update .ruby-gemset

* Made PostDeploymentManager modular

We broke PostDeploymentManager out into several files to make it easier to maintain.

We also added a "force" mode that forces the script to consider all CSV files. This is useful for testing the post-deploy script itself. This should only be used in test mode or you will dirty your database.

We also fine tuned some of the logging to make sure that both verbose and non-verbose modes are helpful.
2025-01-15 14:56:38 -08:00

73 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require_relative '../logging_helper'
module PostDeployment
class ImageDownloader
include LoggingHelper
STORAGE_DESCRIPTIONS = {
local: 'to local disk',
s3: 'to S3',
both: 'to local disk and S3'
}.freeze
def initialize(test_mode:, verbose:, storage:, new_records:, updated_records:)
@test_mode = test_mode
@verbose = verbose
@storage = storage
@new_records = new_records
@updated_records = updated_records
end
def run
log_header 'Downloading images...', '+'
[@new_records, @updated_records].each do |records|
records.each do |type, items|
next if items.empty?
download_type_images(type, items)
end
end
end
private
def download_type_images(type, items)
if @verbose
log_header "Processing #{type.pluralize} (#{items.size} records)...", "-"
puts "\n"
end
download_options = {
test_mode: @test_mode,
verbose: @verbose,
storage: @storage
}
items.each do |item|
id = @test_mode ? item[:granblue_id] : item.id
download_single_image(type, id, download_options)
end
end
def download_single_image(type, id, options)
action_text = @test_mode ? 'Would download' : 'Downloading'
storage_text = STORAGE_DESCRIPTIONS[options[:storage]]
log_verbose "#{action_text} images #{storage_text} for #{type} #{id}...\n"
unless @test_mode
Granblue::Downloader::DownloadManager.download_for_object(
type,
id,
**options
)
end
rescue => e
error_message = "Error #{@test_mode ? 'would occur' : 'occurred'} downloading images for #{type} #{id}: #{e.message}"
puts error_message
puts e.backtrace.take(5) if @verbose
end
end
end