Class: Thor::Application
- Inherits:
-
Bsl::Application
- Object
- Bsl::Application
- Thor::Application
- Defined in:
- lib/ThorApplication.rb
Constant Summary collapse
- @@AMQP_DEFAULT_RETRY_INTERVAL =
3
- @@AMQP_MAX_RETRY_INTERVAL =
(30)
- @@AMQP_MAX_RETRY_ATTEMPS =
-1
- @@AMQP_RETRY_MULTIPLER =
1.5
Instance Attribute Summary collapse
-
#request_exit ⇒ Object
Returns the value of attribute request_exit.
Instance Method Summary collapse
- #amqp_handle_failure(e) ⇒ Object
- #amqp_loop(amqp) ⇒ Object
-
#amqp_start ⇒ Object
Starts AMQP connection.
-
#amqp_stop ⇒ Object
Stops Running AMQP connection.
-
#initialize(opts = {}) ⇒ Application
constructor
A new instance of Application.
- #main ⇒ Object
- #run(opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Application
Returns a new instance of Application.
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 69 70 71 72 73 74 |
# File 'lib/ThorApplication.rb', line 31 def initialize(opts = {}) super(opts) # AMQP options [:amqp] = {} [:amqp][:host] = "localhost" [:amqp][:port] = 8467 [:amqp][:user] = "guest" [:amqp][:password] = "guest" [:amqp][:vhost] = "/" @amqp_retry_interval = @@AMQP_DEFAULT_RETRY_INTERVAL @amqp_retry_attempt = 0 # Signalizes that application wants exit for some reason @request_exit = false initialize_optparser { |opts| # AMQP Host opts.on( '--amqp-host STRING', "AMQP Server hostname [default: #{[:amqp][:host]}]") do |host| [:amqp][:host] = host end # AMQP Port opts.on('--amqp-port NUM', "AMQP Server port number [default: #{[:amqp][:port]}]") do |port| [:amqp][:port] = port end # AMQP Username opts.on('--amqp-user STRING', "AMQP Username [default: #{[:amqp][:user]}]") do |user| [:amqp][:user] = user end # AMQP Password opts.on('--amqp-password STRING', "AMQP Password [default: #{[:amqp][:password]}]") do |password| [:amqp][:password] = password end # AMQP Vhost opts.on('--amqp-vhost STRING', "AMQP Virtual Host [default: #{[:amqp][:vhost]}]") do |vhost| [:amqp][:vhost] = vhost end } end |
Instance Attribute Details
#request_exit ⇒ Object
Returns the value of attribute request_exit.
24 25 26 |
# File 'lib/ThorApplication.rb', line 24 def request_exit @request_exit end |
Instance Method Details
#amqp_handle_failure(e) ⇒ Object
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 |
# File 'lib/ThorApplication.rb', line 96 def amqp_handle_failure(e) amqp_stop() Bsl::Logger::Log "AMQP Failure, reason: '#{e.inspect}'." if(@request_exit == true) return false end 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 "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 return true end |
#amqp_loop(amqp) ⇒ Object
76 77 78 |
# File 'lib/ThorApplication.rb', line 76 def amqp_loop(amqp) end |
#amqp_start ⇒ Object
Starts AMQP connection
81 82 83 84 85 86 87 88 |
# File 'lib/ThorApplication.rb', line 81 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| Bsl::Logger::Log "Connected to AMQP broker. Running #{AMQP::VERSION} version of the gem..." amqp_loop(amqp) end end |
#amqp_stop ⇒ Object
Stops Running AMQP connection
91 92 93 94 |
# File 'lib/ThorApplication.rb', line 91 def amqp_stop Bsl::Logger::Log "Stopping AMQP" AMQP.stop { EM.stop } end |
#main ⇒ Object
131 132 133 |
# File 'lib/ThorApplication.rb', line 131 def main super() end |
#run(opts = {}) ⇒ Object
127 128 129 |
# File 'lib/ThorApplication.rb', line 127 def run(opts = {}) = opts end |