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.



26
27
28
29
30
31
32
33
# File 'lib/exporter/export_metadata.rb', line 26

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.



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

def database
  @database
end

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

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#export_finished_atObject

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_idObject

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_timeObject

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_typeObject

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

#filepathObject

Returns the value of attribute filepath.



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

def filepath
  @filepath
end

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

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_valObject

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_duplicateObject

Returns the value of attribute on_duplicate.



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

def on_duplicate
  @on_duplicate
end

#sshObject

Returns the value of attribute ssh.



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

def ssh
  @ssh
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

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

#tableObject

Returns the value of attribute table.



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

def table
  @table
end

#zippedObject

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



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_pathObject

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

#filenameObject



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_callbacksObject

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

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

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

: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 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]
  @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_jsonObject



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