Class: Taps::Operation
- Inherits:
-
Object
- Object
- Taps::Operation
- Defined in:
- lib/taps/operation.rb
Instance Attribute Summary collapse
-
#database_url ⇒ Object
readonly
Returns the value of attribute database_url.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#remote_url ⇒ Object
readonly
Returns the value of attribute remote_url.
-
#session_uri ⇒ Object
readonly
Returns the value of attribute session_uri.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_table_filter(tables) ⇒ Object
- #catch_errors(&blk) ⇒ Object
- #close_session ⇒ Object
- #completed_tables ⇒ Object
- #compression_disabled? ⇒ Boolean
- #db ⇒ Object
- #default_chunksize ⇒ Object
- #exiting? ⇒ Boolean
- #file_prefix ⇒ Object
- #format_number(num) ⇒ Object
- #http_headers(extra = {}) ⇒ Object
- #indexes_first? ⇒ Boolean
-
#initialize(database_url, remote_url, opts = {}) ⇒ Operation
constructor
A new instance of Operation.
- #log ⇒ Object
- #resuming? ⇒ Boolean
- #safe_database_url ⇒ Object
- #safe_remote_url ⇒ Object
- #safe_url(url) ⇒ Object
- #server ⇒ Object
- #session_resource ⇒ Object
- #set_session(uri) ⇒ Object
- #setup_signal_trap ⇒ Object
- #store_session ⇒ Object
- #stream_state ⇒ Object
- #stream_state=(val) ⇒ Object
- #table_filter ⇒ Object
- #to_hash ⇒ Object
- #verify_server ⇒ Object
Constructor Details
#initialize(database_url, remote_url, opts = {}) ⇒ Operation
Returns a new instance of Operation.
20 21 22 23 24 25 26 |
# File 'lib/taps/operation.rb', line 20 def initialize(database_url, remote_url, opts={}) @database_url = database_url @remote_url = remote_url @opts = opts @exiting = false @session_uri = opts[:session_uri] end |
Instance Attribute Details
#database_url ⇒ Object (readonly)
Returns the value of attribute database_url.
17 18 19 |
# File 'lib/taps/operation.rb', line 17 def database_url @database_url end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
17 18 19 |
# File 'lib/taps/operation.rb', line 17 def opts @opts end |
#remote_url ⇒ Object (readonly)
Returns the value of attribute remote_url.
17 18 19 |
# File 'lib/taps/operation.rb', line 17 def remote_url @remote_url end |
#session_uri ⇒ Object (readonly)
Returns the value of attribute session_uri.
18 19 20 |
# File 'lib/taps/operation.rb', line 18 def session_uri @session_uri end |
Class Method Details
.factory(type, database_url, remote_url, opts) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/taps/operation.rb', line 214 def self.factory(type, database_url, remote_url, opts) type = :resume if opts[:resume] klass = case type when :pull then Taps::Pull when :push then Taps::Push when :resume then eval(opts[:klass]) else raise "Unknown Operation Type -> #{type}" end klass.new(database_url, remote_url, opts) end |
Instance Method Details
#apply_table_filter(tables) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/taps/operation.rb', line 40 def apply_table_filter(tables) return tables unless table_filter re = Regexp.new(table_filter) if tables.kind_of?(Hash) ntables = {} tables.each do |t, d| unless re.match(t.to_s).nil? ntables[t] = d end end ntables else tables.reject { |t| re.match(t.to_s).nil? } end end |
#catch_errors(&blk) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/taps/operation.rb', line 190 def catch_errors(&blk) verify_server begin blk.call close_session rescue RestClient::Exception, Taps::BaseError => e store_session if e.kind_of?(Taps::BaseError) puts "!!! Caught Server Exception" puts "#{e.class}: #{e.}" puts "\n#{e.original_backtrace}" if e.original_backtrace exit(1) elsif e.respond_to?(:response) puts "!!! Caught Server Exception" puts "HTTP CODE: #{e.http_code}" puts "#{e.response.to_s}" exit(1) else raise end end end |
#close_session ⇒ Object
140 141 142 |
# File 'lib/taps/operation.rb', line 140 def close_session @session_resource.delete(http_headers) if @session_resource end |
#completed_tables ⇒ Object
104 105 106 |
# File 'lib/taps/operation.rb', line 104 def completed_tables opts[:completed_tables] ||= [] end |
#compression_disabled? ⇒ Boolean
116 117 118 |
# File 'lib/taps/operation.rb', line 116 def compression_disabled? !!opts[:disable_compression] end |
#db ⇒ Object
120 121 122 |
# File 'lib/taps/operation.rb', line 120 def db @db ||= Sequel.connect(database_url) end |
#default_chunksize ⇒ Object
100 101 102 |
# File 'lib/taps/operation.rb', line 100 def default_chunksize opts[:default_chunksize] end |
#exiting? ⇒ Boolean
80 81 82 |
# File 'lib/taps/operation.rb', line 80 def exiting? !!@exiting end |
#file_prefix ⇒ Object
28 29 30 |
# File 'lib/taps/operation.rb', line 28 def file_prefix "op" end |
#format_number(num) ⇒ Object
166 167 168 |
# File 'lib/taps/operation.rb', line 166 def format_number(num) num.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,") end |
#http_headers(extra = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/taps/operation.rb', line 156 def http_headers(extra = {}) base = { :taps_version => Taps.version } if compression_disabled? base[:accept_encoding] = "" else base[:accept_encoding] = "gzip, deflate" end base.merge(extra) end |
#indexes_first? ⇒ Boolean
32 33 34 |
# File 'lib/taps/operation.rb', line 32 def indexes_first? !!opts[:indexes_first] end |
#resuming? ⇒ Boolean
96 97 98 |
# File 'lib/taps/operation.rb', line 96 def resuming? opts[:resume] == true end |
#safe_database_url ⇒ Object
152 153 154 |
# File 'lib/taps/operation.rb', line 152 def safe_database_url safe_url(database_url) end |
#safe_remote_url ⇒ Object
148 149 150 |
# File 'lib/taps/operation.rb', line 148 def safe_remote_url safe_url(remote_url) end |
#safe_url(url) ⇒ Object
144 145 146 |
# File 'lib/taps/operation.rb', line 144 def safe_url(url) url.sub(/\/\/(.+?)?:(.*?)@/, '//\1:[hidden]@') end |
#server ⇒ Object
124 125 126 |
# File 'lib/taps/operation.rb', line 124 def server @server ||= RestClient::Resource.new(remote_url) end |
#session_resource ⇒ Object
128 129 130 131 132 133 |
# File 'lib/taps/operation.rb', line 128 def session_resource @session_resource ||= begin @session_uri ||= server['sessions'].post('', http_headers).to_s server[@session_uri] end end |
#set_session(uri) ⇒ Object
135 136 137 138 |
# File 'lib/taps/operation.rb', line 135 def set_session(uri) session_uri = uri @session_resource = server[session_uri] end |
#setup_signal_trap ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/taps/operation.rb', line 84 def setup_signal_trap trap("INT") { puts "\nCompleting current action..." @exiting = true } trap("TERM") { puts "\nCompleting current action..." @exiting = true } end |
#store_session ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/taps/operation.rb', line 60 def store_session file = "#{file_prefix}_#{Time.now.strftime("%Y%m%d%H%M")}.dat" puts "\nSaving session to #{file}.." File.open(file, 'w') do |f| f.write(to_hash.to_json) end end |
#stream_state ⇒ Object
108 109 110 |
# File 'lib/taps/operation.rb', line 108 def stream_state opts[:stream_state] ||= {} end |
#stream_state=(val) ⇒ Object
112 113 114 |
# File 'lib/taps/operation.rb', line 112 def stream_state=(val) opts[:stream_state] = val end |
#table_filter ⇒ Object
36 37 38 |
# File 'lib/taps/operation.rb', line 36 def table_filter opts[:table_filter] end |
#to_hash ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/taps/operation.rb', line 68 def to_hash { :klass => self.class.to_s, :database_url => database_url, :remote_url => remote_url, :session_uri => session_uri, :stream_state => stream_state, :completed_tables => completed_tables, :table_filter => table_filter, } end |
#verify_server ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/taps/operation.rb', line 170 def verify_server begin server['/'].get(http_headers) rescue RestClient::RequestFailed => e if e.http_code == 417 puts "#{safe_remote_url} is running a different minor version of taps." puts "#{e.response.to_s}" exit(1) else raise end rescue RestClient::Unauthorized puts "Bad credentials given for #{safe_remote_url}" exit(1) rescue Errno::ECONNREFUSED puts "Can't connect to #{safe_remote_url}. Please check that it's running" exit(1) end end |