rservicebus 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ require "uri"
2
+
3
+ class AppResource
4
+ @uri
5
+
6
+ def initialize( uri )
7
+ @uri = uri
8
+ end
9
+
10
+ def getResource
11
+ raise "Method, getResource, needs to be implemented for resource"
12
+ end
13
+ end
@@ -5,118 +5,113 @@ class Config
5
5
  ## @appName: CreateUser
6
6
  ## errorQueueName: error
7
7
  #
8
+ attr_reader :appName, :messageEndpointMappings, :handlerPathList, :localQueueName, :errorQueueName, :maxRetries, :forwardReceivedMessagesTo, :verbose, :beanstalkHost
8
9
 
9
- attr_reader :appName, :handlerPathList
10
- @config
11
10
  @appName
12
-
11
+ @messageEndpointMappings
13
12
  @handlerPathList
13
+
14
+ @localQueueName
15
+ @errorQueueName
16
+ @maxRetries
17
+ @forwardReceivedMessagesTo
18
+
19
+ @verbose
14
20
 
21
+ @beanstalkHost
22
+
23
+ def initialize()
24
+ puts "Cannot instantiate config directly."
25
+ puts "For production, use ConfigFromEnv."
26
+ puts "For debugging or testing, you could try ConfigFromSetter"
27
+ abort()
28
+ end
29
+
15
30
  def getValue( name, default=nil )
16
- if ENV[name].nil? then
17
- return default
18
- end
19
-
20
- return ENV[name]
31
+ value = ENV["#{name}"].nil? ? default : ENV["#{name}"];
32
+ puts "Env value: #{name}: #{value}"
33
+ return value
21
34
  end
22
35
 
23
- def loadMessageEndpointMappings( host )
24
- mapping = self.getValue( "RSERVICEBUS_MESSAGEENDPOINTMAPPINGS" )
36
+ def loadMessageEndpointMappings()
37
+ mapping = self.getValue( "MESSAGE_ENDPOINT_MAPPINGS" )
25
38
 
26
39
  messageEndpointMappings=Hash.new
27
40
  if !mapping.nil? then
28
41
  mapping.split( ";" ).each do |line|
29
42
  match = line.match( /(.+):(.+)/ )
30
- messageEndpointMappings[match[0]] = match[1]
43
+ messageEndpointMappings[match[1]] = match[2]
31
44
  end
32
45
  end
33
46
 
34
- host.messageEndpointMappings=messageEndpointMappings
35
- end
47
+ @messageEndpointMappings=messageEndpointMappings
36
48
 
37
- def loadHandlerPathList(host)
38
- path = self.getValue( "RSERVICEBUS_MESSAGEHANDLERPATH", "MessageHandler" )
49
+ return self
50
+ end
39
51
 
40
- @handlerPathList = Array.new
52
+ def loadHandlerPathList()
53
+ path = self.getValue( "MSGHANDLERPATH", "MessageHandler" )
54
+ handlerPathList = Array.new
41
55
  path.split( ";" ).each do |path|
42
56
  path = path.strip.chomp( "/" )
43
- @handlerPathList << path
57
+ handlerPathList << path
44
58
  end
45
59
 
46
- host.handlerPathList = @handlerPathList
60
+ @handlerPathList = handlerPathList
61
+
62
+ return self
47
63
  end
48
64
 
49
65
 
50
- def loadHostSection( host )
51
- path = self.getValue( "RSERVICEBUS_APPNAME", "RServiceBus" )
52
- localQueueName = self.getValue( "RSERVICEBUS_APPNAME", "RServiceBus" )
53
- # @appName = self.getValue( "host", "appName", "RServiceBus" )
54
- # host.appName = @appName
55
- host.localQueueName = @appName
56
- host.errorQueueName = self.getValue( "host", "errorQueueName", "error" )
57
- host.maxRetries = self.getValue( "host", "maxRetries", 5 )
58
- host.forwardReceivedMessagesTo = self.getValue( "host", "forwardReceivedMessagesTo", nil )
66
+ def loadHostSection()
67
+ @appName = self.getValue( "APPNAME", "RServiceBus" )
68
+ @localQueueName = @appName
69
+ @errorQueueName = self.getValue( "ERROR_QUEUE_NAME", "error" )
70
+ @maxRetries = self.getValue( "MAX_RETRIES", "5" ).to_i
71
+ @forwardReceivedMessagesTo = self.getValue( "FORWARD_RECEIVED_MESSAGES_TO" )
59
72
 
60
- self.loadHandlerPathList(host)
73
+ return self
61
74
  end
62
75
 
63
-
64
- def configureLogging( host )
65
- logger = Logger.new "rservicebus." + @appName
66
- loggingLevel = self.getLoggingLevel()
67
-
68
- if self.getValue( "logger", "stdout", true ) != false then
69
- Outputter.stdout.level = loggingLevel
70
- logger.outputters = Outputter.stdout
76
+ def loadContracts()
77
+ if self.getValue( "CONTRACTS" ).nil? then
78
+ return self
71
79
  end
72
80
 
73
- fileName = self.getValue( "logger", "fileName", @appName + ".log" );
74
- if fileName != false then
75
- file = FileOutputter.new(@appName + ".file", :filename => fileName,:trunc => false)
76
- file.level = loggingLevel
77
- file.formatter = PatternFormatter.new(:pattern => self.getValue( "logger", "fileFormat", "[%l] %d :: %m" ))
78
- logger.add( file )
81
+ self.getValue( "CONTRACTS" ).split( ";" ).each do |path|
82
+ self.log "Loading contracts from, #{path}"
83
+ require path
79
84
  end
80
- host.logger = logger
85
+ return self
81
86
  end
82
87
 
83
- def processConfig( host )
84
- self.loadHostSection(host)
85
- self.configureLogging(host)
86
- self.loadMessageEndpointMappings( host )
87
-
88
+ def configureLogging()
89
+ @verbose = !self.getValue( "VERBOSE", nil ).nil?
88
90
 
89
91
  return self
90
92
  end
91
93
 
92
- end
94
+ def configureBeanstalk
95
+ @beanstalkHost = self.getValue( "BEANSTALK", "localhost:11300" )
93
96
 
97
+ return self
98
+ end
94
99
 
95
- class ConfigFromFile<Config
96
100
 
97
- def getConfigurationFilePath(configFilePath)
98
- configFilePath = configFilePath.nil? ? "RServiceBus.yml" : configFilePath
99
- if File.exists?(configFilePath) == false then
100
- puts "Config file could not be found at: " + configFilePath
101
- puts "(You can specifiy a config file with: ruby RServiceBus [your config file path]"
102
- abort()
103
- end
101
+ end
104
102
 
105
- return configFilePath
106
- end
107
103
 
104
+ class ConfigFromEnv<Config
108
105
 
109
- def initialize(configFilePath )
110
- configFilePath = self.getConfigurationFilePath(configFilePath)
111
- @config = YAML.load_file(configFilePath)
106
+ def initialize()
112
107
  end
113
108
 
114
109
  end
115
110
 
116
- class ConfigFromYAMLObject<Config
111
+ class ConfigFromSetter<Config
112
+ attr_writer :appName, :messageEndpointMappings, :handlerPathList, :localQueueName, :errorQueueName, :maxRetries, :forwardReceivedMessagesTo, :verbose, :beanstalkHost
117
113
 
118
- def initialize(config )
119
- @config = config
114
+ def initialize()
120
115
  end
121
116
 
122
117
  end
@@ -1,32 +1,4 @@
1
1
  require "uri"
2
- require "redis"
3
-
4
- class AppResource
5
- @uri
6
-
7
- def initialize( uri )
8
- @uri = uri
9
- end
10
-
11
- def getResource
12
- raise "Method, getResource, needs to be implemented for resource"
13
- end
14
- end
15
-
16
- class RedisAppResource<AppResource
17
-
18
- @connection
19
-
20
- def initialize( uri )
21
- super(uri)
22
- @connection = Redis.new
23
- end
24
-
25
- def getResource
26
- return @connection
27
- end
28
-
29
- end
30
2
 
31
3
  class ConfigureAppResource
32
4
 
@@ -2,104 +2,36 @@ module RServiceBus
2
2
 
3
3
  class Host
4
4
 
5
- @appName
6
5
 
7
- @handlerPathList
8
6
  @handlerList
9
7
 
10
- @errorQueueName
11
- @maxRetries
12
-
13
- @localQueueName
14
-
15
- @forwardReceivedMessagesTo
16
8
  @forwardReceivedMessagesToQueue
17
-
18
- @messageEndpointMappings
19
9
 
20
10
  @subscriptions
21
11
 
22
12
  @beanstalk
23
13
 
24
- @verbose
25
14
  @appResources
15
+
16
+
17
+ @config
26
18
 
27
19
  def log(string, ver=false)
28
20
  type = ver ? "VERB" : "INFO"
29
- if @verbose || !ver then
21
+ if @config.verbose || !ver then
30
22
  timestamp = Time.new.strftime( "%Y-%m-%d %H:%M:%S" )
31
23
  puts "[#{type}] #{timestamp} :: #{string}"
32
24
  end
33
25
  end
34
26
 
35
- def getValue( name, default=nil )
36
- value = ENV["#{name}"].nil? ? default : ENV["#{name}"];
37
- self.log "Env value: #{name}: #{value}", true
38
- return value
39
- end
40
-
41
- def loadMessageEndpointMappings()
42
- mapping = self.getValue( "MESSAGE_ENDPOINT_MAPPINGS" )
43
-
44
- messageEndpointMappings=Hash.new
45
- if !mapping.nil? then
46
- mapping.split( ";" ).each do |line|
47
- match = line.match( /(.+):(.+)/ )
48
- messageEndpointMappings[match[0]] = match[1]
49
- end
50
- end
51
-
52
- @messageEndpointMappings=messageEndpointMappings
53
-
54
- return self
55
- end
56
-
57
- def loadHandlerPathList()
58
- path = self.getValue( "MSGHANDLERPATH", "MessageHandler" )
59
- handlerPathList = Array.new
60
- path.split( ";" ).each do |path|
61
- path = path.strip.chomp( "/" )
62
- handlerPathList << path
63
- end
64
-
65
- @handlerPathList = handlerPathList
66
-
67
- return self
68
- end
69
-
70
-
71
- def loadHostSection()
72
- @appName = self.getValue( "APPNAME", "RServiceBus" )
73
- @localQueueName = @appName
74
- @errorQueueName = self.getValue( "ERROR_QUEUE_NAME", "error" )
75
- @maxRetries = self.getValue( "MAX_RETRIES", "5" ).to_i
76
- @forwardReceivedMessagesTo = self.getValue( "FORWARD_RECEIVED_MESSAGES_TO" )
77
-
78
- return self
79
- end
80
-
81
- def loadContracts()
82
- if self.getValue( "CONTRACTS" ).nil? then
83
- return self
84
- end
85
-
86
- self.getValue( "CONTRACTS" ).split( ";" ).each do |path|
87
- self.log "Loading contracts from, #{path}"
88
- require path
89
- end
90
- return self
91
- end
92
-
93
- def configureLogging()
94
- @verbose = !self.getValue( "VERBOSE", nil ).nil?
95
-
96
- return self
27
+ def configureAppResource
28
+ @appResources = ConfigureAppResource.new.getResources( ENV )
29
+ return self;
97
30
  end
98
31
 
99
- def configureBeanstalk
100
- beanstalkHost = self.getValue( "BEANSTALK", "localhost:11300" )
32
+ def connectTpBeanstalk
101
33
  begin
102
- @beanstalk = Beanstalk::Pool.new([beanstalkHost])
34
+ @beanstalk = Beanstalk::Pool.new([@beanstalkHost])
103
35
  rescue Exception => e
104
36
  if e.message == "Beanstalk::NotConnected" then
105
37
  puts "Error connecting to Beanstalk"
@@ -114,20 +46,18 @@ class Host
114
46
  return self
115
47
  end
116
48
 
117
- def configureAppResource
118
- @appResources = ConfigureAppResource.new.getResources( ENV )
119
- return self;
120
- end
121
-
122
49
  def initialize()
123
50
 
124
- self.configureLogging()
51
+ @config = ConfigFromEnv.new
52
+ .configureLogging()
125
53
  .loadHostSection()
126
54
  .configureBeanstalk()
127
- .configureAppResource()
128
55
  .loadContracts()
129
56
  .loadMessageEndpointMappings()
130
- .loadHandlerPathList()
57
+ .loadHandlerPathList();
58
+
59
+ self.configureAppResource()
60
+ .connectTpBeanstalk()
131
61
  .loadHandlers()
132
62
  .loadSubscriptions()
133
63
  .sendSubscriptions()
@@ -165,7 +95,7 @@ class Host
165
95
  def loadHandlers()
166
96
  log "Load Message Handlers"
167
97
 
168
- @handlerPathList.each do |path|
98
+ @config.handlerPathList.each do |path|
169
99
  self.loadHandlersFromPath(path)
170
100
  end
171
101
 
@@ -174,7 +104,7 @@ class Host
174
104
 
175
105
  def sendSubscriptions
176
106
  log "Send Subscriptions"
177
- @messageEndpointMappings.each do |eventName,queueName|
107
+ @config.messageEndpointMappings.each do |eventName,queueName|
178
108
  log "Checking, " + eventName + " for Event", true
179
109
  if eventName.end_with?( "Event" ) then
180
110
  log eventName + ", is an event. About to send subscription to, " + queueName, true
@@ -192,7 +122,7 @@ class Host
192
122
 
193
123
  redis = Redis.new
194
124
 
195
- prefix = @appName + ".Subscriptions."
125
+ prefix = @config.appName + ".Subscriptions."
196
126
  subscriptions = redis.keys prefix + "*Event"
197
127
 
198
128
  subscriptions.each do |subscriptionName|
@@ -214,7 +144,7 @@ class Host
214
144
  def addSubscrption( eventName, queueName )
215
145
  log "Adding subscrption for, " + eventName + ", to, " + queueName
216
146
  redis = Redis.new
217
- key = @appName + ".Subscriptions." + eventName
147
+ key = @config.appName + ".Subscriptions." + eventName
218
148
  redis.sadd key, queueName
219
149
 
220
150
  if @subscriptions[eventName].nil? then
@@ -226,10 +156,10 @@ class Host
226
156
  def run
227
157
  log "Starting the Host"
228
158
 
229
- log "Watching, " + @localQueueName
230
- @beanstalk.watch( @localQueueName )
231
- if !@forwardReceivedMessagesTo.nil? then
232
- log "Forwarding all received messages to: " + @forwardReceivedMessagesTo.to_s
159
+ log "Watching, " + @config.localQueueName
160
+ @beanstalk.watch( @config.localQueueName )
161
+ if !@config.forwardReceivedMessagesTo.nil? then
162
+ log "Forwarding all received messages to: " + @config.forwardReceivedMessagesTo.to_s
233
163
  end
234
164
 
235
165
  self.StartListeningToEndpoints
@@ -242,15 +172,15 @@ class Host
242
172
  loop do
243
173
  job = @beanstalk.reserve
244
174
  body = job.body
245
- retries = @maxRetries
175
+ retries = @config.maxRetries
246
176
  begin
247
177
  @msg = YAML::load(body)
248
178
  if @msg.msg.class.name == "RServiceBus::Subscription" then
249
179
  self.addSubscrption( @msg.msg.eventName, @msg.returnAddress )
250
180
  else
251
181
  self.HandleMessage()
252
- if !@forwardReceivedMessagesTo.nil? then
253
- self._SendAlreadyWrappedAndSerialised(body,@forwardReceivedMessagesTo)
182
+ if !@config.forwardReceivedMessagesTo.nil? then
183
+ self._SendAlreadyWrappedAndSerialised(body,@config.forwardReceivedMessagesTo)
254
184
  end
255
185
  end
256
186
  job.delete
@@ -260,9 +190,9 @@ class Host
260
190
  errorString = e.message + ". " + e.backtrace[0]
261
191
  log errorString
262
192
 
263
- @msg.addErrorMsg( @localQueueName, errorString )
193
+ @msg.addErrorMsg( @config.localQueueName, errorString )
264
194
  serialized_object = YAML::dump(@msg)
265
- self._SendAlreadyWrappedAndSerialised(serialized_object, @errorQueueName)
195
+ self._SendAlreadyWrappedAndSerialised(serialized_object, @config.errorQueueName)
266
196
  end
267
197
  end
268
198
  end
@@ -297,7 +227,7 @@ class Host
297
227
  def _SendNeedsWrapping( msg, queueName )
298
228
  log "Bus._SendNeedsWrapping", true
299
229
 
300
- rMsg = RServiceBus::Message.new( msg, @localQueueName )
230
+ rMsg = RServiceBus::Message.new( msg, @config.localQueueName )
301
231
  serialized_object = YAML::dump(rMsg)
302
232
  log "Sending: " + msg.class.name + " to: " + queueName, true
303
233
  self._SendAlreadyWrappedAndSerialised( serialized_object, queueName )
@@ -315,13 +245,13 @@ class Host
315
245
 
316
246
 
317
247
  msgName = msg.class.name
318
- if !@messageEndpointMappings.has_key?( msgName ) then
248
+ if !@config.messageEndpointMappings.has_key?( msgName ) then
319
249
  log "No end point mapping found for: " + msgName
320
250
  log "**** Check in RServiceBus.yml that the section MessageEndpointMappings contains an entry named : " + msgName
321
251
  raise "No end point mapping found for: " + msgName
322
252
  end
323
253
 
324
- queueName = @messageEndpointMappings[msgName]
254
+ queueName = @config.messageEndpointMappings[msgName]
325
255
 
326
256
  self._SendNeedsWrapping( msg, queueName )
327
257
  end
@@ -347,7 +277,7 @@ class Host
347
277
  log "Bus.Subscribe: " + eventName, true
348
278
 
349
279
 
350
- queueName = @messageEndpointMappings[eventName]
280
+ queueName = @config.messageEndpointMappings[eventName]
351
281
  subscription = Subscription.new( eventName )
352
282
 
353
283
 
@@ -0,0 +1,16 @@
1
+ require "redis"
2
+
3
+ class RedisAppResource<AppResource
4
+
5
+ @connection
6
+
7
+ def initialize( uri )
8
+ super(uri)
9
+ @connection = Redis.new
10
+ end
11
+
12
+ def getResource
13
+ return @connection
14
+ end
15
+
16
+ end
@@ -1,12 +1,16 @@
1
1
  module RServiceBus
2
2
 
3
- def RServiceBus.convertDTOToJson( obj )
3
+ def RServiceBus.convertDTOToHash( obj )
4
4
  hash = {};
5
5
  obj.instance_variables.each {|var| hash[var.to_s.delete("@")] = obj.instance_variable_get(var) }
6
-
7
- newOne = hash.to_json
8
6
 
9
- return newOne
7
+ return hash
8
+ end
9
+
10
+ def RServiceBus.convertDTOToJson( obj )
11
+ hash = RServiceBus.convertDTOToHash(obj)
12
+
13
+ return hash.to_json
10
14
  end
11
15
 
12
16
  end
data/lib/rservicebus.rb CHANGED
@@ -12,7 +12,10 @@ require "rservicebus/Message"
12
12
  require "rservicebus/Subscription"
13
13
  require "rservicebus/HandlerLoader"
14
14
  require "rservicebus/ConfigureAppResource"
15
+ require "rservicebus/AppResource"
16
+ require "rservicebus/RedisAppResource"
15
17
  require "rservicebus/Host"
18
+ require "rservicebus/Config"
16
19
 
17
20
 
18
21
  module RServiceBus
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.3
4
+ version: 0.0.4
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.rb
22
23
  - lib/rservicebus/Config.rb
23
24
  - lib/rservicebus/ConfigureAppResource.rb
24
25
  - lib/rservicebus/ErrorMessage.rb
@@ -26,6 +27,7 @@ files:
26
27
  - lib/rservicebus/helper_functions.rb
27
28
  - lib/rservicebus/Host.rb
28
29
  - lib/rservicebus/Message.rb
30
+ - lib/rservicebus/RedisAppResource.rb
29
31
  - lib/rservicebus/Subscription.rb
30
32
  - lib/rservicebus.rb
31
33
  - bin/rservicebus