Module: Brown::ModuleMethods

Includes:
Logger
Included in:
Brown
Defined in:
lib/brown/module_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#backtrace, #logger

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/brown/module_methods.rb', line 14

def connection
  @connection
end

#log_levelObject (readonly)

Returns the value of attribute log_level.



14
15
16
# File 'lib/brown/module_methods.rb', line 14

def log_level
  @log_level
end

Instance Method Details

#compile_aclsObject



16
17
18
19
# File 'lib/brown/module_methods.rb', line 16

def compile_acls
	@compiler = ACLCompiler.new
	@compiler.compile
end

#configObject



21
22
23
# File 'lib/brown/module_methods.rb', line 21

def config
	Hash.new { |h,k| h[k] = Hash.new { |h,k| h[k] = "" } }
end

#running?Boolean



25
26
27
# File 'lib/brown/module_methods.rb', line 25

def running?
	EM.reactor_running?
end

#shutdown_hook(&block) ⇒ Object



74
75
76
# File 'lib/brown/module_methods.rb', line 74

def shutdown_hook(&block)
	EM.add_shutdown_hook(&block)
end

#start(opts = {}) ⇒ Object



29
30
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
# File 'lib/brown/module_methods.rb', line 29

def start(opts={})
	@log_level = opts[:log_level] || "info"

	# Why these are not the defaults, I will never know
	EM.epoll if EM.epoll?
	EM.kqueue if EM.kqueue?

	connection_settings = {
	  :on_tcp_connection_failure          => method(:tcp_connection_failure_handler),
	  :on_possible_authentication_failure => method(:authentication_failure_handler)
	}

	AMQP.start(opts[:server_url], connection_settings) do |connection|
		EM.threadpool_size = 1
		@connection = connection

		connection.on_connection do
			logger.info { "Connected to: AMQP Broker: #{broker_identifier(connection)}" }
		end

		connection.on_tcp_connection_loss do |connection, settings|
			logger.info { "Reconnecting to AMQP Broker: #{broker_identifier(connection)} in 5s" }
			connection.reconnect(false, 5)
		end

		connection.after_recovery do |connection|
			logger.info { "Connection with AMQP Broker restored: #{broker_identifier(connection)}" }
		end

		connection.on_error do |connection, connection_close|
			# If the broker is gracefully shutdown we get a 320. Log a nice message.
			if connection_close.reply_code == 320
				logger.info { "AMQP Broker shutdown: #{broker_identifier(connection)}" }
			else
				logger.warn { connection_close.reply_text }
			end
		end

		# This will be the last thing run by the reactor.
		shutdown_hook { logger.debug { "Reactor Stopped" } }

		yield if block_given?
	end
end

#stop(immediately = false, &blk) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/brown/module_methods.rb', line 78

def stop(immediately=false, &blk)
	shutdown_hook(&blk) if blk

	if running?
		if immediately
			EM.next_tick do
				@connection.close { EM.stop_event_loop }
			end
		else
			EM.add_timer(1) do
				@connection.close { EM.stop_event_loop }
			end
		end
	else
		logger.fatal { "Eventmachine is not running, exiting with prejudice" }
		exit!
	end
end