Class: Tapsoob::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/tapsoob/operation.rb

Direct Known Subclasses

Pull, Push

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_url, dump_path = nil, opts = {}) ⇒ Operation

Returns a new instance of Operation.



15
16
17
18
19
20
# File 'lib/tapsoob/operation.rb', line 15

def initialize(database_url, dump_path = nil, opts={})
  @database_url = database_url
  @dump_path    = dump_path
  @opts         = opts
  @exiting      = false
end

Instance Attribute Details

#database_urlObject (readonly)

Returns the value of attribute database_url.



13
14
15
# File 'lib/tapsoob/operation.rb', line 13

def database_url
  @database_url
end

#dump_pathObject (readonly)

Returns the value of attribute dump_path.



13
14
15
# File 'lib/tapsoob/operation.rb', line 13

def dump_path
  @dump_path
end

#optsObject (readonly)

Returns the value of attribute opts.



13
14
15
# File 'lib/tapsoob/operation.rb', line 13

def opts
  @opts
end

Class Method Details

.factory(type, database_url, dump_path, opts) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/tapsoob/operation.rb', line 165

def self.factory(type, database_url, dump_path, opts)
  type = :resume if opts[:resume]
  klass = case type
    when :pull   then Tapsoob::Pull
    when :push   then Tapsoob::Push
    when :resume then eval(opts[:klass])
    else raise "Unknown Operation Type -> #{type}"
  end

  klass.new(database_url, dump_path, opts)
end

Instance Method Details

#add_completed_table(table_name) ⇒ Object



147
148
149
150
151
# File 'lib/tapsoob/operation.rb', line 147

def add_completed_table(table_name)
  completed_tables_mutex.synchronize do
    completed_tables << table_name.to_s
  end
end

#apply_table_filter(tables) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tapsoob/operation.rb', line 46

def apply_table_filter(tables)
  return tables if table_filter.empty? && exclude_tables.empty?

  if tables.kind_of?(Hash)
    ntables = {}
    tables.each do |t, d|
      if !exclude_tables.include?(t.to_s) && (!table_filter.empty? && table_filter.include?(t.to_s))
        ntables[t] = d
      end
    end
    ntables
  else
    tables.reject { |t| exclude_tables.include?(t.to_s) }.select { |t| table_filter.include?(t.to_s) }
  end
end

#catch_errors(&blk) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/tapsoob/operation.rb', line 157

def catch_errors(&blk)
  begin
    blk.call
  rescue Exception => e
    raise e
  end
end

#completed_tablesObject



109
110
111
# File 'lib/tapsoob/operation.rb', line 109

def completed_tables
  opts[:completed_tables] ||= []
end

#completed_tables_mutexObject



143
144
145
# File 'lib/tapsoob/operation.rb', line 143

def completed_tables_mutex
  @completed_tables_mutex ||= Mutex.new
end

#data?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/tapsoob/operation.rb', line 26

def data?
  opts[:data]
end

#dbObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/tapsoob/operation.rb', line 121

def db
  @db ||= Sequel.connect(database_url, max_connections: parallel_workers * 2)
  @db.extension :schema_dumper
  @db.loggers << Tapsoob.log if opts[:debug]

  # Set parameters
  if @db.uri =~ /oracle/i
    @db << "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'"
    @db << "ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS:FF6'"
  end

  @db
end

#default_chunksizeObject



105
106
107
# File 'lib/tapsoob/operation.rb', line 105

def default_chunksize
  opts[:default_chunksize]
end

#exclude_tablesObject



42
43
44
# File 'lib/tapsoob/operation.rb', line 42

def exclude_tables
  opts[:exclude_tables] || []
end

#exiting?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/tapsoob/operation.rb', line 85

def exiting?
  !!@exiting
end

#file_prefixObject



22
23
24
# File 'lib/tapsoob/operation.rb', line 22

def file_prefix
  "op"
end

#format_number(num) ⇒ Object



153
154
155
# File 'lib/tapsoob/operation.rb', line 153

def format_number(num)
  num.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
end

#indexes_first?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/tapsoob/operation.rb', line 34

def indexes_first?
  !!opts[:indexes_first]
end

#logObject



62
63
64
65
# File 'lib/tapsoob/operation.rb', line 62

def log
  Tapsoob.log.level = Logger::DEBUG if opts[:debug]
  Tapsoob.log
end

#parallel?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/tapsoob/operation.rb', line 135

def parallel?
  parallel_workers > 1
end

#parallel_workersObject



139
140
141
# File 'lib/tapsoob/operation.rb', line 139

def parallel_workers
  @parallel_workers ||= [opts[:parallel].to_i, 1].max
end

#resuming?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/tapsoob/operation.rb', line 101

def resuming?
  opts[:resume] == true
end

#schema?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tapsoob/operation.rb', line 30

def schema?
  opts[:schema]
end

#setup_signal_trapObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tapsoob/operation.rb', line 89

def setup_signal_trap
  trap("INT") {
    puts "\nCompleting current action..."
    @exiting = true
  }

  trap("TERM") {
    puts "\nCompleting current action..."
    @exiting = true
  }
end

#store_sessionObject



67
68
69
70
71
72
73
# File 'lib/tapsoob/operation.rb', line 67

def store_session
  file = "#{file_prefix}_#{Time.now.strftime("%Y%m%d%H%M")}.dat"
  log.info "\nSaving session to #{file}..."
  File.open(file, 'w') do |f|
    f.write(JSON.generate(to_hash))
  end
end

#stream_stateObject



113
114
115
# File 'lib/tapsoob/operation.rb', line 113

def stream_state
  opts[:stream_state] ||= {}
end

#stream_state=(val) ⇒ Object



117
118
119
# File 'lib/tapsoob/operation.rb', line 117

def stream_state=(val)
  opts[:stream_state] = val
end

#table_filterObject



38
39
40
# File 'lib/tapsoob/operation.rb', line 38

def table_filter
  opts[:tables] || []
end

#to_hashObject



75
76
77
78
79
80
81
82
83
# File 'lib/tapsoob/operation.rb', line 75

def to_hash
  {
    :klass            => self.class.to_s,
    :database_url     => database_url,
    :stream_state     => stream_state,
    :completed_tables => completed_tables,
    :table_filter     => table_filter,
  }
end