eventbus 0.24 → 0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/eventbus/common_init.rb +50 -0
- data/lib/eventbus/connectors/stomp/slogger.rb +303 -0
- data/lib/eventbus/connectors/stomp.rb +59 -18
- data/lib/eventbus/message.rb +1 -0
- data/lib/eventbus/queue.rb +6 -2
- data/lib/eventbus/service.rb +27 -13
- metadata +8 -23
- data/lib/eventbus/connectors/bunny.rb +0 -71
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
|
3
|
+
module EventBus
|
4
|
+
|
5
|
+
def self.set_up_logging
|
6
|
+
base_logdir = ENV['EVENTBUS_LOGDIR']
|
7
|
+
|
8
|
+
if base_logdir.nil?
|
9
|
+
base_logdir = File.join(ENV['HOME'], "logs", self.PROD_LEVEL, "eventbus", self.application_id)
|
10
|
+
end
|
11
|
+
|
12
|
+
FileUtils.mkdir_p(base_logdir) unless File.directory?(base_logdir)
|
13
|
+
|
14
|
+
filename = [File.basename($PROGRAM_NAME, File.extname($PROGRAM_NAME)), ".log"].join
|
15
|
+
logfile_path = File.join(base_logdir, filename)
|
16
|
+
|
17
|
+
shift_age = ENV['EVENTBUS_LOG_SHIFT_AGE'] || "daily"
|
18
|
+
shift_size = ENV['EVENTBUS_LOG_SHIFT_SIZE'] || 20971520 # 20MB
|
19
|
+
|
20
|
+
puts "Logging to #{logfile_path}."
|
21
|
+
self.logger = Logger.new(logfile_path, shift_age, shift_size)
|
22
|
+
logger.info "Logging initialized. Rotation age: #{shift_age} Rotation size: #{shift_size}"
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# CONSTANTS
|
28
|
+
|
29
|
+
# True Constants
|
30
|
+
mattr_accessor :CORE_APPLICATION_ID
|
31
|
+
mattr_accessor :DEFAULT_APPLICATION_ID
|
32
|
+
mattr_accessor :DEFAULT_PROD_LEVEL
|
33
|
+
|
34
|
+
self.CORE_APPLICATION_ID = "EVENTBUS_CORE"
|
35
|
+
self.DEFAULT_APPLICATION_ID = self.CORE_APPLICATION_ID
|
36
|
+
self.DEFAULT_PROD_LEVEL = "development"
|
37
|
+
|
38
|
+
# Calculated Constants
|
39
|
+
mattr_accessor :PROD_LEVEL
|
40
|
+
self.PROD_LEVEL = ENV['EVENTBUS_PROD_LEVEL'] || self.DEFAULT_PROD_LEVEL
|
41
|
+
|
42
|
+
# VARIABLES
|
43
|
+
|
44
|
+
mattr_accessor :application_id
|
45
|
+
self.application_id = ENV['EVENTBUS_APPLICATION_ID'] || self.DEFAULT_APPLICATION_ID
|
46
|
+
|
47
|
+
mattr_accessor :logger
|
48
|
+
self.set_up_logging
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,303 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'eventbus/common_init'
|
4
|
+
|
5
|
+
# This is taken almost directly from the Stomp gem's example
|
6
|
+
# logger, with minor modifications. Will work on customizing later.
|
7
|
+
|
8
|
+
# == Example STOMP call back logger class.
|
9
|
+
#
|
10
|
+
# Optional callback methods:
|
11
|
+
#
|
12
|
+
# * on_connecting: connection starting
|
13
|
+
# * on_connected: successful connect
|
14
|
+
# * on_connectfail: unsuccessful connect (will usually be retried)
|
15
|
+
# * on_disconnect: successful disconnect
|
16
|
+
#
|
17
|
+
# * on_miscerr: on miscellaneous xmit/recv errors
|
18
|
+
#
|
19
|
+
# * on_publish: publish called
|
20
|
+
# * on_subscribe: subscribe called
|
21
|
+
# * on_unsubscribe: unsubscribe called
|
22
|
+
#
|
23
|
+
# * on_begin: begin called
|
24
|
+
# * on_ack: ack called
|
25
|
+
# * on_nack: nack called
|
26
|
+
# * on_commit: commit called
|
27
|
+
# * on_abort: abort called
|
28
|
+
#
|
29
|
+
# * on_receive: receive called and successful
|
30
|
+
#
|
31
|
+
# * on_ssl_connecting: SSL connection starting
|
32
|
+
# * on_ssl_connected: successful SSL connect
|
33
|
+
# * on_ssl_connectfail: unsuccessful SSL connect (will usually be retried)
|
34
|
+
#
|
35
|
+
# * on_hbread_fail: unsuccessful Heartbeat read
|
36
|
+
# * on_hbwrite_fail: unsuccessful Heartbeat write
|
37
|
+
# * on_hbfire: on any send or receive heartbeat
|
38
|
+
#
|
39
|
+
# All methods are optional, at the user's requirements.
|
40
|
+
#
|
41
|
+
# If a method is not provided, it is not called (of course.)
|
42
|
+
#
|
43
|
+
# IMPORTANT NOTE: in general, call back logging methods *SHOULD* not raise exceptions,
|
44
|
+
# otherwise the underlying STOMP connection may fail in mysterious ways.
|
45
|
+
#
|
46
|
+
# There are two useful exceptions to this rule for:
|
47
|
+
#
|
48
|
+
# * on_connectfail
|
49
|
+
# * on_ssl_connectfail
|
50
|
+
#
|
51
|
+
# These two methods can raise a Stomp::Errors::LoggerConnectionError. If this
|
52
|
+
# exception is raised, it is passed up the chain to the caller.
|
53
|
+
#
|
54
|
+
# Callback parameters: are a copy of the @parameters instance variable for
|
55
|
+
# the Stomp::Connection.
|
56
|
+
#
|
57
|
+
|
58
|
+
module EventBus
|
59
|
+
module Connectors
|
60
|
+
module Stomp
|
61
|
+
|
62
|
+
class Slogger
|
63
|
+
|
64
|
+
# Initialize a new callback logger instance.
|
65
|
+
def initialize(init_parms = nil)
|
66
|
+
EventBus.logger.info("Logger is: #{EventBus.logger.inspect}")
|
67
|
+
EventBus.logger.info("Logger initialization complete.")
|
68
|
+
end
|
69
|
+
|
70
|
+
# Log connecting events
|
71
|
+
def on_connecting(parms)
|
72
|
+
begin
|
73
|
+
EventBus.logger.info "Connecting: #{info(parms)}"
|
74
|
+
rescue
|
75
|
+
EventBus.logger.debug "Connecting oops"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Log connected events
|
80
|
+
def on_connected(parms)
|
81
|
+
begin
|
82
|
+
EventBus.logger.info "Connected: #{info(parms)}"
|
83
|
+
rescue
|
84
|
+
EventBus.logger.debug "Connected oops"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Log connectfail events
|
89
|
+
def on_connectfail(parms)
|
90
|
+
begin
|
91
|
+
EventBus.logger.error "Connect Fail #{info(parms)}"
|
92
|
+
rescue
|
93
|
+
EventBus.logger.debug "Connect Fail oops"
|
94
|
+
end
|
95
|
+
=begin
|
96
|
+
# An example LoggerConnectionError raise
|
97
|
+
EventBus.logger.debug "Connect Fail, will raise"
|
98
|
+
raise Stomp::Error::LoggerConnectionError.new("quit from connect fail")
|
99
|
+
=end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Log disconnect events
|
103
|
+
def on_disconnect(parms)
|
104
|
+
begin
|
105
|
+
EventBus.logger.info "Disconnected #{info(parms)}"
|
106
|
+
rescue
|
107
|
+
EventBus.logger.debug "Disconnected oops"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Log miscellaneous errors
|
112
|
+
def on_miscerr(parms, errstr)
|
113
|
+
begin
|
114
|
+
EventBus.logger.unknown "Miscellaneous Error #{info(parms)}"
|
115
|
+
EventBus.logger.unknown "Miscellaneous Error String #{errstr}"
|
116
|
+
rescue
|
117
|
+
EventBus.logger.debug "Miscellaneous Error oops"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Log Subscribe
|
122
|
+
def on_subscribe(parms, headers)
|
123
|
+
begin
|
124
|
+
EventBus.logger.info "Subscribe Parms #{info(parms)}"
|
125
|
+
EventBus.logger.info "Subscribe Headers #{headers}"
|
126
|
+
rescue
|
127
|
+
EventBus.logger.debug "Subscribe oops"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# Log UnSubscribe
|
132
|
+
def on_unsubscribe(parms, headers)
|
133
|
+
begin
|
134
|
+
EventBus.logger.info "UnSubscribe Parms #{info(parms)}"
|
135
|
+
EventBus.logger.info "UnSubscribe Headers #{headers}"
|
136
|
+
rescue
|
137
|
+
EventBus.logger.debug "UnSubscribe oops"
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# Log Publish
|
142
|
+
def on_publish(parms, message, headers)
|
143
|
+
begin
|
144
|
+
EventBus.logger.info "Publish Parms #{info(parms)}"
|
145
|
+
EventBus.logger.info "Publish Message #{message}"
|
146
|
+
EventBus.logger.info "Publish Headers #{headers}"
|
147
|
+
rescue
|
148
|
+
EventBus.logger.debug "Publish oops"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# Log Receive
|
153
|
+
def on_receive(parms, result)
|
154
|
+
begin
|
155
|
+
EventBus.logger.info "Receive Parms #{info(parms)}"
|
156
|
+
EventBus.logger.info "Receive Result #{result}"
|
157
|
+
rescue
|
158
|
+
EventBus.logger.debug "Receive oops"
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Log Begin
|
163
|
+
def on_begin(parms, headers)
|
164
|
+
begin
|
165
|
+
EventBus.logger.info "Begin Parms #{info(parms)}"
|
166
|
+
EventBus.logger.info "Begin Result #{headers}"
|
167
|
+
rescue
|
168
|
+
EventBus.logger.debug "Begin oops"
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# Log Ack
|
173
|
+
def on_ack(parms, headers)
|
174
|
+
begin
|
175
|
+
EventBus.logger.debug "Ack Parms #{info(parms)}"
|
176
|
+
EventBus.logger.debug "Ack Result #{headers}"
|
177
|
+
rescue
|
178
|
+
EventBus.logger.debug "Ack oops"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# Log NAck
|
183
|
+
def on_nack(parms, headers)
|
184
|
+
begin
|
185
|
+
EventBus.logger.debug "NAck Parms #{info(parms)}"
|
186
|
+
EventBus.logger.debug "NAck Result #{headers}"
|
187
|
+
rescue
|
188
|
+
EventBus.logger.debug "NAck oops"
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
# Log Commit
|
193
|
+
def on_commit(parms, headers)
|
194
|
+
begin
|
195
|
+
EventBus.logger.info "Commit Parms #{info(parms)}"
|
196
|
+
EventBus.logger.info "Commit Result #{headers}"
|
197
|
+
rescue
|
198
|
+
EventBus.logger.debug "Commit oops"
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
# Log Abort
|
203
|
+
def on_abort(parms, headers)
|
204
|
+
begin
|
205
|
+
EventBus.logger.info "Abort Parms #{info(parms)}"
|
206
|
+
EventBus.logger.info "Abort Result #{headers}"
|
207
|
+
rescue
|
208
|
+
EventBus.logger.debug "Abort oops"
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
# Stomp 1.1+ - heart beat read (receive) failed.
|
213
|
+
def on_hbread_fail(parms, ticker_data)
|
214
|
+
begin
|
215
|
+
EventBus.logger.warn "Hbreadf Parms #{info(parms)}"
|
216
|
+
EventBus.logger.warn "Hbreadf Result #{ticker_data.inspect}"
|
217
|
+
rescue
|
218
|
+
EventBus.logger.debug "Hbreadf oops"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# Stomp 1.1+ - heart beat send (transmit) failed.
|
223
|
+
def on_hbwrite_fail(parms, ticker_data)
|
224
|
+
begin
|
225
|
+
EventBus.logger.warn "Hbwritef Parms #{info(parms)}"
|
226
|
+
EventBus.logger.warn "Hbwritef Result #{ticker_data.inspect}"
|
227
|
+
rescue
|
228
|
+
EventBus.logger.debug "Hbwritef oops"
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
# Log SSL connection start.
|
233
|
+
def on_ssl_connecting(parms)
|
234
|
+
begin
|
235
|
+
EventBus.logger.info "SSL Connecting Parms #{info(parms)}"
|
236
|
+
rescue
|
237
|
+
EventBus.logger.debug "SSL Connecting oops"
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
# Log a successful SSL connect.
|
242
|
+
def on_ssl_connected(parms)
|
243
|
+
begin
|
244
|
+
EventBus.logger.info "SSL Connected Parms #{info(parms)}"
|
245
|
+
rescue
|
246
|
+
EventBus.logger.debug "SSL Connected oops"
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
# Log an unsuccessful SSL connect.
|
251
|
+
def on_ssl_connectfail(parms)
|
252
|
+
begin
|
253
|
+
EventBus.logger.error "SSL Connect Fail Parms #{info(parms)}"
|
254
|
+
EventBus.logger.error "SSL Connect Fail Exception #{parms[:ssl_exception]}, #{parms[:ssl_exception].message}"
|
255
|
+
rescue
|
256
|
+
EventBus.logger.debug "SSL Connect Fail oops"
|
257
|
+
end
|
258
|
+
=begin
|
259
|
+
# An example LoggerConnectionError raise
|
260
|
+
EventBus.logger.debug "SSL Connect Fail, will raise"
|
261
|
+
raise Stomp::Error::LoggerConnectionError.new("quit from SSL connect")
|
262
|
+
=end
|
263
|
+
end
|
264
|
+
|
265
|
+
# Log heart beat fires
|
266
|
+
def on_hbfire(parms, srind, curt)
|
267
|
+
begin
|
268
|
+
EventBus.logger.debug "HeartBeat Fire Parms #{info(parms)}"
|
269
|
+
EventBus.logger.debug "HeartBeat Fire Send/Receive #{srind}"
|
270
|
+
rescue
|
271
|
+
EventBus.logger.debug "HeartBeat Fire oops"
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
# Example information extract.
|
278
|
+
def info(parms)
|
279
|
+
#
|
280
|
+
# Available in the parms Hash:
|
281
|
+
# parms[:cur_host]
|
282
|
+
# parms[:cur_port]
|
283
|
+
# parms[:cur_login]
|
284
|
+
# parms[:cur_passcode]
|
285
|
+
# parms[:cur_ssl]
|
286
|
+
# parms[:cur_recondelay]
|
287
|
+
# parms[:cur_parseto]
|
288
|
+
# parms[:cur_conattempts]
|
289
|
+
# parms[:openstat]
|
290
|
+
#
|
291
|
+
# For the on_ssl_connectfail callback these are also available:
|
292
|
+
# parms[:ssl_exception]
|
293
|
+
#
|
294
|
+
"Host: #{parms[:cur_host]}, Port: #{parms[:cur_port]}, Login: #{parms[:cur_login]}, Passcode: #{parms[:cur_passcode]}," +
|
295
|
+
" ssl: #{parms[:cur_ssl]}, open: #{parms[:openstat]}"
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
|
300
|
+
# module ends
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
@@ -1,31 +1,73 @@
|
|
1
1
|
# This module is primarily intended for use with ActiveMQ, though
|
2
2
|
# it should work with any Stomp-speaking broker.
|
3
|
+
require 'eventbus/common_init'
|
4
|
+
require_relative 'stomp/slogger'
|
5
|
+
require 'socket'
|
3
6
|
require 'stomp'
|
4
7
|
module StompConnectionDriver
|
5
8
|
|
6
9
|
@@StompConnector_Connection = nil
|
7
10
|
|
8
11
|
def connection_driver_initialize
|
9
|
-
|
12
|
+
|
10
13
|
end
|
11
14
|
|
12
15
|
def get_connection
|
13
|
-
|
14
|
-
|
16
|
+
|
17
|
+
opts = {
|
18
|
+
:hosts => [
|
19
|
+
{
|
15
20
|
:host => ENV["BROKER_HOST"] || "localhost",
|
16
|
-
:port => ENV["BROKER_PORT"] || 61613,
|
17
|
-
:
|
18
|
-
:
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
:port => ENV["BROKER_PORT"] || (ENV['EVENTBUS_STOMP_SSL'] == "false" ? 61613 : 61614),
|
22
|
+
:login => ENV["BROKER_USER"] || "eventbus",
|
23
|
+
:passcode => ENV["BROKER_PASS"] || "eventbus",
|
24
|
+
:ssl => ENV['EVENTBUS_STOMP_SSL'] == "false" ? false : true # True unless specified in ENV
|
25
|
+
}
|
26
|
+
],
|
27
|
+
:reliable => ENV['EVENTBUS_STOMP_RELIABLE'] == "false" ? false : true, # True unless specified in ENV
|
28
|
+
:initial_reconnect_delay => 1,
|
29
|
+
:max_reconnect_delay => 10,
|
30
|
+
:use_exponential_back_off => true,
|
31
|
+
:back_off_multiplier => 2,
|
32
|
+
:closed_check => ENV['EVENTBUS_STOMP_CLOSED_CHECK'] == "false" ? false : true,
|
33
|
+
:hbser => true,
|
34
|
+
:max_hbread_fails => ENV['EVENTBUS_STOMP_MAX_HBREAD_FAILS'] || 5,
|
35
|
+
:max_hbrlck_fails => ENV['EVENTBUS_STOMP_MAX_HBREADLOCK_FAILS'] || 5,
|
36
|
+
:connect_timeout => 30,
|
37
|
+
:connect_headers => {
|
38
|
+
# Set up heartbeating to keep connection alive and notice right away when it isn't.
|
39
|
+
#
|
40
|
+
# Value is two integer millisecond interval values, comma-delimited
|
41
|
+
# <interval we can send>,<interval we want to receive from server>
|
42
|
+
# Heartbeating can be disabled entirely or from one end by using 0 for
|
43
|
+
# one or both of these values in the EVENTBUS_STOMP_HEARTBEAT_INTERVALS environment variable:
|
44
|
+
# 0,0 Disable all heartbeating
|
45
|
+
# 0,1000 Receive heartbeats from broker every 1000ms, but don't send them.
|
46
|
+
# 1000,0 Send heartbeats from client every 1000ms, but don't look for them from broker.
|
47
|
+
#
|
48
|
+
# This is turned on by default to keep the connection alive in some environments where they can be
|
49
|
+
# dropped silently. If the heartbeat fails, it will trigger the :reliable re-connect.
|
50
|
+
# Best to leave it on.
|
51
|
+
#
|
52
|
+
# For heartbeats to work properly, you must be using Stomp protocol version 1.2 or higher.
|
53
|
+
# Tested to work with Apache Apollo.
|
54
|
+
:host => Socket.gethostname,
|
55
|
+
:"accept-version" => ENV['EVENTBUS_STOMP_PROTOCOL_VERSION'] || "1.2",
|
56
|
+
:"heart-beat" => ENV['EVENTBUS_STOMP_HEARTBEAT_INTERVALS'] || '1000,1000',
|
57
|
+
},
|
58
|
+
:logger => ENV['EVENTBUS_STOMP_DETAILED_LOGGING'] == "true" ? EventBus::Connectors::Stomp::Slogger.new : nil,
|
59
|
+
}
|
60
|
+
|
61
|
+
# If we never opened one, or if it's closed and not reliable, then open a new connection.
|
62
|
+
if @@StompConnector_Connection.nil? || (@@StompConnector_Connection.closed? && ! @@StompConnector_Connction.reliable?)
|
63
|
+
EventBus.logger.info("Connecting to STOMP with options: #{opts.to_s}")
|
64
|
+
@@StompConnector_Connection = Stomp::Client.new(opts)
|
65
|
+
end
|
66
|
+
|
67
|
+
# I'm not sure whether we should sleep here if the connection is closed?() but is reliable?()
|
68
|
+
# Assuming that the connection will do the right thing and punting until I find out different.
|
69
|
+
|
70
|
+
return @@StompConnector_Connection
|
29
71
|
|
30
72
|
end
|
31
73
|
|
@@ -48,7 +90,7 @@ module StompConnectionDriver
|
|
48
90
|
# and *more useful* behavior.
|
49
91
|
opts[:suppress_content_length] = true if opts[:suppress_content_length].nil?
|
50
92
|
|
51
|
-
|
93
|
+
EventBus.logger.info "Stomp sending message to: #{queue_name}"
|
52
94
|
|
53
95
|
client = get_connection
|
54
96
|
client.publish(queue_name, content, opts)
|
@@ -65,7 +107,6 @@ module StompConnectionDriver
|
|
65
107
|
# processes and machines. Therefore, don't lock the queue exclusively (i.e. don't kill other clients!)
|
66
108
|
# and only take blocks of one message rather than claiming large blocks. These assumptions should become
|
67
109
|
# configurable at some point, but for now it's not really a concern.
|
68
|
-
puts "Subscribing to #{queue_name}..."
|
69
110
|
|
70
111
|
client.subscribe(queue_name, {"activemq.prefetchSize" => 1, "activemq.exclusive" => false}) do |msg|
|
71
112
|
yield msg.body
|
data/lib/eventbus/message.rb
CHANGED
data/lib/eventbus/queue.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
|
+
require 'eventbus/common_init'
|
2
|
+
|
1
3
|
module EventBus
|
2
4
|
|
3
5
|
class Queue
|
4
6
|
|
5
|
-
def Queue.calc_name(base_name, application_id
|
7
|
+
def Queue.calc_name(base_name, application_id, prod_level, opts = {})
|
6
8
|
|
9
|
+
application_id ||= EventBus.application_id
|
10
|
+
prod_level ||= EventBus.PROD_LEVEL
|
11
|
+
|
7
12
|
global_queue = opts.delete(:global_queue) || false
|
8
13
|
system_queue = opts.delete(:system_queue) || false
|
9
14
|
|
@@ -19,7 +24,6 @@ module EventBus
|
|
19
24
|
queue_name = "#{application_id}.#{queue_name}" unless system_queue
|
20
25
|
end
|
21
26
|
|
22
|
-
puts "Calculated queue name: #{queue_name}"
|
23
27
|
return queue_name
|
24
28
|
end
|
25
29
|
|
data/lib/eventbus/service.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'logger'
|
2
|
+
require 'eventbus/common_init'
|
2
3
|
require 'eventbus/queue'
|
4
|
+
require 'fileutils'
|
3
5
|
|
4
6
|
module EventBus
|
7
|
+
|
5
8
|
class Service
|
6
9
|
|
7
10
|
attr_accessor :listen_queue
|
8
11
|
attr_accessor :system_process
|
9
12
|
attr_accessor :global_process
|
10
|
-
attr_accessor :logger
|
11
13
|
|
12
14
|
# Methods expected to be overridden in subclasses
|
13
15
|
def handle_exception(message, exception)
|
@@ -21,10 +23,10 @@ class Service
|
|
21
23
|
end
|
22
24
|
|
23
25
|
|
24
|
-
def initialize(application_id =
|
25
|
-
@
|
26
|
-
|
27
|
-
@
|
26
|
+
def initialize(application_id = EventBus.application_id)
|
27
|
+
@application_id = application_id
|
28
|
+
|
29
|
+
@listen_queue = self.class.name
|
28
30
|
@system_process = false
|
29
31
|
@global_process = false
|
30
32
|
|
@@ -51,7 +53,7 @@ class Service
|
|
51
53
|
opts[:system_queue] = @system_process
|
52
54
|
opts[:global_process] = @global_process
|
53
55
|
|
54
|
-
queue_name = Queue.calc_name(listen_queue, @application_id,
|
56
|
+
queue_name = Queue.calc_name(listen_queue, @application_id, EventBus.PROD_LEVEL, opts)
|
55
57
|
|
56
58
|
watch_queue(queue_name) do |message|
|
57
59
|
|
@@ -77,13 +79,23 @@ class Service
|
|
77
79
|
|
78
80
|
end
|
79
81
|
|
82
|
+
def logger
|
83
|
+
EventBus.logger
|
84
|
+
end
|
85
|
+
|
86
|
+
def logger=(new_logger)
|
87
|
+
EventBus.logger = new_logger
|
88
|
+
end
|
89
|
+
|
80
90
|
private
|
81
91
|
|
92
|
+
|
93
|
+
|
82
94
|
def process_message_wrapper(message)
|
83
95
|
|
84
96
|
begin
|
85
|
-
|
86
|
-
|
97
|
+
logger.info "Beginning processing of message in new thread:"
|
98
|
+
logger.info message
|
87
99
|
|
88
100
|
process_message message
|
89
101
|
rescue
|
@@ -93,11 +105,11 @@ class Service
|
|
93
105
|
end
|
94
106
|
|
95
107
|
def handle_exception_base(message, exception)
|
96
|
-
|
97
|
-
|
98
|
-
exception.backtrace.map { | frame |
|
108
|
+
logger.error "Caught exception. Exception Text: #{exception}"
|
109
|
+
logger.error "Backtrace:"
|
110
|
+
exception.backtrace.map { | frame | logger.error "- #{frame}"}
|
99
111
|
|
100
|
-
msg = EventBus::Message.new("
|
112
|
+
msg = EventBus::Message.new("EVENTBUS_CORE")
|
101
113
|
msg.load(message)
|
102
114
|
msg.set_error exception
|
103
115
|
|
@@ -106,8 +118,10 @@ class Service
|
|
106
118
|
msg.send
|
107
119
|
end
|
108
120
|
|
121
|
+
# This shouldn't be used deliberately when it can be helped (i.e. in EventBus code),
|
122
|
+
# but it re-directs puts in libs to logger.
|
109
123
|
def puts(message)
|
110
|
-
|
124
|
+
logger.info message
|
111
125
|
end
|
112
126
|
|
113
127
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eventbus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.26'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: bunny
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
30
14
|
- !ruby/object:Gem::Dependency
|
31
15
|
name: amqp
|
32
16
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,12 +65,13 @@ executables: []
|
|
81
65
|
extensions: []
|
82
66
|
extra_rdoc_files: []
|
83
67
|
files:
|
84
|
-
- lib/eventbus/
|
85
|
-
- lib/eventbus/connectors/bunny.rb
|
86
|
-
- lib/eventbus/connectors/stomp.rb
|
68
|
+
- lib/eventbus/service.rb
|
87
69
|
- lib/eventbus/connectors/amqp.rb
|
70
|
+
- lib/eventbus/connectors/stomp.rb
|
71
|
+
- lib/eventbus/connectors/stomp/slogger.rb
|
72
|
+
- lib/eventbus/queue.rb
|
88
73
|
- lib/eventbus/message.rb
|
89
|
-
- lib/eventbus/
|
74
|
+
- lib/eventbus/common_init.rb
|
90
75
|
homepage: http://spadea.net/?page_id=8
|
91
76
|
licenses: []
|
92
77
|
post_install_message:
|
@@ -110,5 +95,5 @@ rubyforge_project:
|
|
110
95
|
rubygems_version: 1.8.23
|
111
96
|
signing_key:
|
112
97
|
specification_version: 3
|
113
|
-
summary: Libraries for creating EventBus services
|
98
|
+
summary: Libraries for creating EventBus clients and services
|
114
99
|
test_files: []
|
@@ -1,71 +0,0 @@
|
|
1
|
-
# BunnyConnectionDriver is the original implementation of
|
2
|
-
# the AMQP-based broker. I am trying to make this work with
|
3
|
-
# straight Ruby AMQP driver, but this one is the default
|
4
|
-
# until I get that working properly.
|
5
|
-
|
6
|
-
module BunnyConnectionDriver
|
7
|
-
|
8
|
-
@@BunnyConnector_Connection = nil
|
9
|
-
|
10
|
-
def connection_driver_initialize
|
11
|
-
puts "BunnyConnectionDriver is loaded!"
|
12
|
-
end
|
13
|
-
|
14
|
-
def get_connection
|
15
|
-
if @@BunnyConnector_Connection.nil?
|
16
|
-
new_opts = {
|
17
|
-
:vhost => ENV["BROKER_VHOST"] || "eventbus",
|
18
|
-
:host => ENV["BROKER_HOST"] || "localhost",
|
19
|
-
:port => ENV["BROKER_PORT"] || 5672,
|
20
|
-
:user => ENV["BROKER_USER"] || "eventbus",
|
21
|
-
:pass => ENV["BROKER_PASS"] || "eventbus",
|
22
|
-
# :logging => opts.delete(:logging) || false,
|
23
|
-
#:logfile => opts.delete(:logfile) || STDOUT
|
24
|
-
}
|
25
|
-
|
26
|
-
@@BunnyConnector_Connection = Bunny.new(new_opts)
|
27
|
-
@@BunnyConnector_Connection.start
|
28
|
-
end
|
29
|
-
|
30
|
-
return @@BunnyConnector_Connection
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
def send_raw(content, opts = {})
|
35
|
-
queue_name = opts.delete(:queue_name)
|
36
|
-
client = get_connection
|
37
|
-
|
38
|
-
exchange = client.exchange('eventbus', :durable => true, :persistent => true, :immediate => false)
|
39
|
-
|
40
|
-
puts "Declaring queue: #{queue_name}"
|
41
|
-
q = client.queue(queue_name, :durable => true, :persistent => true, :immediate => false)
|
42
|
-
q.bind('eventbus', :key => queue_name)
|
43
|
-
|
44
|
-
puts "Publishing content to #{queue_name}: #{content}"
|
45
|
-
exchange.publish(content, :key => queue_name)
|
46
|
-
puts "Done!"
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
def watch_queue(listen_queue)
|
51
|
-
|
52
|
-
b = get_connection
|
53
|
-
|
54
|
-
@logger.info "Listening on #{listen_queue}"
|
55
|
-
|
56
|
-
exchange = b.exchange('eventbus', :durable => true, :persistent => true, :immediate => false)
|
57
|
-
|
58
|
-
q = b.queue(listen_queue, :durable => true, :persistent => true, :immediate => false)
|
59
|
-
q.bind(exchange, :key => listen_queue)
|
60
|
-
|
61
|
-
q.pop do |msg|
|
62
|
-
if msg[:delivery_details].nil?
|
63
|
-
# Gotta figure out why this is necessary. Blech.
|
64
|
-
sleep 0.25
|
65
|
-
else
|
66
|
-
yield msg[:payload]
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
71
|
-
end
|