Class: Myreplicator::ExportMetadata
- Inherits:
-
Object
- Object
- Myreplicator::ExportMetadata
- Defined in:
- lib/exporter/export_metadata.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
Returns the value of attribute database.
-
#ensure_callbacks ⇒ Object
readonly
Returns the value of attribute ensure_callbacks.
-
#error ⇒ Object
Returns the value of attribute error.
-
#export_finished_at ⇒ Object
Returns the value of attribute export_finished_at.
-
#export_id ⇒ Object
Returns the value of attribute export_id.
-
#export_time ⇒ Object
Returns the value of attribute export_time.
-
#export_to ⇒ Object
Returns the value of attribute export_to.
-
#export_type ⇒ Object
Returns the value of attribute export_type.
-
#failure_callbacks ⇒ Object
readonly
Returns the value of attribute failure_callbacks.
-
#filepath ⇒ Object
Returns the value of attribute filepath.
-
#ignore_callbacks ⇒ Object
readonly
Returns the value of attribute ignore_callbacks.
-
#incremental_col ⇒ Object
Returns the value of attribute incremental_col.
-
#incremental_val ⇒ Object
Returns the value of attribute incremental_val.
-
#on_duplicate ⇒ Object
Returns the value of attribute on_duplicate.
-
#ssh ⇒ Object
Returns the value of attribute ssh.
-
#state ⇒ Object
Returns the value of attribute state.
-
#store_in ⇒ Object
Returns the value of attribute store_in.
-
#success_callbacks ⇒ Object
readonly
Returns the value of attribute success_callbacks.
-
#table ⇒ Object
Returns the value of attribute table.
-
#zipped ⇒ Object
Returns the value of attribute zipped.
Class Method Summary collapse
-
.record(*args) ⇒ Object
Keeps track of the state of the export Stores itself in a JSON file on exit.
Instance Method Summary collapse
- #destination_filepath(tmp_dir) ⇒ Object
-
#equals(object) ⇒ Object
Compares the object with another metadata object Return true if they are for the same table.
-
#export_path ⇒ Object
Final path of the dump file after zip.
- #filename ⇒ Object
-
#initialize(*args) ⇒ ExportMetadata
constructor
A new instance of ExportMetadata.
- #load(metadata_path) ⇒ Object
- #metadata_filename ⇒ Object
- #metadata_filepath(tmp_dir) ⇒ Object
-
#on_failure(*args, &block) ⇒ Object
Add a callback to run on failure of the export.
-
#on_ignore(*args, &block) ⇒ Object
Adds a callback that runs if the export is already running.
-
#on_success(*args, &block) ⇒ Object
Adds a callback that runs if the export is completed successfully.
-
#run_ensure_callbacks ⇒ Object
:nodoc:.
-
#run_failure_callbacks ⇒ Object
:nodoc:.
-
#run_ignore_callbacks ⇒ Object
:nodoc:.
-
#run_success_callbacks ⇒ Object
:nodoc:.
- #set_attributes(options) ⇒ Object
-
#store! ⇒ Object
Writes Json to file using echo file is written to remote server via SSH Echo is used for writing the file.
- #to_json ⇒ Object
Constructor Details
#initialize(*args) ⇒ ExportMetadata
Returns a new instance of ExportMetadata.
27 28 29 30 31 32 33 34 |
# File 'lib/exporter/export_metadata.rb', line 27 def initialize *args = args. if [:metadata_path] load [:metadata_path] else set_attributes end end |
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def database @database end |
#ensure_callbacks ⇒ Object (readonly)
Returns the value of attribute ensure_callbacks.
24 25 26 |
# File 'lib/exporter/export_metadata.rb', line 24 def ensure_callbacks @ensure_callbacks end |
#error ⇒ Object
Returns the value of attribute error.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def error @error end |
#export_finished_at ⇒ Object
Returns the value of attribute export_finished_at.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def export_finished_at @export_finished_at end |
#export_id ⇒ Object
Returns the value of attribute export_id.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def export_id @export_id end |
#export_time ⇒ Object
Returns the value of attribute export_time.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def export_time @export_time end |
#export_to ⇒ Object
Returns the value of attribute export_to.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def export_to @export_to end |
#export_type ⇒ Object
Returns the value of attribute export_type.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def export_type @export_type end |
#failure_callbacks ⇒ Object (readonly)
Returns the value of attribute failure_callbacks.
22 23 24 |
# File 'lib/exporter/export_metadata.rb', line 22 def failure_callbacks @failure_callbacks end |
#filepath ⇒ Object
Returns the value of attribute filepath.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def filepath @filepath end |
#ignore_callbacks ⇒ Object (readonly)
Returns the value of attribute ignore_callbacks.
25 26 27 |
# File 'lib/exporter/export_metadata.rb', line 25 def ignore_callbacks @ignore_callbacks end |
#incremental_col ⇒ Object
Returns the value of attribute incremental_col.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def incremental_col @incremental_col end |
#incremental_val ⇒ Object
Returns the value of attribute incremental_val.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def incremental_val @incremental_val end |
#on_duplicate ⇒ Object
Returns the value of attribute on_duplicate.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def on_duplicate @on_duplicate end |
#ssh ⇒ Object
Returns the value of attribute ssh.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def ssh @ssh end |
#state ⇒ Object
Returns the value of attribute state.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def state @state end |
#store_in ⇒ Object
Returns the value of attribute store_in.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def store_in @store_in end |
#success_callbacks ⇒ Object (readonly)
Returns the value of attribute success_callbacks.
23 24 25 |
# File 'lib/exporter/export_metadata.rb', line 23 def success_callbacks @success_callbacks end |
#table ⇒ Object
Returns the value of attribute table.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def table @table end |
#zipped ⇒ Object
Returns the value of attribute zipped.
5 6 7 |
# File 'lib/exporter/export_metadata.rb', line 5 def zipped @zipped end |
Class Method Details
.record(*args) ⇒ Object
Keeps track of the state of the export Stores itself in a JSON file on exit
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/exporter/export_metadata.rb', line 71 def self.record *args = args. .reverse_merge!(:export_time => Time.now, :state => "exporting") begin = ExportMetadata.new .set_attributes yield .run_success_callbacks rescue Exceptions::ExportError => e .state = "failed" .error = "#{e.}\n#{e.backtrace}" .run_failure_callbacks rescue Exceptions::ExportIgnored => e .state = "ignored" .run_ignore_callbacks .filepath = .filepath + ".ignored" ensure .export_finished_at = Time.now .state = "failed" if .state == "exporting" .store! .ssh.close .run_ensure_callbacks end end |
Instance Method Details
#destination_filepath(tmp_dir) ⇒ Object
52 53 54 |
# File 'lib/exporter/export_metadata.rb', line 52 def destination_filepath tmp_dir File.join(tmp_dir, filename) end |
#equals(object) ⇒ Object
Compares the object with another metadata object Return true if they are for the same table
60 61 62 63 64 65 |
# File 'lib/exporter/export_metadata.rb', line 60 def equals object if table == object.table && database == object.database return true end return false end |
#export_path ⇒ Object
Final path of the dump file after zip
182 183 184 185 |
# File 'lib/exporter/export_metadata.rb', line 182 def export_path path = @zipped ? @filepath + ".gz" : @filepath return path end |
#filename ⇒ Object
36 37 38 39 40 |
# File 'lib/exporter/export_metadata.rb', line 36 def filename name = filepath.split("/").last name = zipped ? "#{name}.gz" : name return name end |
#load(metadata_path) ⇒ Object
200 201 202 203 204 |
# File 'lib/exporter/export_metadata.rb', line 200 def load json = File.open(, "rb").read hash = JSON.parse(json) set_attributes hash end |
#metadata_filename ⇒ Object
42 43 44 45 46 |
# File 'lib/exporter/export_metadata.rb', line 42 def name = filepath.split("/").last name += ".json" return name end |
#metadata_filepath(tmp_dir) ⇒ Object
48 49 50 |
# File 'lib/exporter/export_metadata.rb', line 48 def tmp_dir File.join(tmp_dir, ) end |
#on_failure(*args, &block) ⇒ Object
Add a callback to run on failure of the export
105 106 107 108 109 110 111 |
# File 'lib/exporter/export_metadata.rb', line 105 def on_failure *args, &block if block_given? @failure_callbacks << block else @failure_callbacks << args.shift end end |
#on_ignore(*args, &block) ⇒ Object
Adds a callback that runs if the export is already running
115 116 117 118 119 120 121 |
# File 'lib/exporter/export_metadata.rb', line 115 def on_ignore *args, &block if block_given? @ignore_callbacks << block else @ignore_callbacks << args.shift end end |
#on_success(*args, &block) ⇒ Object
Adds a callback that runs if the export is completed successfully
125 126 127 128 129 130 131 |
# File 'lib/exporter/export_metadata.rb', line 125 def on_success *args, &block if block_given? @success_callbacks << block else @success_callbacks << args.shift end end |
#run_ensure_callbacks ⇒ Object
:nodoc:
134 135 136 137 138 |
# File 'lib/exporter/export_metadata.rb', line 134 def run_ensure_callbacks @ensure_callbacks.each do | ec | ec.call(self) end end |
#run_failure_callbacks ⇒ Object
:nodoc:
155 156 157 158 159 |
# File 'lib/exporter/export_metadata.rb', line 155 def run_failure_callbacks @failure_callbacks.each do | fc | fc.call(self) end end |
#run_ignore_callbacks ⇒ Object
:nodoc:
141 142 143 144 145 |
# File 'lib/exporter/export_metadata.rb', line 141 def run_ignore_callbacks @ignore_callbacks.each do | ic | ic.call(self) end end |
#run_success_callbacks ⇒ Object
:nodoc:
148 149 150 151 152 |
# File 'lib/exporter/export_metadata.rb', line 148 def run_success_callbacks @success_callbacks.each do | sc | sc.call(self) end end |
#set_attributes(options) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/exporter/export_metadata.rb', line 206 def set_attributes .symbolize_keys! @export_time = [:export_time] if [:export_time] @table = [:table] if [:table] @database = [:database] if [:database] @state = [:state] if [:state] @incremental_col = [:incremental_col] if [:incremental_col] @incremental_val = [:incremental_val] if [:incremental_val] @export_id = [:export_id] if [:export_id] @filepath = [:filepath].nil? ? nil : [:filepath] @on_duplicate = [:on_duplicate] if [:on_duplicate] @export_type = [:export_type] if [:export_type] @zipped = [:zipped].nil? ? false : [:zipped] @store_in = [:store_in] if [:store_in] @export_to = [:export_to] if [:export_to] @ssh = nil @success_callbacks = [] @failure_callbacks = [] @ensure_callbacks = [] @ignore_callbacks = [] end |
#store! ⇒ Object
Writes Json to file using echo file is written to remote server via SSH Echo is used for writing the file
192 193 194 195 196 197 198 |
# File 'lib/exporter/export_metadata.rb', line 192 def store! Kernel.p "$$$$$$$$$$$$$$$$$$ @ssh CHECK $$$$$$$$$$$$$$$$$$" cmd = "echo \"#{self.to_json.gsub("\"","\\\\\"")}\" > #{@filepath}.json" puts cmd result = @ssh.exec!(cmd) puts result end |
#to_json ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/exporter/export_metadata.rb', line 161 def to_json obj = { :export_time => @export_time, :table => @table, :database => @database, :state => @state, :incremental_col => @incremental_col, :incremental_val => @incremental_val, :export_id => @export_id, :filepath => @filepath, :zipped => @zipped, :export_type => @export_type, :export_to => @export_to, :store_in => @store_in } return obj.to_json end |