Skip to content

Instantly share code, notes, and snippets.

@pnispel
Last active April 3, 2018 16:31
Show Gist options
  • Save pnispel/d4a22ff494417e3bb31b78a4d74e2b0d to your computer and use it in GitHub Desktop.
Save pnispel/d4a22ff494417e3bb31b78a4d74e2b0d to your computer and use it in GitHub Desktop.
Transfer Manifest
module Storage
class CompanyStorageProfileMigrator
attr_reader :company_id
attr_reader :new_profile
attr_reader :max_number_of_records_to_update
attr_accessor :manifest
def initialize(company_id:, new_profile:, max_number_of_records_to_update: Float::INFINITY)
@company_id = company_id
@new_profile = new_profile
@max_number_of_records_to_update = max_number_of_records_to_update
@manifest = ""
end
def perform(company_id:, new_profile:, max_number_of_records_to_update: Float::INFINITY)
begin
ProstoreFile.for_company(company_id).where(storage_type: ProstoreFile.storage_types[:on_s3]).where.not(storage_profile_key: new_profile).find_each do |prostore_file|
from_location = prostore_file.storage_location
to_location = prostore_file.storage_location
to_location.profile_name = new_profile
to_location.key = prostore_file.generate_storage_key
manifest += "#{from_location.to_s} #{to_location.to_s}\n"
Storage::DocumentCopyWorker.perform_async(
'ProstoreFile',
prostore_file.id,
to_location,
prostore_file.storage_options,
manifest_location
)
end
rescue => exception
# log to console and swallow so we will still write the manifest of our progress
puts exception.backtrace
end
write_manifest_to_s3
end
private
def write_manifest_to_s3
manifest_s3_object.write(manifest)
end
def manifest_s3_object
location = manifest_storage_location
S3Store[location.profile_name][location.purpose].object(location.key)
end
def manifest_storage_key
current_time = Time.now.strftime("%d/%m/%Y %H:%M")
"companies/#{company_id}/#{current_time}"
end
def manifest_storage_location
Storage::Location.new(
service: 's3',
profile_name: 'maintenance',
purpose: 'migration_manifest',
key: manifest_storage_key
)
end
end
end
class S3Store
PROFILES = {
'default' => {
'general' => {
region_name: 'us-east-1',
bucket_name: 'pro-core.com'
},
...
},
'maintenance' => {
'migration_manifest' => {
region_name: 'us-east-1',
bucket_name: 'migration_manifests'
}
}
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment