Created
December 13, 2017 23:10
-
-
Save pnispel/56d395dc33a478840953c79cf45f7b5c to your computer and use it in GitHub Desktop.
USD Work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Storage | |
module Migrations | |
# Implements the action of moving a document from its current location to | |
# the location its internal data suggests. | |
class Move | |
attr_reader :document | |
attr_reader :from | |
attr_reader :to | |
def initialize(document, to_usd) | |
@document = document | |
@from = document.usd | |
@to = to_usd | |
end | |
def from_object | |
S3Store[from.profile][from.purpose].object[from.key] | |
end | |
def to_object | |
S3Store[to.profile][to.purpose].object[to.key] | |
end | |
def perform | |
from_object.move_to(to_object) | |
document.usd = @to | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProstoreFile | |
... | |
def generate_storage_key | |
[ | |
SecureRandom.hex(4), # partition key | |
ULID.generate, # ensure uniqueness | |
company.generate_storage_suffix | |
].compact.join('/') + extension | |
end | |
def usd | |
@usd ||= build_usd | |
usd_serializer.load(@usd) | |
end | |
def usd=(value) | |
self.storage_profile_key = value.profile_name | |
self.key = value.key | |
@usd = usd_serializer.dump(value) | |
end | |
def build_usd | |
USD.new( | |
service: 's3', | |
profile_name: storage_profile_key, | |
purpose: 'general', | |
key: key, | |
meta: {} | |
) | |
end | |
private | |
def usd_serializer | |
@usd_serializer ||= USD::Serializer.new( | |
service: 's3', | |
profile_name: 'default', | |
purpose: 'general' | |
) | |
end | |
... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Storage | |
class CompanyStorageProfileMigrationWorker | |
include Sidekiq::Worker | |
sidekiq_options queue: :data_migration | |
def perform(company_id, new_profile) | |
if company = Company.find_by(id: company_id) | |
to_usd = prostore_file.build_usd | |
to_usd.profile_name = new_profile | |
# This is not very efficient, probably want to just select the ID. | |
company.prostore_files.select(:id).find_each do |prostore_file| | |
Workers::Storage::DocumentMoveWorker.perform_async( | |
prostore_file.name, | |
prostore_file.id, | |
to_usd | |
) | |
end | |
else | |
# Failing to find the company is a programming error or the company | |
# was deleted in the meanwhile. Report and move on. | |
report("Something something document is missing.") | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Storage | |
class DocumentMoveWorker | |
include Sidekiq::Worker | |
sidekiq_options queue: :data_migration | |
def perform(klass, id, to_usd) | |
document = klass.singularize.classify.constantize.find(id: id) | |
if document | |
Storage::Migrations::Move.new(document, to_usd).perform | |
else | |
# Failing to find the object to work on suggests a programming error | |
# or a stale job in the queue. Report and forget about this object. | |
report("Something something document is missing.") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment