rservicebus 0.0.59 → 0.0.60

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rservicebus.rb CHANGED
@@ -11,6 +11,7 @@ require "uri"
11
11
  require "rservicebus/helper_functions"
12
12
  require "rservicebus/ErrorMessage"
13
13
  require "rservicebus/HandlerLoader"
14
+ require "rservicebus/HandlerManager"
14
15
  require "rservicebus/ConfigureAppResource"
15
16
  require "rservicebus/ConfigureMQ"
16
17
  require "rservicebus/Host"
@@ -25,7 +25,9 @@ module RServiceBus
25
25
  # @param [String] uri a location for the resource to which we will attach, eg redis://127.0.0.1/foo
26
26
  def initialize( uri )
27
27
  @uri = uri
28
+ #Do a connect / disconnect loop on startup to validate the connection
28
29
  self._connect
30
+ self.finished
29
31
  end
30
32
 
31
33
  # The method which actually configures the resource.
@@ -6,10 +6,7 @@ module RServiceBus
6
6
  # loading handlers
7
7
  class HandlerLoader
8
8
 
9
- attr_reader :handlerList, :resourceList
10
-
11
- @host
12
- @appResources
9
+ attr_reader :handlerList
13
10
 
14
11
  @baseDir
15
12
  @filepath
@@ -21,7 +18,6 @@ class HandlerLoader
21
18
  @handler
22
19
 
23
20
  @handlerList
24
- @resourceList
25
21
 
26
22
  @listOfLoadedPaths
27
23
 
@@ -29,12 +25,10 @@ class HandlerLoader
29
25
  #
30
26
  # @param [RServiceBus::Host] host instance
31
27
  # @param [Hash] appResources As hash[k,v] where k is the name of a resource, and v is the resource
32
- def initialize( host, appResources )
33
- @host = host
34
- @appResources = appResources
35
-
36
- @handlerList = Hash.new
37
- @resourceList = Hash.new
28
+ def initialize( host, handlerManager )
29
+ @host = host
30
+
31
+ @handlerManager = handlerManager
38
32
 
39
33
  @listOfLoadedPaths = Hash.new
40
34
  end
@@ -78,43 +72,12 @@ class HandlerLoader
78
72
  return handler
79
73
  end
80
74
 
81
- # setBusAttributeIfRequested
82
- #
83
- # @param [RServiceBus::Handler] handler
84
- def setBusAttributeIfRequested( handler )
85
- if defined?( handler.Bus ) then
86
- handler.Bus = @host
87
- @host.log "Bus attribute set for: " + handler.class.name
88
- end
89
-
90
- return self
91
- end
92
-
93
- # Assigns appropriate resources to writable attributes in the handler that match keys in the resource hash
94
- #
95
- # @param [RServiceBus::Handler] handler
96
- ## @param [Hash] appResources As hash[k,v] where k is the name of a resource, and v is the resource
97
- def setAppResources( handler )
98
- @host.log "Checking app resources for: #{handler.class.name}", true
99
- @host.log "If your attribute is not getting set, check that it is in the 'attr_accessor' list", true
100
- @appResources.each do |k,v|
101
- if handler.class.method_defined?( k ) then
102
- handler.instance_variable_set( "@#{k}", v.getResource() )
103
- @resourceList[handler.class.name] = Array.new if @resourceList[handler.class.name].nil?
104
- @resourceList[handler.class.name] << v
105
- @host.log "App resource attribute, #{k}, set for: " + handler.class.name
106
- end
107
- end
108
-
109
- return self
110
- end
111
-
112
75
  # Wrapper function
113
76
  #
114
77
  # @param [String] filePath
115
78
  # @param [String] handlerName
116
79
  # @returns [RServiceBus::Handler] handler
117
- def loadAndConfigureHandler(msgName, filePath, handlerName)
80
+ def loadHandler(msgName, filePath, handlerName)
118
81
  if @listOfLoadedPaths.has_key?( filePath ) then
119
82
  @host.log "Not reloading, #{filePath}"
120
83
  return
@@ -125,12 +88,9 @@ class HandlerLoader
125
88
  @host.log "handlerName: " + handlerName, true
126
89
 
127
90
  handler = self.loadHandlerFromFile( handlerName, filePath )
128
- self.setBusAttributeIfRequested( handler )
129
- self.setAppResources( handler )
130
91
  @host.log "Loaded Handler: " + handlerName
131
92
 
132
- @handlerList[msgName] = Array.new unless @handlerList.has_key?( msgName )
133
- @handlerList[msgName] << handler;
93
+ @handlerManager.addHandler( msgName, handler )
134
94
 
135
95
  @listOfLoadedPaths[filePath] = 1
136
96
  rescue Exception => e
@@ -167,7 +127,7 @@ class HandlerLoader
167
127
  fileName = File.basename( filePath ).sub( ".rb", "" )
168
128
  handlerName = "MessageHandler_#{msgName}_#{fileName}"
169
129
 
170
- self.loadAndConfigureHandler( msgName, filePath, handlerName )
130
+ self.loadHandler( msgName, filePath, handlerName )
171
131
  end
172
132
  end
173
133
  end
@@ -201,7 +161,7 @@ class HandlerLoader
201
161
  self.loadHandlersFromSecondLevelPath( msgName, filePath )
202
162
  else
203
163
  handlerName = "MessageHandler_#{msgName}"
204
- self.loadAndConfigureHandler( msgName, filePath, handlerName )
164
+ self.loadHandler( msgName, filePath, handlerName )
205
165
  end
206
166
  end
207
167
  end
@@ -0,0 +1,145 @@
1
+ module RServiceBus
2
+
3
+ #Given a directory, this class is responsible for finding
4
+ # msgnames,
5
+ # handlernames, and
6
+ # loading handlers
7
+ class HandlerManager
8
+
9
+ # Constructor
10
+ #
11
+ # @param [RServiceBus::Host] host instance
12
+ # @param [Hash] appResources As hash[k,v] where k is the name of a resource, and v is the resource
13
+ def initialize( host, appResources )
14
+ @host = host
15
+ @appResources = appResources
16
+
17
+ @handlerList = Hash.new
18
+ @resourceListByHandlerName = Hash.new
19
+ end
20
+
21
+
22
+ # setBusAttributeIfRequested
23
+ #
24
+ # @param [RServiceBus::Handler] handler
25
+ def setBusAttributeIfRequested( handler )
26
+ if defined?( handler.Bus ) then
27
+ handler.Bus = @host
28
+ @host.log "Bus attribute set for: " + handler.class.name
29
+ end
30
+
31
+ return self
32
+ end
33
+
34
+ def interrogateHandlerForAppResources( handler )
35
+ @host.log "Checking app resources for: #{handler.class.name}", true
36
+ @host.log "If your attribute is not getting set, check that it is in the 'attr_accessor' list", true
37
+
38
+ @resourceListByHandlerName[handler.class.name] = Array.new
39
+ @appResources.each do |k,v|
40
+ if handler.class.method_defined?( k ) then
41
+ @resourceListByHandlerName[handler.class.name] << k
42
+ @host.log "Resource attribute, #{k}, found for: " + handler.class.name
43
+ end
44
+ end
45
+
46
+ return self
47
+ end
48
+
49
+ def addHandler( msgName, handler )
50
+ # @handlerList[msgName] = Array.new unless @handlerList.has_key?( msgName )
51
+ @handlerList[msgName] = Array.new if @handlerList[msgName].nil?
52
+ @handlerList[msgName] << handler
53
+
54
+ self.setBusAttributeIfRequested( handler )
55
+ self.interrogateHandlerForAppResources( handler )
56
+ end
57
+
58
+ # Assigns appropriate resources to writable attributes in the handler that match keys in the resource hash
59
+ #
60
+ # @param [RServiceBus::Handler] handler
61
+ ## @param [Hash] appResources As hash[k,v] where k is the name of a resource, and v is the resource
62
+ def setAppResources( handler )
63
+ @host.log "Checking app resources for: #{handler.class.name}", true
64
+ @host.log "If your attribute is not getting set, check that it is in the 'attr_accessor' list", true
65
+ @appResources.each do |k,v|
66
+ if handler.class.method_defined?( k ) then
67
+ v._connect
68
+ v.Begin
69
+ handler.instance_variable_set( "@#{k}", v.getResource() )
70
+ @host.log "App resource attribute, #{k}, set for: " + handler.class.name
71
+ end
72
+ end
73
+
74
+ return self
75
+ end
76
+
77
+ def getListOfResourcesNeededToProcessMsg( msgName )
78
+ list = Array.new
79
+ @handlerList[msgName].each do |handler|
80
+ list = list + @resourceListByHandlerName[handler.class.name] unless @resourceListByHandlerName[handler.class.name].nil?
81
+ end
82
+ list.uniq!
83
+
84
+ return list
85
+ end
86
+
87
+ def setResourcesForHandlersNeededToProcessMsg( msgName )
88
+ @handlerList[msgName].each do |handler|
89
+ next if @resourceListByHandlerName[handler.class.name].nil?
90
+ @resourceListByHandlerName[handler.class.name].each do |k|
91
+ handler.instance_variable_set( "@#{k}", @appResources[k].getResource() )
92
+ @host.log "App resource attribute, #{k}, set for: " + handler.class.name
93
+ end
94
+ end
95
+
96
+ end
97
+
98
+ def getHandlerListForMsg( msgName )
99
+ raise NoHandlerFound.new( msgName ) if @handlerList[msgName].nil?
100
+
101
+ list = self.getListOfResourcesNeededToProcessMsg( msgName )
102
+ list.each do |resourceName|
103
+ @host.log( "Preparing reousrce: #{resourceName}", true )
104
+ r = @appResources[resourceName]
105
+ r._connect
106
+ r.Begin
107
+ end
108
+
109
+ self.setResourcesForHandlersNeededToProcessMsg( msgName )
110
+
111
+ return @handlerList[msgName]
112
+ end
113
+
114
+ def commitResourcesUsedToProcessMsg( msgName )
115
+ list = self.getListOfResourcesNeededToProcessMsg( msgName )
116
+ list.each do |resourceName|
117
+ r = @appResources[resourceName]
118
+ r.Commit
119
+ end
120
+ end
121
+
122
+ def rollbackResourcesUsedToProcessMsg( msgName )
123
+ @host.log "HandlerManager.rollbackResourcesUsedToProcessMsg, #{msgName}", true
124
+ list = self.getListOfResourcesNeededToProcessMsg( msgName )
125
+ list.each do |resourceName|
126
+ begin
127
+ r = @appResources[resourceName]
128
+ @host.log "Rollback resource, #{r.class.name}", true
129
+ r.Rollback
130
+ rescue Exception => e1
131
+ @host.log "Caught nested exception rolling back, #{r.class.name}, for msg, #{msgName}"
132
+ @host.log "****"
133
+ @host.log e1.message
134
+ @host.log e.backtrace
135
+ @host.log "****"
136
+ end
137
+ end
138
+
139
+ end
140
+
141
+ def canMsgBeHandledLocally( msgName )
142
+ return @handlerList.has_key?(msgName)
143
+ end
144
+ end
145
+ end
@@ -12,7 +12,7 @@ module RServiceBus
12
12
  class Host
13
13
 
14
14
  @handlerList
15
- @resourceByHandlerNameList
15
+ @resourceListByHandlerName
16
16
 
17
17
  @subscriptions
18
18
 
@@ -84,15 +84,13 @@ module RServiceBus
84
84
  #
85
85
  def loadHandlers()
86
86
  log "Load Message Handlers"
87
- @handlerLoader = HandlerLoader.new( self, @appResources )
87
+ @handlerManager = HandlerManager.new( self, @appResources )
88
+ @handlerLoader = HandlerLoader.new( self, @handlerManager )
88
89
 
89
90
  @config.handlerPathList.each do |path|
90
91
  @handlerLoader.loadHandlersFromPath(path)
91
92
  end
92
93
 
93
- @handlerList = @handlerLoader.handlerList
94
- @resourceByHandlerNameList = @handlerLoader.resourceList
95
-
96
94
  return self
97
95
  end
98
96
 
@@ -115,7 +113,7 @@ module RServiceBus
115
113
  log "Load Libs"
116
114
 
117
115
  @config.libList.each do |path|
118
- $:.unshift path
116
+ $:.unshift path
119
117
  if Dir.exists?( path ) then
120
118
  path = path.strip.chomp( "/" )
121
119
  path = path + "/**/*.rb"
@@ -197,11 +195,11 @@ $:.unshift path
197
195
  #Popping a msg off the queue should not be in the message handler, as it affects retry
198
196
  begin
199
197
  if statOutputCountdown == 0 then
200
- # log @stats.getForReporting
201
- statOutputCountdown = @config.statOutputCountdown-1
202
- else
203
- statOutputCountdown = statOutputCountdown - 1
198
+ log @stats.getForReporting
199
+ statOutputCountdown = @config.statOutputCountdown
204
200
  end
201
+ statOutputCountdown = statOutputCountdown - 1
202
+
205
203
  body = @mq.pop
206
204
  begin
207
205
  @stats.incTotalProcessed
@@ -217,21 +215,21 @@ $:.unshift path
217
215
  end
218
216
  @mq.ack
219
217
  rescue ClassNotFoundForMsg => e
220
- puts "*** Class not found for msg, #{e.message}"
221
- puts "*** Ensure, #{e.message}, is defined in Contract.rb, most likely as 'Class #{e.message} end"
218
+ puts "*** Class not found for msg, #{e.message}"
219
+ puts "*** Ensure, #{e.message}, is defined in Contract.rb, most likely as 'Class #{e.message} end"
222
220
 
223
- @msg.addErrorMsg( @config.localQueueName, e.message )
224
- serialized_object = YAML::dump(@msg)
225
- self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
226
- @mq.ack
221
+ @msg.addErrorMsg( @config.localQueueName, e.message )
222
+ serialized_object = YAML::dump(@msg)
223
+ self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
224
+ @mq.ack
227
225
  rescue NoHandlerFound => e
228
- puts "*** Handler not found for msg, #{e.message}"
229
- puts "*** Ensure a handler named, #{e.message}, is present in the MessageHandler directory."
226
+ puts "*** Handler not found for msg, #{e.message}"
227
+ puts "*** Ensure a handler named, #{e.message}, is present in the MessageHandler directory."
230
228
 
231
- @msg.addErrorMsg( @config.localQueueName, e.message )
232
- serialized_object = YAML::dump(@msg)
233
- self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
234
- @mq.ack
229
+ @msg.addErrorMsg( @config.localQueueName, e.message )
230
+ serialized_object = YAML::dump(@msg)
231
+ self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
232
+ @mq.ack
235
233
  rescue Exception => e
236
234
  sleep 0.5
237
235
 
@@ -240,22 +238,6 @@ $:.unshift path
240
238
  puts e.backtrace
241
239
  puts "***"
242
240
 
243
- tempHandlerList = Hash.new
244
- tempResourceList = Hash.new
245
- puts @msg.to_s
246
- puts @msg.msg.to_s
247
- puts @msg.msg.class.to_s
248
- puts @msg.msg.class.name
249
-
250
- @handlerList[@msg.msg.class.name].each do |handler|
251
- tempHandlerList[handler.class.name] = handler
252
- @resourceByHandlerNameList[handler.class.name].each do |resource|
253
- tempResourceList[resource.class.name] = resource
254
- end
255
- end
256
- tempResourceList.each {|k,resource| resource.reconnect }
257
- tempHandlerList.each {|k,handler| @handlerLoader.setAppResources( handler ) }
258
-
259
241
  if retries > 0 then
260
242
  retries = retries - 1
261
243
  @mq.returnToQueue
@@ -315,61 +297,32 @@ $:.unshift path
315
297
  #
316
298
  def HandleMessage()
317
299
  msgName = @msg.msg.class.name
318
- handlerList = @handlerList[msgName]
300
+ handlerList = @handlerManager.getHandlerListForMsg(msgName)
319
301
 
320
- if handlerList == nil then
321
- raise NoHandlerFound.new( msgName )
302
+ log "Handler found for: " + msgName, true
303
+ begin
304
+ @queueForMsgsToBeSentOnComplete = Array.new
322
305
 
323
- else
324
- log "Handler found for: " + msgName, true
325
- log "Prep app resources", true
326
- tempResourceList = Hash.new
327
306
  handlerList.each do |handler|
328
- if !@resourceByHandlerNameList[handler.class.name].nil? then
329
- @resourceByHandlerNameList[handler.class.name].each do |resource|
330
- tempResourceList[resource.class.name] = resource
331
- end
307
+ begin
308
+ handler.Handle( @msg.msg )
309
+ rescue Exception => e
310
+ puts "E #{e.message}"
311
+ log "An error occured in Handler: " + handler.class.name
312
+ raise e
332
313
  end
333
314
  end
334
- begin
335
- @queueForMsgsToBeSentOnComplete = Array.new
336
-
337
- tempResourceList.each do |name, resource|
338
- log "Prep resource, #{name}", true
339
- resource.Begin
340
- end
341
-
342
- handlerList.each do |handler|
343
- begin
344
- handler.Handle( @msg.msg )
345
- rescue Exception => e
346
- puts "E #{e.message}"
347
- log "An error occured in Handler: " + handler.class.name
348
- raise e
349
- end
350
- end
351
-
352
- tempResourceList.each do |name, resource|
353
- log "Commit resource, #{name}", true
354
- resource.Commit
355
- end
356
-
357
- self.sendQueuedMsgs
358
-
359
- rescue Exception => e
360
-
361
- tempResourceList.each do |name, resource|
362
- log "Rollback resource, #{name}", true
363
- begin
364
- resource.Rollback
365
- rescue Exception => e1
366
- log "Nested exception rolling back, #{resource.class.name}, for msg, #{msgName}"
367
- end
368
- end
369
- @queueForMsgsToBeSentOnComplete = nil
370
-
371
- raise e
372
- end
315
+
316
+ @handlerManager.commitResourcesUsedToProcessMsg( msgName )
317
+
318
+ self.sendQueuedMsgs
319
+
320
+ rescue Exception => e
321
+
322
+ @handlerManager.rollbackResourcesUsedToProcessMsg( msgName )
323
+ @queueForMsgsToBeSentOnComplete = nil
324
+
325
+ raise e
373
326
  end
374
327
  end
375
328
 
@@ -430,11 +383,11 @@ $:.unshift path
430
383
  def Send( msg )
431
384
  log "Bus.Send", true
432
385
  @stats.incTotalSent
433
-
386
+
434
387
  msgName = msg.class.name
435
388
  if @config.messageEndpointMappings.has_key?( msgName ) then
436
389
  queueName = @config.messageEndpointMappings[msgName]
437
- elsif !@handlerList[msgName].nil? then
390
+ elsif @handlerManager.canMsgBeHandledLocally(msgName) then
438
391
  queueName = @config.localQueueName
439
392
  else
440
393
  log "No end point mapping found for: " + msgName
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rservicebus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.59
4
+ version: 0.0.60
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-25 00:00:00.000000000 Z
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Ruby interpretation of NServiceBus
15
15
  email: guy@guyirvine.com
@@ -42,6 +42,7 @@ files:
42
42
  - lib/rservicebus/ConfigureSubscriptionStorage.rb
43
43
  - lib/rservicebus/ErrorMessage.rb
44
44
  - lib/rservicebus/HandlerLoader.rb
45
+ - lib/rservicebus/HandlerManager.rb
45
46
  - lib/rservicebus/helper_functions.rb
46
47
  - lib/rservicebus/Host.rb
47
48
  - lib/rservicebus/Message/Subscription.rb