app/models/myreplicator/export.rb in myreplicator-0.0.16 vs app/models/myreplicator/export.rb in myreplicator-0.0.17

- old
+ new

@@ -26,12 +26,26 @@ :transporter_pid, :loader_pid ) attr_reader :filename - - def export + + @queue = :myreplicator_export # Provided for Resque + + ## + # Perfoms the export job, Provided for Resque + ## + def self.perform(export_id, *args) + options = args.extract_options! + export_obj = Export.find(export_id) + export_obj.export_table + end + + ## + # Runs the export process using the required Exporter library + ## + def export_table exporter = MysqlExporter.new exporter.export_table self end def filename @@ -68,48 +82,119 @@ def sftp_to_source puts "Connecting SFTP..." return connection_factory(:sftp) end - + + ## + # Connects to the server via ssh/sftp + ## def connection_factory type + config = Myreplicator.configs[self.source_schema] + case type when :ssh - if Myreplicator.configs[self.source_schema].has_key? "ssh_password" - return Net::SSH.start(Myreplicator.configs[self.source_schema]["ssh_host"], - Myreplicator.configs[self.source_schema]["ssh_user"], - :password => Myreplicator.configs[self.source_schema]["ssh_password"]) + if config.has_key? "ssh_password" + return Net::SSH.start(config["ssh_host"], config["ssh_user"], :password => config["ssh_password"]) - elsif(Myreplicator.configs[self.source_schema].has_key? "ssh_private_key") - return Net::SSH.start(Myreplicator.configs[self.source_schema]["ssh_host"], - Myreplicator.configs[self.source_schema]["ssh_user"], - :keys => [Myreplicator.configs[self.source_schema]["ssh_private_key"]]) + 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 Myreplicator.configs[self.source_schema].has_key? "ssh_password" - return Net::SFTP.start(Myreplicator.configs[self.source_schema]["ssh_host"], - Myreplicator.configs[self.source_schema]["ssh_user"], - :password => Myreplicator.configs[self.source_schema]["ssh_password"]) + if config.has_key? "ssh_password" + return Net::SFTP.start(config["ssh_host"], config["ssh_user"], :password => config["ssh_password"]) - elsif(Myreplicator.configs[self.source_schema].has_key? "ssh_private_key") - return Net::SFTP.start(Myreplicator.configs[self.source_schema]["ssh_host"], - Myreplicator.configs[self.source_schema]["ssh_user"], - :keys => [Myreplicator.configs[self.source_schema]["ssh_private_key"]]) + 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 ## + # Returns a hash of {DB_NAME => [TableName1,...], DB => ...} + ## + def self.available_tables + metadata = {} + available_dbs.each do |db| + tables = SourceDb.get_tables(db) + metadata[db] = tables + end + return metadata + end + + ## + # List of all avaiable databases from database.yml file + # All Export/Load jobs can use these databases + ## + def self.available_dbs + dbs = ActiveRecord::Base.configurations.keys + dbs.delete("development") + dbs.delete("test") + return dbs + end + + ## + # NOTE: Provided for Resque use + # Schedules all the exports in resque + # Requires Resque Scheduler + ## + 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 + + ## + # Name used for the job in Resque + ## + def schedule_name + name = "#{source_schema}_#{destination_schema}_#{table_name}" + end + + ## + # Schedules the export job in Resque + ## + def schedule + Resque.set_schedule(schedule_name, { + :cron => cron, + :class => "Myreplicator::Export", + :queue => "myreplicator_export", + :args => id + }) + end + + ## # Inner Class that connects to the source database # Handles connecting to multiple databases ## class SourceDb < ActiveRecord::Base def self.connect db - @@connected ||= true establish_connection(ActiveRecord::Base.configurations[db]) - Kernel.p ActiveRecord::Base.connected? + end + + ## + # Returns tables as an Array + # releases the connection + ## + def self.get_tables(db) + tables = [] + begin + self.connect(db) + tables = self.connection.tables + self.connection_pool.release_connection + rescue Mysql2::Error => e + puts "Connection to #{db} Failed!" + puts e.message + end + return tables end def self.exec_sql source_db,sql SourceDb.connect(source_db) return SourceDb.connection.execute(sql)