class RR::ReplicationRunner

This class implements the functionality of the 'replicate' command.

Attributes

options[RW]

Provided options. Possible values:

  • :config_file: path to config file

termination_requested[RW]

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

Public Class Methods

run(args) click to toggle source

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

# 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

Public Instance Methods

clear_session() click to toggle source

Removes current Session.

# File lib/rubyrep/replication_runner.rb, line 84
def clear_session
  @session = nil
end
execute() click to toggle source

Executes an endless loop of replication runs

# 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_once() click to toggle source

Executes a single replication run

# 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_waiter() click to toggle source

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

# 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_replication() click to toggle source

Wait for the next replication time

# 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_replication() click to toggle source

Prepares the replication

# File lib/rubyrep/replication_runner.rb, line 112
def prepare_replication
  initializer = ReplicationInitializer.new session
  initializer.prepare_replication
end
process_options(args) click to toggle source

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

# 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_finished() click to toggle source

For testing: will be called after replication preparation is finished

# File lib/rubyrep/replication_runner.rb, line 132
def replication_preparation_finished
end
replication_run_finished() click to toggle source

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

# File lib/rubyrep/replication_runner.rb, line 136
def replication_run_finished
end
session() click to toggle source

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

# 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