Module: DynaModel::Extensions::S3Backup::ClassMethods

Defined in:
lib/dyna_model/extensions/s3_backup.rb

Instance Method Summary collapse

Instance Method Details

#create_dyna_model_s3_backup_bucket!Object



75
76
77
# File 'lib/dyna_model/extensions/s3_backup.rb', line 75

def create_dyna_model_s3_backup_bucket!
  self.dyna_model_s3_backup_client.buckets.create(self.dyna_model_s3_backup_config[:bucket])
end

#dyna_model_s3_backup(options = {}) ⇒ Object



48
49
50
51
52
53
# File 'lib/dyna_model/extensions/s3_backup.rb', line 48

def dyna_model_s3_backup(options={})
  raise "DynaModel::Extensions::S3Backup requires a bucket." unless options[:bucket]
  options[:prefix] ||= "#{self.to_s.underscore.pluralize}-#{Rails.env}"
  #options[:after_save] = lambda { |obj| ... }
  (@@dyna_model_s3_backup_config ||= {})[self.to_s] = options.dup
end

#dyna_model_s3_backup_bucketObject



63
64
65
# File 'lib/dyna_model/extensions/s3_backup.rb', line 63

def dyna_model_s3_backup_bucket
  self.dyna_model_s3_backup_client.buckets[self.dyna_model_s3_backup_config[:bucket]]
end

#dyna_model_s3_backup_clientObject



55
56
57
# File 'lib/dyna_model/extensions/s3_backup.rb', line 55

def dyna_model_s3_backup_client
  Thread.current[:dyna_model_s3_backup_client] ||= AWS::S3.new
end

#dyna_model_s3_backup_configObject



59
60
61
# File 'lib/dyna_model/extensions/s3_backup.rb', line 59

def dyna_model_s3_backup_config
  (@@dyna_model_s3_backup_config ||= {})[self.to_s]
end

#dyna_model_s3_backup_object(guid) ⇒ Object

Helper for sidekiq/etc delay method on class



80
81
82
# File 'lib/dyna_model/extensions/s3_backup.rb', line 80

def dyna_model_s3_backup_object(guid)
  self.read_guid(guid).try(:write_dyna_model_s3_backup!)
end

#enable_dyna_model_s3_backup_versioning!Object



67
68
69
# File 'lib/dyna_model/extensions/s3_backup.rb', line 67

def enable_dyna_model_s3_backup_versioning!
  self.dyna_model_s3_backup_bucket.enable_versioning
end

#import_from_dyna_model_s3_backupObject

TODO: improve for high scale… ability to resume



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dyna_model/extensions/s3_backup.rb', line 85

def import_from_dyna_model_s3_backup
  self.dyna_model_s3_backup_bucket.objects.with_prefix(self.dyna_model_s3_backup_config[:prefix]).each_batch do |batch|
    batch.each do |item|
      puts "Found #{item.key}"
      obj_json = ActiveSupport::JSON.decode(item.read)
      obj = obj_json["class"].constantize.new
      obj.attributes = obj_json["attributes"]
      if obj.save
        puts "Saved #{obj.dynamo_db_guid}."
      else
        puts "Failed to save #{obj.dynamo_db_guid}. #{obj.errors.full_messages.to_sentence}"
      end
    end
  end
end

#suspend_dyna_model_s3_backup_versioning!Object



71
72
73
# File 'lib/dyna_model/extensions/s3_backup.rb', line 71

def suspend_dyna_model_s3_backup_versioning!
  self.dyna_model_s3_backup_bucket.suspend_versioning
end