Class: RR::ReplicationRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/replication_runner.rb

Overview

This class implements the functionality of the ‘replicate’ command.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Provided options. Possible values:

  • :config_file: path to config file



23
24
25
# File 'lib/rubyrep/replication_runner.rb', line 23

def options
  @options
end

#termination_requestedObject

Should be set to true if the replication runner should be terminated.



26
27
28
# File 'lib/rubyrep/replication_runner.rb', line 26

def termination_requested
  @termination_requested
end

Class Method Details

.run(args) ⇒ Object

Entry points for executing a processing run. args: the array of command line options that were provided by the user.



163
164
165
166
167
168
169
170
171
# File 'lib/rubyrep/replication_runner.rb', line 163

def self.run(args)
  runner = new

  status = runner.process_options(args)
  if runner.options
    runner.execute
  end
  status
end

Instance Method Details

#clear_sessionObject

Removes current Session.



84
85
86
# File 'lib/rubyrep/replication_runner.rb', line 84

def clear_session
  @session = nil
end

#executeObject

Executes an endless loop of replication runs



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rubyrep/replication_runner.rb', line 140

def execute
  init_waiter
  prepare_replication
  replication_preparation_finished

  until termination_requested do
    begin
      execute_once
    rescue Exception => e
      now = Time.now.iso8601
      $stderr.puts "#{now} Exception caught: #{e}"
      if @last_exception_message != e.to_s # only print backtrace if something changed
        @last_exception_message = e.to_s
        $stderr.puts e.backtrace.map {|line| line.gsub(/^/, "#{' ' * now.length} ")}
      end
    end
    pause_replication
    replication_run_finished
  end
end

#execute_onceObject

Executes a single replication run



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rubyrep/replication_runner.rb', line 118

def execute_once
  session.refresh
  timeout = session.configuration.options[:database_connection_timeout]
  terminated = TaskSweeper.timeout(timeout) do |sweeper|
    run = ReplicationRun.new session, sweeper
    run.run
  end.terminated?
  raise "replication run timed out" if terminated
rescue Exception => e
  clear_session
  raise e
end

#init_waiterObject

Initializes the waiter thread used for replication pauses and processing the process TERM signal.



102
103
104
105
106
107
108
109
# File 'lib/rubyrep/replication_runner.rb', line 102

def init_waiter
  @termination_mutex = Monitor.new
  @termination_mutex.lock
  @waiter_thread ||= Thread.new {@termination_mutex.lock; self.termination_requested = true}
  %w(TERM INT).each do |signal|
    Signal.trap(signal) {puts "\nCaught '#{signal}': Initiating graceful shutdown"; @termination_mutex.unlock}
  end
end

#pause_replicationObject

Wait for the next replication time



89
90
91
92
93
94
95
96
97
98
# File 'lib/rubyrep/replication_runner.rb', line 89

def pause_replication
  @last_run ||= 1.year.ago
  now = Time.now
  @next_run = @last_run + session.configuration.options[:replication_interval]
  unless now >= @next_run
    waiting_time = @next_run - now
    @waiter_thread.join waiting_time
  end
  @last_run = Time.now
end

#prepare_replicationObject

Prepares the replication



112
113
114
115
# File 'lib/rubyrep/replication_runner.rb', line 112

def prepare_replication
  initializer = ReplicationInitializer.new session
  initializer.prepare_replication
end

#process_options(args) ⇒ Object

Parses the given command line parameter array. Returns the status (as per UNIX conventions: 1 if parameters were invalid, 0 otherwise)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubyrep/replication_runner.rb', line 31

def process_options(args)
  status = 0
  self.options = {}

  parser = OptionParser.new do |opts|
    opts.banner = <<EOS
Usage: #{$0} replicate [options]

  Replicates two databases as per specified configuration file.
EOS
    opts.separator ""
    opts.separator "  Specific options:"

    opts.on("-c", "--config", "=CONFIG_FILE",
      "Mandatory. Path to configuration file.") do |arg|
      options[:config_file] = arg
    end

    opts.on_tail("--help", "Show this message") do
      $stderr.puts opts
      self.options = nil
    end
  end

  begin
    parser.parse!(args)
    if options # this will be +nil+ if the --help option is specified
      raise("Please specify configuration file") unless options.include?(:config_file)
    end
  rescue Exception => e
    $stderr.puts "Command line parsing failed: #{e}"
    $stderr.puts parser.help
    self.options = nil
    status = 1
  end

  return status
end

#replication_preparation_finishedObject

For testing: will be called after replication preparation is finished



132
133
# File 'lib/rubyrep/replication_runner.rb', line 132

def replication_preparation_finished
end

#replication_run_finishedObject

For testing: will be called after a replication run is finished



136
137
# File 'lib/rubyrep/replication_runner.rb', line 136

def replication_run_finished
end

#sessionObject

Returns the active Session. Loads config file and creates session if necessary.



72
73
74
75
76
77
78
79
80
81
# File 'lib/rubyrep/replication_runner.rb', line 72

def session
  unless @session
    unless @config
      load options[:config_file]
      @config = Initializer.configuration
    end
    @session = Session.new @config
  end
  @session
end