Class: Thor::AppMaster
- Inherits:
-
Application
- Object
- Bsl::Application
- Application
- Thor::AppMaster
- Defined in:
- lib/ThorMaster.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#request_exit ⇒ Object
Returns the value of attribute request_exit.
-
#sql_connection ⇒ Object
Returns the value of attribute sql_connection.
Instance Method Summary collapse
-
#amqp_handle_failure(e) ⇒ Object
Handles failure when connecting to AMQP.
-
#amqp_reset_retry_interval ⇒ Object
Resets internal AMQP connection failure counter/interval.
-
#amqp_start ⇒ Object
Starts AMQP connection.
-
#amqp_stop ⇒ Object
Stops Running AMQP connection.
-
#initialize(opts = {}) ⇒ AppMaster
constructor
A new instance of AppMaster.
-
#main ⇒ Object
Main entry-point.
-
#sql_start ⇒ Object
Connects to SQL.
-
#sql_stop ⇒ Object
Stop SQL connection.
Methods inherited from Application
Constructor Details
#initialize(opts = {}) ⇒ AppMaster
Returns a new instance of AppMaster.
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ThorMaster.rb', line 34 def initialize(opts = {}) super(opts) @@AMQP_DEFAULT_RETRY_INTERVAL = 3 @@AMQP_MAX_RETRY_INTERVAL = (30) @@AMQP_MAX_RETRY_ATTEMPS = -1 @@AMQP_RETRY_MULTIPLER = 1.5 @amqp_retry_interval = @@AMQP_DEFAULT_RETRY_INTERVAL @amqp_retry_attempt = 0 # Signalizes that application wants exit for some reason @request_exit = false # AMQP options @options[:amqp_host] = "localhost" @options[:amqp_port] = 1234 @options[:amqp_user] = "user" @options[:amqp_password] = "password" @options[:amqp_vhost] = "my-vhost" @options[:sql_adapter] = "postgresql" @options[:sql_host] = "host" @options[:sql_port] = 5432 @options[:sql_user] = "user" @options[:sql_password] = "password" @options[:sql_database] = "database" @sql_connection = nil initialize_optparser { |opts| ############################ # AMQP Section ############################ # AMQP Host opts.on( '-H', '--amqp-host STRING', "AMQP Server hostname") do |host| @options[:amqp_host] = host end # AMQP Port opts.on( '-p', '--amqp-port NUM', "AMQP Server port number") do |port| @options[:amqp_port] = port end # AMQP Username opts.on( '-u', '--amqp-user STRING', "AMQP Username") do |user| @options[:amqp_user] = user end # AMQP Password opts.on( '-P', '--amqp-password STRING', "AMQP Password") do |password| @options[:amqp_password] = password end # AMQP Vhost opts.on( '-V', '--amqp-vhost STRING', "AMQP Virtual Host") do |vhost| @options[:amqp_vhost] = vhost end ############################ # Sql section ############################ # SQL Adapter opts.on( '-sA', '--sql-adapter STRING', "SQL Adapter") do |adapter| @options[:sql_adapter] = adapter end # SQL Host opts.on( '-sH', '--sql-host STRING', "SQL Server hostname") do |host| @options[:sql_host] = host end # SQL Port opts.on( '-sp', '--sql-port NUM', "SQL Server port number") do |port| @options[:sql_port] = port end # SQL Username opts.on( '-su', '--sql-user STRING', "SQL Username") do |user| @options[:sql_user] = user end # SQL Password opts.on( '-sP', '--sql-password STRING', "SQL Password") do |password| @options[:sql_password] = password end # SQL Database opts.on( '-sD', '--sql-database STRING', "SQL Database") do |database| @options[:sql_adapter] = database end } end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
32 33 34 |
# File 'lib/ThorMaster.rb', line 32 def @options end |
#request_exit ⇒ Object
Returns the value of attribute request_exit.
32 33 34 |
# File 'lib/ThorMaster.rb', line 32 def request_exit @request_exit end |
#sql_connection ⇒ Object
Returns the value of attribute sql_connection.
32 33 34 |
# File 'lib/ThorMaster.rb', line 32 def sql_connection @sql_connection end |
Instance Method Details
#amqp_handle_failure(e) ⇒ Object
Handles failure when connecting to AMQP
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ThorMaster.rb', line 177 def amqp_handle_failure(e) amqp_stop() max_attempts_reached = false if(@@AMQP_MAX_RETRY_ATTEMPS != nil && @@AMQP_MAX_RETRY_ATTEMPS >= 0) @amqp_retry_attempt = @amqp_retry_attempt + 1 max_attempts_reached = @amqp_retry_attempt > @@AMQP_MAX_RETRY_ATTEMPS end if(max_attempts_reached == false) Bsl::Logger::Log "Unable to connect to AMQP: #{e.to_s}" Bsl::Logger::Log "Next attempt in #{@amqp_retry_interval} sec(s)." sleep (@amqp_retry_interval) @amqp_retry_interval = @amqp_retry_interval * @@AMQP_RETRY_MULTIPLER @amqp_retry_interval = @@AMQP_MAX_RETRY_INTERVAL if @amqp_retry_interval > @@AMQP_MAX_RETRY_INTERVAL else if(@@AMQP_MAX_RETRY_ATTEMPS != nil) Bsl::Logger::Log "Maximum AQMP reconnect attempts limit reached (#{@@AMQP_MAX_RETRY_ATTEMPS}), quitting." end @request_exit = true end end |
#amqp_reset_retry_interval ⇒ Object
Resets internal AMQP connection failure counter/interval
129 130 131 132 |
# File 'lib/ThorMaster.rb', line 129 def amqp_reset_retry_interval @amqp_retry_interval = @@AMQP_DEFAULT_RETRY_INTERVAL @amqp_retry_attempt = 0 end |
#amqp_start ⇒ Object
Starts AMQP connection
135 136 137 138 139 140 |
# File 'lib/ThorMaster.rb', line 135 def amqp_start Bsl::Logger::Log "Starting AMQP - Connecting #{[:amqp_user]}@#{[:amqp_host]}:#{[:amqp_port]}#{[:amqp_vhost]}" AMQP.start(:host => [:amqp_host], :port => [:amqp_port], :vhost => [:amqp_vhost], :user => [:amqp_user], :password => [:amqp_password] ) do amqp_reset_retry_interval() end end |
#amqp_stop ⇒ Object
Stops Running AMQP connection
143 144 145 |
# File 'lib/ThorMaster.rb', line 143 def amqp_stop AMQP.stop { EM.stop } end |
#main ⇒ Object
Main entry-point
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/ThorMaster.rb', line 202 def main super() if(sql_start() == false) Bsl::Logger::Log "Unable to connect to DB, quitting!" exit end # Run loop while exit is not requested while(@request_exit == false) begin amqp_start() rescue Exception => e amqp_handle_failure(e) end end sql_stop() end |
#sql_start ⇒ Object
Connects to SQL
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ThorMaster.rb', line 148 def sql_start Bsl::Logger::Log "Connecting to SQL - Connecting #{[:sql_adapter]}://#{[:sql_user]}@#{[:sql_host]}:#{[:sql_port]}" begin ActiveRecord::Base.establish_connection( { :adapter => [:sql_adapter], :host => [:sql_host], :username => [:sql_user], :password => [:sql_password], :database => [:sql_database] } ) @sql_connection = ActiveRecord::Base.connection rescue Exception => e Bsl::Logger::Log "Could not connect to DB, reason: #{e.}!" return false end return @sql_connection != nil end |
#sql_stop ⇒ Object
Stop SQL connection
172 173 174 |
# File 'lib/ThorMaster.rb', line 172 def sql_stop end |