Class: RR::ReplicationRunner
- Inherits:
-
Object
- Object
- RR::ReplicationRunner
- Defined in:
- lib/rubyrep/replication_runner.rb
Overview
This class implements the functionality of the ‘replicate’ command.
Instance Attribute Summary collapse
-
#options ⇒ Object
Provided options.
-
#termination_requested ⇒ Object
Should be set to
true
if the replication runner should be terminated.
Class Method Summary collapse
-
.run(args) ⇒ Object
Entry points for executing a processing run.
Instance Method Summary collapse
-
#clear_session ⇒ Object
Removes current
Session
. -
#execute ⇒ Object
Executes an endless loop of replication runs.
-
#execute_once ⇒ Object
Executes a single replication run.
-
#init_waiter ⇒ Object
Initializes the waiter thread used for replication pauses and processing the process TERM signal.
-
#pause_replication ⇒ Object
Wait for the next replication time.
-
#prepare_replication ⇒ Object
Prepares the replication.
-
#process_options(args) ⇒ Object
Parses the given command line parameter array.
-
#replication_preparation_finished ⇒ Object
For testing: will be called after replication preparation is finished.
-
#replication_run_finished ⇒ Object
For testing: will be called after a replication run is finished.
-
#session ⇒ Object
Returns the active
Session
.
Instance Attribute Details
#options ⇒ Object
Provided options. Possible values:
-
:config_file
: path to config file
23 24 25 |
# File 'lib/rubyrep/replication_runner.rb', line 23 def @options end |
#termination_requested ⇒ Object
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.(args) if runner. runner.execute end status end |
Instance Method Details
#clear_session ⇒ Object
Removes current Session
.
84 85 86 |
# File 'lib/rubyrep/replication_runner.rb', line 84 def clear_session @session = nil end |
#execute ⇒ Object
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_once ⇒ Object
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.[: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 ⇒ Object
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_replication ⇒ Object
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.[:replication_interval] unless now >= @next_run waiting_time = @next_run - now @waiter_thread.join waiting_time end @last_run = Time.now end |
#prepare_replication ⇒ Object
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 (args) status = 0 self. = {} parser = OptionParser.new do |opts| opts. = <<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| [:config_file] = arg end opts.on_tail("--help", "Show this message") do $stderr.puts opts self. = nil end end begin parser.parse!(args) if # this will be +nil+ if the --help option is specified raise("Please specify configuration file") unless .include?(:config_file) end rescue Exception => e $stderr.puts "Command line parsing failed: #{e}" $stderr.puts parser.help self. = nil status = 1 end return status end |
#replication_preparation_finished ⇒ Object
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_finished ⇒ Object
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 |
#session ⇒ Object
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 [:config_file] @config = Initializer.configuration end @session = Session.new @config end @session end |