Class: Myreplicator::ExportMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/exporter/export_metadata.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  options = args.extract_options!
  if options[:metadata_path] 
    load options[:metadata_path] 
  else
    set_attributes options
  end
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



5
6
7
# File 'lib/exporter/export_metadata.rb', line 5

def database
  @database
end

#ensure_callbacksObject (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

#errorObject

Returns the value of attribute error.



5
6
7
# File 'lib/exporter/export_metadata.rb', line 5

def error
  @error
end

#export_finished_atObject

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_idObject

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_timeObject

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_toObject

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_typeObject

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_callbacksObject (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

#filepathObject

Returns the value of attribute filepath.



5
6
7
# File 'lib/exporter/export_metadata.rb', line 5

def filepath
  @filepath
end

#ignore_callbacksObject (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_colObject

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_valObject

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_duplicateObject

Returns the value of attribute on_duplicate.



5
6
7
# File 'lib/exporter/export_metadata.rb', line 5

def on_duplicate
  @on_duplicate
end

#sshObject

Returns the value of attribute ssh.



5
6
7
# File 'lib/exporter/export_metadata.rb', line 5

def ssh
  @ssh
end

#stateObject

Returns the value of attribute state.



5
6
7
# File 'lib/exporter/export_metadata.rb', line 5

def state
  @state
end

#store_inObject

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_callbacksObject (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

#tableObject

Returns the value of attribute table.



5
6
7
# File 'lib/exporter/export_metadata.rb', line 5

def table
  @table
end

#zippedObject

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
  options = args.extract_options!
  options.reverse_merge!(:export_time => Time.now,
                         :state => "exporting")
  begin
     = .new 
    .set_attributes options

    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



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_pathObject

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

#filenameObject



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_filenameObject



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_callbacksObject

: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_callbacksObject

: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_callbacksObject

: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_callbacksObject

: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 options
  options.symbolize_keys!
  @export_time = options[:export_time] if options[:export_time]
  @table = options[:table] if options[:table]
  @database = options[:database] if options[:database]
  @state = options[:state] if options[:state] 
  @incremental_col = options[:incremental_col] if options[:incremental_col]
  @incremental_val = options[:incremental_val] if options[:incremental_val]
  @export_id = options[:export_id] if options[:export_id]
  @filepath = options[:filepath].nil? ? nil : options[:filepath]
  @on_duplicate = options[:on_duplicate] if options[:on_duplicate]
  @export_type = options[:export_type] if options[:export_type]
  @zipped = options[:zipped].nil? ? false : options[:zipped]
  @store_in = options[:store_in] if options[:store_in]
  @export_to = options[:export_to] if options[: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_jsonObject



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