Class: Myreplicator::Export
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Myreplicator::Export
- Defined in:
- app/models/myreplicator/export.rb
Defined Under Namespace
Classes: SourceDb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
-
.available_dbs ⇒ Object
List of all avaiable databases from database.yml file All Export/Load jobs can use these databases.
-
.available_tables ⇒ Object
Returns a hash of => [TableName1,…], DB => ….
-
.perform(export_id, *args) ⇒ Object
Perfoms the export job, Provided for Resque.
-
.schedule_in_resque ⇒ Object
NOTE: Provided for Resque use Schedules all the exports in resque Requires Resque Scheduler.
Instance Method Summary collapse
-
#connection_factory(type) ⇒ Object
Connects to the server via ssh/sftp.
- #exec_on_source(sql) ⇒ Object
-
#export ⇒ Object
Runs the export process using the required Exporter library.
- #export_type? ⇒ Boolean
- #incremental_export? ⇒ Boolean
-
#is_running? ⇒ Boolean
Throws ExportIgnored if the job is still running Checks the state of the job using PID and state.
- #max_value ⇒ Object
-
#schedule ⇒ Object
Schedules the export job in Resque.
-
#schedule_name ⇒ Object
Name used for the job in Resque.
- #sftp_to_source ⇒ Object
- #ssh_to_source ⇒ Object
- #update_max_val(max_val = nil) ⇒ Object
Instance Attribute Details
#filename ⇒ Object (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_dbs ⇒ Object
List of all avaiable databases from database.yml file All Export/Load jobs can use these databases
148 149 150 151 152 153 154 |
# File 'app/models/myreplicator/export.rb', line 148 def self.available_dbs dbs = ActiveRecord::Base.configurations.keys dbs.delete("development") dbs.delete("production") dbs.delete("test") return dbs end |
.available_tables ⇒ Object
Returns a hash of => [TableName1,…], DB => …
135 136 137 138 139 140 141 142 |
# File 'app/models/myreplicator/export.rb', line 135 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 |
# File 'app/models/myreplicator/export.rb', line 37 def self.perform(export_id, *args) = args. ActiveRecord::Base.verify_active_connections! ActiveRecord::Base.connection.reconnect! export_obj = Export.find(export_id) export_obj.export end |
.schedule_in_resque ⇒ Object
NOTE: Provided for Resque use Schedules all the exports in resque Requires Resque Scheduler
161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/models/myreplicator/export.rb', line 161 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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/myreplicator/export.rb', line 111 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 |
#exec_on_source(sql) ⇒ Object
93 94 95 96 |
# File 'app/models/myreplicator/export.rb', line 93 def exec_on_source sql result = SourceDb.exec_sql(self.source_schema, sql) return result end |
#export ⇒ Object
Runs the export process using the required Exporter library
48 49 50 51 52 53 54 |
# File 'app/models/myreplicator/export.rb', line 48 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
56 57 58 59 60 61 62 |
# File 'app/models/myreplicator/export.rb', line 56 def export_type? if state == "new" return :new elsif incremental_export? return :incremental end end |
#incremental_export? ⇒ Boolean
64 65 66 67 68 69 |
# File 'app/models/myreplicator/export.rb', line 64 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
196 197 198 199 200 201 202 203 204 |
# File 'app/models/myreplicator/export.rb', line 196 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_value ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'app/models/myreplicator/export.rb', line 75 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) return result.first.first.to_s(:db) end |
#schedule ⇒ Object
Schedules the export job in Resque
183 184 185 186 187 188 189 190 |
# File 'app/models/myreplicator/export.rb', line 183 def schedule Resque.set_schedule(schedule_name, { :cron => cron, :class => "Myreplicator::Export", :queue => "myreplicator_export", :args => id }) end |
#schedule_name ⇒ Object
Name used for the job in Resque
176 177 178 |
# File 'app/models/myreplicator/export.rb', line 176 def schedule_name name = "#{source_schema}_#{destination_schema}_#{table_name}" end |
#sftp_to_source ⇒ Object
103 104 105 106 |
# File 'app/models/myreplicator/export.rb', line 103 def sftp_to_source puts "Connecting SFTP..." return connection_factory(:sftp) end |
#ssh_to_source ⇒ Object
98 99 100 101 |
# File 'app/models/myreplicator/export.rb', line 98 def ssh_to_source puts "Connecting SSH..." return connection_factory(:ssh) end |
#update_max_val(max_val = nil) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'app/models/myreplicator/export.rb', line 84 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 |