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_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.
-
#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
-
#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.
26 27 28 29 30 31 32 33 |
# File 'lib/exporter/export_metadata.rb', line 26 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.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def database @database end |
#ensure_callbacks ⇒ Object (readonly)
Returns the value of attribute ensure_callbacks.
23 24 25 |
# File 'lib/exporter/export_metadata.rb', line 23 def ensure_callbacks @ensure_callbacks end |
#error ⇒ Object
Returns the value of attribute error.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def error @error end |
#export_finished_at ⇒ Object
Returns the value of attribute export_finished_at.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def export_finished_at @export_finished_at end |
#export_id ⇒ Object
Returns the value of attribute export_id.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def export_id @export_id end |
#export_time ⇒ Object
Returns the value of attribute export_time.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def export_time @export_time end |
#export_type ⇒ Object
Returns the value of attribute export_type.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def export_type @export_type end |
#failure_callbacks ⇒ Object (readonly)
Returns the value of attribute failure_callbacks.
21 22 23 |
# File 'lib/exporter/export_metadata.rb', line 21 def failure_callbacks @failure_callbacks end |
#filepath ⇒ Object
Returns the value of attribute filepath.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def filepath @filepath end |
#ignore_callbacks ⇒ Object (readonly)
Returns the value of attribute ignore_callbacks.
24 25 26 |
# File 'lib/exporter/export_metadata.rb', line 24 def ignore_callbacks @ignore_callbacks end |
#incremental_col ⇒ Object
Returns the value of attribute incremental_col.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def incremental_col @incremental_col end |
#incremental_val ⇒ Object
Returns the value of attribute incremental_val.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def incremental_val @incremental_val end |
#on_duplicate ⇒ Object
Returns the value of attribute on_duplicate.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def on_duplicate @on_duplicate end |
#ssh ⇒ Object
Returns the value of attribute ssh.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def ssh @ssh end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def state @state end |
#success_callbacks ⇒ Object (readonly)
Returns the value of attribute success_callbacks.
22 23 24 |
# File 'lib/exporter/export_metadata.rb', line 22 def success_callbacks @success_callbacks end |
#table ⇒ Object
Returns the value of attribute table.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 def table @table end |
#zipped ⇒ Object
Returns the value of attribute zipped.
6 7 8 |
# File 'lib/exporter/export_metadata.rb', line 6 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/exporter/export_metadata.rb', line 60 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.message}\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
41 42 43 |
# File 'lib/exporter/export_metadata.rb', line 41 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
49 50 51 52 53 54 |
# File 'lib/exporter/export_metadata.rb', line 49 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
169 170 171 172 |
# File 'lib/exporter/export_metadata.rb', line 169 def export_path path = @zipped ? @filepath + ".gz" : @filepath return path end |
#filename ⇒ Object
35 36 37 38 39 |
# File 'lib/exporter/export_metadata.rb', line 35 def filename name = filepath.split("/").last name = zipped ? "#{name}.gz" : name return name end |
#load(metadata_path) ⇒ Object
186 187 188 189 190 |
# File 'lib/exporter/export_metadata.rb', line 186 def load json = File.open(, "rb").read hash = JSON.parse(json) set_attributes hash end |
#on_failure(*args, &block) ⇒ Object
Add a callback to run on failure of the export
94 95 96 97 98 99 100 |
# File 'lib/exporter/export_metadata.rb', line 94 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
104 105 106 107 108 109 110 |
# File 'lib/exporter/export_metadata.rb', line 104 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
114 115 116 117 118 119 120 |
# File 'lib/exporter/export_metadata.rb', line 114 def on_success *args, &block if block_given? @success_callbacks << block else @success_callbacks << args.shift end end |
#run_ensure_callbacks ⇒ Object
:nodoc:
123 124 125 126 127 |
# File 'lib/exporter/export_metadata.rb', line 123 def run_ensure_callbacks @ensure_callbacks.each do | ec | ec.call(self) end end |
#run_failure_callbacks ⇒ Object
:nodoc:
144 145 146 147 148 |
# File 'lib/exporter/export_metadata.rb', line 144 def run_failure_callbacks @failure_callbacks.each do | fc | fc.call(self) end end |
#run_ignore_callbacks ⇒ Object
:nodoc:
130 131 132 133 134 |
# File 'lib/exporter/export_metadata.rb', line 130 def run_ignore_callbacks @ignore_callbacks.each do | ic | ic.call(self) end end |
#run_success_callbacks ⇒ Object
:nodoc:
137 138 139 140 141 |
# File 'lib/exporter/export_metadata.rb', line 137 def run_success_callbacks @success_callbacks.each do | sc | sc.call(self) end end |
#set_attributes(options) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/exporter/export_metadata.rb', line 192 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] @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
179 180 181 182 183 184 |
# File 'lib/exporter/export_metadata.rb', line 179 def store! cmd = "echo \"#{self.to_json.gsub("\"","\\\\\"")}\" > #{@filepath}.json" puts cmd result = @ssh.exec!(cmd) puts result end |
#to_json ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/exporter/export_metadata.rb', line 150 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 } return obj.to_json end |