Class: RR::ReplicationRun

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

Overview

Executes a single replication run

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ ReplicationRun

Creates a new ReplicationRun instance.

  • session: the current Session



55
56
57
# File 'lib/rubyrep/replication_run.rb', line 55

def initialize(session)
  self.session = session
end

Instance Attribute Details

#sessionObject

The current Session object



9
10
11
# File 'lib/rubyrep/replication_run.rb', line 9

def session
  @session
end

Instance Method Details

#helperObject

Returns the current ReplicationHelper; creates it if necessary



12
13
14
# File 'lib/rubyrep/replication_run.rb', line 12

def helper
  @helper ||= ReplicationHelper.new(self)
end

#replicatorObject

Returns the current replicator; creates it if necessary.



17
18
19
20
# File 'lib/rubyrep/replication_run.rb', line 17

def replicator
  @replicator ||=
    Replicators.replicators[session.configuration.options[:replicator]].new(helper)
end

#runObject

Executes the replication run.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubyrep/replication_run.rb', line 23

def run
  return unless [:left, :right].any? do |database|
    Timeout::timeout(session.configuration.options[:database_connection_timeout]) do
      session.send(database).select_one(
        "select id from #{session.configuration.options[:rep_prefix]}_pending_changes"
      ) != nil
    end
  end
  begin
    success = false
    replicator # ensure that replicator is created and has chance to validate settings

    loop do
      begin
        session.reload_changes # ensure the cache of change log records is up-to-date
        diff = ReplicationDifference.new session
        diff.load
        break unless diff.loaded?
        replicator.replicate_difference diff if diff.type != :no_diff
      rescue Exception => e
        helper.log_replication_outcome diff, e.message,
          e.class.to_s + "\n" + e.backtrace.join("\n")
      end
    end
    success = true # considered to be successful if we get till here
  ensure
    helper.finalize success
  end
end