rservicebus 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rservicebus.rb CHANGED
@@ -9,14 +9,14 @@ require "rservicebus/helper_functions"
9
9
  require "rservicebus/Agent"
10
10
  require "rservicebus/ErrorMessage"
11
11
  require "rservicebus/Message"
12
- require "rservicebus/Subscription"
13
12
  require "rservicebus/HandlerLoader"
14
13
  require "rservicebus/ConfigureAppResource"
15
- require "rservicebus/AppResource"
16
- require "rservicebus/RedisAppResource"
17
14
  require "rservicebus/Host"
18
15
  require "rservicebus/Config"
19
16
 
17
+ require "rservicebus/AppResource"
18
+ require "rservicebus/AppResource/Redis"
19
+
20
20
 
21
21
  module RServiceBus
22
22
 
@@ -1,7 +1,7 @@
1
1
  require "redis"
2
2
 
3
3
  #Implementation of an AppResource - Redis
4
- class RedisAppResource<AppResource
4
+ class AppResource_Redis<AppResource
5
5
 
6
6
  @connection
7
7
 
@@ -108,6 +108,12 @@ class Config
108
108
  end
109
109
  end
110
110
 
111
+ #Marshals paths for contracts
112
+ #
113
+ #Note. .rb extension is optional
114
+ #
115
+ #Expected format;
116
+ # /one/two/Contracts
111
117
  def loadContracts()
112
118
  if self.getValue( "CONTRACTS", "./Contract" ).nil? then
113
119
  return self
@@ -12,7 +12,7 @@ class ConfigureAppResource
12
12
  uri = URI.parse( v )
13
13
  case uri.scheme
14
14
  when "redis"
15
- resources[k.sub( "RSB_", "" )] = RedisAppResource.new( uri )
15
+ resources[k.sub( "RSB_", "" )] = AppResource_Redis.new( uri )
16
16
  else
17
17
  abort("Scheme, #{uri.scheme}, not recognised when configuring app resource, #{k}=#{v}");
18
18
  end
@@ -1,5 +1,6 @@
1
1
  module RServiceBus
2
2
 
3
+ #Host process for rservicebus
3
4
  class Host
4
5
 
5
6
 
@@ -11,8 +12,7 @@ class Host
11
12
 
12
13
  @beanstalk
13
14
 
14
- @appResources
15
-
15
+ @appResources
16
16
 
17
17
  @config
18
18
 
@@ -48,21 +48,18 @@ class Host
48
48
  return self
49
49
  end
50
50
 
51
- def initialize()
52
-
53
- @config = ConfigFromEnv.new
54
- .configureLogging()
55
- .loadHostSection()
56
- .configureBeanstalk()
57
- .loadContracts()
58
- .loadMessageEndpointMappings()
59
- .loadHandlerPathList();
60
-
61
- self.configureAppResource()
62
- .connectToBeanstalk()
63
- .loadHandlers()
64
- .loadSubscriptions()
65
- .sendSubscriptions()
51
+ #Subscriptions are specified by adding events to the
52
+ #msg endpoint mapping
53
+ def sendSubscriptions
54
+ log "Send Subscriptions"
55
+ @config.messageEndpointMappings.each do |eventName,queueName|
56
+ log "Checking, " + eventName + " for Event", true
57
+ if eventName.end_with?( "Event" ) then
58
+ log eventName + ", is an event. About to send subscription to, " + queueName, true
59
+ self.Subscribe( eventName )
60
+ log "Subscribed to, " + eventName + " at, " + queueName
61
+ end
62
+ end
66
63
 
67
64
  return self
68
65
  end
@@ -80,20 +77,7 @@ class Host
80
77
  return self
81
78
  end
82
79
 
83
- def sendSubscriptions
84
- log "Send Subscriptions"
85
- @config.messageEndpointMappings.each do |eventName,queueName|
86
- log "Checking, " + eventName + " for Event", true
87
- if eventName.end_with?( "Event" ) then
88
- log eventName + ", is an event. About to send subscription to, " + queueName, true
89
- self.Subscribe( eventName )
90
- log "Subscribed to, " + eventName + " at, " + queueName
91
- end
92
- end
93
-
94
- return self
95
- end
96
-
80
+ #Load an existing subscription - startup function
97
81
  def loadSubscriptions
98
82
  log "Load subscriptions"
99
83
  @subscriptions = Hash.new
@@ -133,6 +117,26 @@ class Host
133
117
  return self
134
118
  end
135
119
 
120
+ def initialize()
121
+
122
+ @config = ConfigFromEnv.new
123
+ .configureLogging()
124
+ .loadHostSection()
125
+ .configureBeanstalk()
126
+ .loadContracts()
127
+ .loadMessageEndpointMappings()
128
+ .loadHandlerPathList();
129
+
130
+ self.configureAppResource()
131
+ .connectToBeanstalk()
132
+ .loadHandlers()
133
+ .loadSubscriptions()
134
+ .sendSubscriptions()
135
+
136
+ return self
137
+ end
138
+
139
+ #Process a subscription request from a subscriber
136
140
  def addSubscrption( eventName, queueName )
137
141
  log "Adding subscrption for, " + eventName + ", to, " + queueName
138
142
  redis = Redis.new
@@ -162,10 +166,11 @@ class Host
162
166
  log "Waiting for messages. To exit press CTRL+C"
163
167
 
164
168
  loop do
165
- job = @beanstalk.reserve
166
- body = job.body
167
169
  retries = @config.maxRetries
168
170
  begin
171
+ job = @beanstalk.reserve
172
+ body = job.body
173
+
169
174
  @msg = YAML::load(body)
170
175
  if @msg.msg.class.name == "RServiceBus::Subscription" then
171
176
  self.addSubscrption( @msg.msg.eventName, @msg.returnAddress )
@@ -177,8 +182,21 @@ class Host
177
182
  end
178
183
  job.delete
179
184
  rescue Exception => e
185
+ sleep 0.5
180
186
  retry if (retries -= 1) > 0
181
187
 
188
+ if e.class.name == "Beanstalk::NotConnected" then
189
+ puts "Lost connection to beanstalkd."
190
+ puts "*** Start or Restart beanstalkd and try again."
191
+ abort();
192
+ end
193
+
194
+ if e.class.name == "Redis::CannotConnectError" then
195
+ puts "Lost connection to redis."
196
+ puts "*** Start or Restart redis and try again."
197
+ abort();
198
+ end
199
+
182
200
  errorString = e.message + ". " + e.backtrace[0]
183
201
  if e.backtrace.length > 1 then
184
202
  errorString = errorString + ". " + e.backtrace[1]
@@ -216,6 +234,10 @@ class Host
216
234
  end
217
235
  end
218
236
 
237
+ #Sends a msg across the bus
238
+ #
239
+ # @param [String] serialized_object serialized RServiceBus::Message
240
+ # @param [String] queueName endpoint to which the msg will be sent
219
241
  def _SendAlreadyWrappedAndSerialised( serialized_object, queueName )
220
242
  log "Bus._SendAlreadyWrappedAndSerialised", true
221
243
 
@@ -223,6 +245,10 @@ class Host
223
245
  @beanstalk.put( serialized_object )
224
246
  end
225
247
 
248
+ #Sends a msg across the bus
249
+ #
250
+ # @param [RServiceBus::Message] msg msg to be sent
251
+ # @param [String] queueName endpoint to which the msg will be sent
226
252
  def _SendNeedsWrapping( msg, queueName )
227
253
  log "Bus._SendNeedsWrapping", true
228
254
 
@@ -232,6 +258,11 @@ class Host
232
258
  self._SendAlreadyWrappedAndSerialised( serialized_object, queueName )
233
259
  end
234
260
 
261
+ #Sends a msg back across the bus
262
+ #Reply queues are specified in each msg. It works like
263
+ #email, where the reply address can actually be anywhere
264
+ #
265
+ # @param [RServiceBus::Message] msg msg to be sent
235
266
  def Reply( msg )
236
267
  log "Reply with: " + msg.class.name + " To: " + @msg.returnAddress, true
237
268
 
@@ -239,10 +270,13 @@ class Host
239
270
  end
240
271
 
241
272
 
273
+ #Send a msg across the bus
274
+ #msg destination is specified at the infrastructure level
275
+ #
276
+ # @param [RServiceBus::Message] msg msg to be sent
242
277
  def Send( msg )
243
278
  log "Bus.Send", true
244
279
 
245
-
246
280
  msgName = msg.class.name
247
281
  if !@config.messageEndpointMappings.has_key?( msgName ) then
248
282
  log "No end point mapping found for: " + msgName
@@ -255,6 +289,9 @@ class Host
255
289
  self._SendNeedsWrapping( msg, queueName )
256
290
  end
257
291
 
292
+ #Sends an event to all subscribers across the bus
293
+ #
294
+ # @param [RServiceBus::Message] msg msg to be sent
258
295
  def Publish( msg )
259
296
  log "Bus.Publish", true
260
297
 
@@ -270,6 +307,9 @@ class Host
270
307
 
271
308
  end
272
309
 
310
+ #Sends a subscription request across the Bus
311
+ #
312
+ # @param [String] eventName event to be subscribes to
273
313
  def Subscribe( eventName )
274
314
  log "Bus.Subscribe: " + eventName, true
275
315
 
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.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -19,6 +19,7 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - lib/rservicebus/Agent.rb
22
+ - lib/rservicebus/AppResource/Redis.rb
22
23
  - lib/rservicebus/AppResource.rb
23
24
  - lib/rservicebus/Config.rb
24
25
  - lib/rservicebus/ConfigureAppResource.rb
@@ -27,8 +28,6 @@ files:
27
28
  - lib/rservicebus/helper_functions.rb
28
29
  - lib/rservicebus/Host.rb
29
30
  - lib/rservicebus/Message.rb
30
- - lib/rservicebus/RedisAppResource.rb
31
- - lib/rservicebus/Subscription.rb
32
31
  - lib/rservicebus/Test/Bus.rb
33
32
  - lib/rservicebus/Test/Redis.rb
34
33
  - lib/rservicebus/Test.rb
@@ -1,13 +0,0 @@
1
- module RServiceBus
2
-
3
-
4
- class Subscription
5
- attr_reader :eventName
6
-
7
- def initialize( eventName )
8
- @eventName=eventName
9
- end
10
-
11
- end
12
-
13
- end