Class: Myreplicator::Export

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/myreplicator/export.rb

Defined Under Namespace

Classes: SourceDb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



30
31
32
# File 'app/models/myreplicator/export.rb', line 30

def filename
  @filename
end

Class Method Details

.available_dbsObject

List of all avaiable databases from database.yml file All Export/Load jobs can use these databases



207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/myreplicator/export.rb', line 207

def self.available_dbs
  dbs = ActiveRecord::Base.configurations.keys
  available = [] 

  dbs.each do |db|
    db_config = ActiveRecord::Base.configurations[db]
    unless db_config["myreplicator"].nil?
      available << db if db_config["myreplicator"]
    end
  end
  return available
end

.available_tablesObject

Returns a hash of => [TableName1,…], DB => …



194
195
196
197
198
199
200
201
# File 'app/models/myreplicator/export.rb', line 194

def self.available_tables
   = {}
  available_dbs.each do |db|
    tables = SourceDb.get_tables(db)
    [db] = tables
  end
  return 
end

.perform(export_id, *args) ⇒ Object

Perfoms the export job, Provided for Resque



37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/myreplicator/export.rb', line 37

def self.perform(export_id, *args)
 options = args.extract_options!
 ActiveRecord::Base.verify_active_connections!
 ActiveRecord::Base.connection.reconnect!
 export_obj = Export.find(export_id)

 if export_obj.active
   export_obj.export
 end

end

.schedule_in_resqueObject

NOTE: Provided for Resque use Schedules all the exports in resque Requires Resque Scheduler



225
226
227
228
229
230
231
232
233
234
235
# File 'app/models/myreplicator/export.rb', line 225

def self.schedule_in_resque
  exports = Export.find(:all)
  exports.each do |export|
    if export.active
      export.schedule
    else
      Resque.remove_schedule(export.schedule_name)
    end
  end
  Resque.reload_schedule! # Reload all schedules in Resque
end

Instance Method Details

#connection_factory(type) ⇒ Object

Connects to the server via ssh/sftp



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/myreplicator/export.rb', line 170

def connection_factory type
  config = Myreplicator.configs[self.source_schema]
  
  case type
  when :ssh
    if config.has_key? "ssh_password"
      return Net::SSH.start(config["ssh_host"], config["ssh_user"], :password => config["ssh_password"])

    elsif(config.has_key? "ssh_private_key")
      return Net::SSH.start(config["ssh_host"], config["ssh_user"], :keys => [config["ssh_private_key"]])
    end
  when :sftp
    if config.has_key? "ssh_password"
      return Net::SFTP.start(config["ssh_host"], config["ssh_user"], :password => config["ssh_password"])

    elsif(config.has_key? "ssh_private_key")
      return Net::SFTP.start(config["ssh_host"], config["ssh_user"], :keys => [config["ssh_private_key"]])
    end          
  end
end

#destination_max_incremental_valueObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/myreplicator/export.rb', line 89

def destination_max_incremental_value
  sql = SqlCommands.max_value_sql(:incremental_col => self.incremental_column,
                                              :db => self.destination_schema,
                                              :table => self.table_name)
  puts sql
  if self.export_to == 'vertica'
    begin
      result = Myreplicator::DB.exec_sql('vertica',sql)
      if result.rows.first[:max].blank?
        return "0"
      else
        case result.rows.first[:max].class.to_s
        when "DateTime"
          return result.rows.first[:max].strftime('%Y-%m-%d %H:%M:%S')
        else
          return result.rows.first[:max].to_s
        end
      end
    rescue Exception => e
      puts "Vertica Table Not Existed"
    end
  else
    result = Myreplicator::DB.exec_sql(self.destination_schema,sql)
    if result.first.nil?
      return ""
    else
      case result.first.first.class.to_s
      when "Symbol", "Fixnum"
        return result.first.first.to_s
      else
        return result.first.first.to_s(:db)
      end
    end
  end
  return "0"
end

#exec_on_source(sql) ⇒ Object



152
153
154
155
# File 'app/models/myreplicator/export.rb', line 152

def exec_on_source sql
  result = SourceDb.exec_sql(self.source_schema, sql)
  return result
end

#exportObject

Runs the export process using the required Exporter library



52
53
54
55
56
57
58
# File 'app/models/myreplicator/export.rb', line 52

def export
  Log.run(:job_type => "export", :name => schedule_name, 
          :file => filename, :export_id => id) do |log|
    exporter = MysqlExporter.new
    exporter.export_table self # pass current object to exporter
  end
end

#export_type?Boolean

end

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'app/models/myreplicator/export.rb', line 70

def export_type?
  if state == "new"
    return :new
  elsif incremental_export?
    return :incremental
  end
end

#incremental_export?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'app/models/myreplicator/export.rb', line 78

def incremental_export?
  if export_type == "incremental"
    return true
  end
  return false
end

#is_running?Boolean

Throws ExportIgnored if the job is still running Checks the state of the job using PID and state

Returns:

  • (Boolean)


260
261
262
263
264
265
266
267
268
# File 'app/models/myreplicator/export.rb', line 260

def is_running?
  return false if state != "exporting"
  begin
    Process.getpgid(exporter_pid)
    raise Exceptions::ExportIgnored.new("Ignored")
  rescue Errno::ESRCH
    return false
  end
end

#max_valueObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/myreplicator/export.rb', line 126

def max_value
  sql = SqlCommands.max_value_sql(:incremental_col => self.incremental_column,
                                  :db => self.source_schema,
                                  :table => self.table_name)
  result = exec_on_source(sql)
  if result.first.nil?
    return ""
  else
    case result.first.first.class.to_s
    when "Symbol", "Fixnum"
      return result.first.first.to_s
    else
      return result.first.first.to_s(:db)
    end
  end
end

#scheduleObject

Schedules the export job in Resque



247
248
249
250
251
252
253
254
# File 'app/models/myreplicator/export.rb', line 247

def schedule
  Resque.set_schedule(schedule_name, {
                        :cron => cron,
                        :class => "Myreplicator::Export",
                        :queue => "myreplicator_export",
                        :args => id
                      })
end

#schedule_nameObject

Name used for the job in Resque



240
241
242
# File 'app/models/myreplicator/export.rb', line 240

def schedule_name
  name = "#{source_schema}_#{destination_schema}_#{table_name}"
end

#sftp_to_sourceObject



162
163
164
165
# File 'app/models/myreplicator/export.rb', line 162

def sftp_to_source
  puts "Connecting SFTP..."
  return connection_factory(:sftp)
end

#ssh_to_sourceObject



157
158
159
160
# File 'app/models/myreplicator/export.rb', line 157

def ssh_to_source
  puts "Connecting SSH..."
  return connection_factory(:ssh) 
end

#update_max_val(max_val = nil) ⇒ Object



143
144
145
146
147
148
149
150
# File 'app/models/myreplicator/export.rb', line 143

def update_max_val(max_val = nil)
  if max_val.nil?
    self.max_incremental_value = max_value
  else
    self.max_incremental_value = max_val
    self.save!
  end
end