Class: Thor::Application

Inherits:
Bsl::Application
  • Object
show all
Defined in:
lib/ThorApplication.rb

Direct Known Subclasses

AppAmqp2Sql, AppClient, AppCmd, AppMaster, AppWorker, Job, Node

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

Instance Method Summary collapse

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
			options[:amqp] = {}
			options[:amqp][:host] = "localhost"
			options[:amqp][:port] = 8467
			options[:amqp][:user] = "guest"
			options[:amqp][:password] = "guest"
			options[: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: #{options[:amqp][:host]}]") do |host|
	options[:amqp][:host] = host
end

# AMQP Port
opts.on('--amqp-port NUM', "AMQP Server port number [default: #{options[:amqp][:port]}]") do |port|
	options[:amqp][:port] = port
end

# AMQP Username
opts.on('--amqp-user STRING', "AMQP Username [default: #{options[:amqp][:user]}]") do |user|
	options[:amqp][:user] = user
end

# AMQP Password
opts.on('--amqp-password STRING', "AMQP Password [default: #{options[:amqp][:password]}]") do |password|
	options[:amqp][:password] = password
end

# AMQP Vhost
opts.on('--amqp-vhost STRING', "AMQP Virtual Host [default: #{options[:amqp][:vhost]}]") do |vhost|
	options[:amqp][:vhost] = vhost
end
			}
end

Instance Attribute Details

#request_exitObject

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_startObject

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 #{options[:amqp]['user']}@#{options[:amqp]['host']}:#{options[:amqp]['port']}#{options[:amqp]['vhost']}"		
	AMQP.start(:host => options[:amqp]['host'], :port => options[:amqp]['port'],  :vhost => options[:amqp]['vhost'], :user => options[:amqp]['user'], :password => options[:amqp]['password'] ) do |amqp|
		Bsl::Logger::Log "Connected to AMQP broker. Running #{AMQP::VERSION} version of the gem..."
		
		amqp_loop(amqp)
	end
end

#amqp_stopObject

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

#mainObject



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 = {})
  options = opts
end