rservicebus 0.0.2 → 0.0.3
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/rservicebus.rb +1 -0
- data/lib/rservicebus/Agent.rb +2 -2
- data/lib/rservicebus/ConfigureAppResource.rb +53 -0
- data/lib/rservicebus/HandlerLoader.rb +49 -31
- data/lib/rservicebus/Host.rb +30 -11
- metadata +3 -2
data/lib/rservicebus.rb
CHANGED
data/lib/rservicebus/Agent.rb
CHANGED
@@ -4,8 +4,8 @@ require 'beanstalk-client'
|
|
4
4
|
class Agent
|
5
5
|
@beanstalk
|
6
6
|
|
7
|
-
def initialize()
|
8
|
-
@beanstalk = Beanstalk::Pool.new(
|
7
|
+
def initialize(url=['localhost:11300'])
|
8
|
+
@beanstalk = Beanstalk::Pool.new(url)
|
9
9
|
end
|
10
10
|
|
11
11
|
def sendMsg(messageObj, queueName, returnAddress=nil)
|
@@ -0,0 +1,53 @@
|
|
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
|
+
|
31
|
+
class ConfigureAppResource
|
32
|
+
|
33
|
+
def getResources( env )
|
34
|
+
resources = Hash.new
|
35
|
+
|
36
|
+
env.each do |k,v|
|
37
|
+
if v.is_a?(String) and
|
38
|
+
k.start_with?( "RSB_" ) then
|
39
|
+
uri = URI.parse( v )
|
40
|
+
case uri.scheme
|
41
|
+
when "redis"
|
42
|
+
resources[k.sub( "RSB_", "" )] = RedisAppResource.new( uri )
|
43
|
+
else
|
44
|
+
abort("Scheme, #{uri.scheme}, not recognised when configuring app resource, #{k}=#{v}");
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
return resources
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -5,6 +5,7 @@ class HandlerLoader
|
|
5
5
|
attr_reader :messageName, :handler
|
6
6
|
|
7
7
|
@host
|
8
|
+
@appResources
|
8
9
|
|
9
10
|
@baseDir
|
10
11
|
@filepath
|
@@ -15,11 +16,9 @@ class HandlerLoader
|
|
15
16
|
@messageName
|
16
17
|
@handler
|
17
18
|
|
18
|
-
def initialize(
|
19
|
+
def initialize( host, appResources )
|
19
20
|
@host = host
|
20
|
-
|
21
|
-
@baseDir = baseDir
|
22
|
-
@filePath = filePath
|
21
|
+
@appResources = appResources
|
23
22
|
end
|
24
23
|
|
25
24
|
def getMessageName( baseDir, fileName )
|
@@ -27,7 +26,7 @@ class HandlerLoader
|
|
27
26
|
if name.count( "/" ) == 0 then
|
28
27
|
return name.match( /(.+)\./ )[1]
|
29
28
|
end
|
30
|
-
|
29
|
+
|
31
30
|
if name.count( "/" ) == 1 then
|
32
31
|
return name.match( /\/(.+)\./ )[1]
|
33
32
|
end
|
@@ -40,53 +39,72 @@ class HandlerLoader
|
|
40
39
|
end
|
41
40
|
|
42
41
|
def getRequirePath( filePath )
|
42
|
+
if !filePath.start_with?( "/" ) then
|
43
|
+
filePath = "./" + filePath
|
44
|
+
end
|
45
|
+
|
43
46
|
if File.exists?( filePath ) then
|
44
47
|
return filePath.sub( ".rb", "")
|
45
48
|
end
|
46
|
-
|
47
|
-
if File.exists?( "./" + filePath ) then
|
48
|
-
return "./" + filePath.sub( ".rb", "")
|
49
|
-
end
|
50
49
|
|
51
50
|
abort( "Filepath, " + filePath + ", given for MessageHandler require doesn't exist" );
|
52
51
|
end
|
53
52
|
|
54
|
-
def
|
55
|
-
|
56
|
-
|
57
|
-
@handlerName = @filePath.sub( ".rb", "").sub( @baseDir, "MessageHandler" ).gsub( "/", "_" )
|
58
|
-
|
59
|
-
puts @handlerName
|
60
|
-
puts @filePath + ":" + @messageName + ":" + @handlerName
|
53
|
+
def getHandlerName( baseDir, filePath )
|
54
|
+
handlerName = filePath.sub( ".rb", "").sub( baseDir, "MessageHandler" ).gsub( "/", "_" )
|
55
|
+
return handlerName
|
61
56
|
end
|
62
57
|
|
63
|
-
def loadHandlerFromFile
|
64
|
-
require
|
58
|
+
def loadHandlerFromFile( requirePath, handlerName, filePath )
|
59
|
+
require requirePath
|
65
60
|
begin
|
66
|
-
|
61
|
+
handler = Object.const_get(handlerName).new();
|
67
62
|
rescue Exception => e
|
68
|
-
puts "Expected class name: " +
|
69
|
-
puts "**** Check in " +
|
63
|
+
puts "Expected class name: " + handlerName + ", not found after require: " + requirePath
|
64
|
+
puts "**** Check in " + filePath + " that the class is named : " + handlerName
|
70
65
|
puts "( In case its not that )"
|
71
66
|
raise e
|
72
67
|
end
|
68
|
+
|
69
|
+
return handler
|
73
70
|
end
|
74
71
|
|
75
|
-
def setBusAttributeIfRequested
|
76
|
-
if defined?(
|
77
|
-
|
78
|
-
|
72
|
+
def setBusAttributeIfRequested( handler, handlerName )
|
73
|
+
if defined?( handler.Bus ) then
|
74
|
+
handler.Bus = @host
|
75
|
+
@host.log "Bus attribute set for: " + handlerName
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def setAppResources( handler, handlerName, appResources )
|
80
|
+
@host.log "Checking app resources for: #{handlerName}", true
|
81
|
+
appResources.each do |k,v|
|
82
|
+
if handler.class.method_defined?( k ) then
|
83
|
+
handler.instance_variable_set( "@#{k}", v.getResource() )
|
84
|
+
@host.log "App resource attribute, #{k}, set for: " + handlerName
|
85
|
+
end
|
79
86
|
end
|
80
87
|
end
|
81
88
|
|
82
|
-
def loadHandler()
|
89
|
+
def loadHandler(baseDir, filePath)
|
83
90
|
begin
|
84
|
-
self.
|
85
|
-
self.
|
86
|
-
self.
|
87
|
-
|
91
|
+
requirePath = self.getRequirePath( filePath )
|
92
|
+
messageName = self.getMessageName( baseDir, filePath )
|
93
|
+
handlerName = self.getHandlerName( baseDir, filePath )
|
94
|
+
|
95
|
+
@host.log "filePath: " + filePath, true
|
96
|
+
@host.log "requirePath: " + requirePath, true
|
97
|
+
@host.log "messageName: " + messageName, true
|
98
|
+
@host.log "handlerName: " + handlerName, true
|
99
|
+
|
100
|
+
handler = self.loadHandlerFromFile( requirePath, handlerName, filePath )
|
101
|
+
self.setBusAttributeIfRequested( handler, handlerName )
|
102
|
+
self.setAppResources( handler, handlerName, @appResources )
|
103
|
+
@host.log "Loaded Handler: " + handlerName + ", for, " + messageName
|
104
|
+
|
105
|
+
return messageName, handler
|
88
106
|
rescue Exception => e
|
89
|
-
puts "Exception loading handler from file: " +
|
107
|
+
puts "Exception loading handler from file: " + filePath
|
90
108
|
puts e.message
|
91
109
|
puts e.backtrace[0]
|
92
110
|
|
data/lib/rservicebus/Host.rb
CHANGED
@@ -11,7 +11,7 @@ class Host
|
|
11
11
|
@maxRetries
|
12
12
|
|
13
13
|
@localQueueName
|
14
|
-
|
14
|
+
|
15
15
|
@forwardReceivedMessagesTo
|
16
16
|
@forwardReceivedMessagesToQueue
|
17
17
|
|
@@ -22,6 +22,7 @@ class Host
|
|
22
22
|
@beanstalk
|
23
23
|
|
24
24
|
@verbose
|
25
|
+
@appResources
|
25
26
|
|
26
27
|
def log(string, ver=false)
|
27
28
|
type = ver ? "VERB" : "INFO"
|
@@ -81,8 +82,9 @@ class Host
|
|
81
82
|
if self.getValue( "CONTRACTS" ).nil? then
|
82
83
|
return self
|
83
84
|
end
|
84
|
-
|
85
|
-
self.getValue( "CONTRACTS" ).split( ";" ) do |path|
|
85
|
+
|
86
|
+
self.getValue( "CONTRACTS" ).split( ";" ).each do |path|
|
87
|
+
self.log "Loading contracts from, #{path}"
|
86
88
|
require path
|
87
89
|
end
|
88
90
|
return self
|
@@ -96,16 +98,33 @@ class Host
|
|
96
98
|
|
97
99
|
def configureBeanstalk
|
98
100
|
beanstalkHost = self.getValue( "BEANSTALK", "localhost:11300" )
|
99
|
-
|
101
|
+
begin
|
102
|
+
@beanstalk = Beanstalk::Pool.new([beanstalkHost])
|
103
|
+
rescue Exception => e
|
104
|
+
if e.message == "Beanstalk::NotConnected" then
|
105
|
+
puts "Error connecting to Beanstalk"
|
106
|
+
puts "***Most likely, beanstalk is not running. Start beanstalk, and try running this again."
|
107
|
+
puts "***If you still get this error, check beanstalk is running at, " + beanstalkHost
|
108
|
+
abort()
|
109
|
+
else
|
110
|
+
raise e
|
111
|
+
end
|
112
|
+
end
|
100
113
|
|
101
114
|
return self
|
102
115
|
end
|
103
116
|
|
117
|
+
def configureAppResource
|
118
|
+
@appResources = ConfigureAppResource.new.getResources( ENV )
|
119
|
+
return self;
|
120
|
+
end
|
121
|
+
|
104
122
|
def initialize()
|
105
123
|
|
106
|
-
self.
|
107
|
-
.
|
124
|
+
self.configureLogging()
|
125
|
+
.loadHostSection()
|
108
126
|
.configureBeanstalk()
|
127
|
+
.configureAppResource()
|
109
128
|
.loadContracts()
|
110
129
|
.loadMessageEndpointMappings()
|
111
130
|
.loadHandlerPathList()
|
@@ -119,6 +138,7 @@ class Host
|
|
119
138
|
def loadHandlersFromPath(baseDir, subDir="")
|
120
139
|
log "Load Message Handlers from baseDir, " + baseDir + ", subDir, " + subDir
|
121
140
|
log "Checking, " + baseDir, true
|
141
|
+
handlerLoader = HandlerLoader.new( self, @appResources )
|
122
142
|
|
123
143
|
@handlerList = {};
|
124
144
|
Dir[baseDir + "/" + subDir + "*"].each do |filePath|
|
@@ -128,14 +148,13 @@ class Host
|
|
128
148
|
if File.directory?( filePath ) then
|
129
149
|
self.loadHandlersFromPath( filePath.sub( baseDir ) )
|
130
150
|
else
|
131
|
-
|
132
|
-
handlerLoader.loadHandler
|
151
|
+
messageName, handler = handlerLoader.loadHandler( baseDir, filePath )
|
133
152
|
|
134
|
-
if !@handlerList.has_key?(
|
135
|
-
@handlerList[
|
153
|
+
if !@handlerList.has_key?( messageName ) then
|
154
|
+
@handlerList[messageName] = Array.new
|
136
155
|
end
|
137
156
|
|
138
|
-
@handlerList[
|
157
|
+
@handlerList[messageName] << handler;
|
139
158
|
end
|
140
159
|
end
|
141
160
|
end
|
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.
|
4
|
+
version: 0.0.3
|
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: 2012-06-
|
12
|
+
date: 2012-06-14 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A ruby implementation of NServiceBus
|
15
15
|
email: guy@guyirvine.com
|
@@ -20,6 +20,7 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- lib/rservicebus/Agent.rb
|
22
22
|
- lib/rservicebus/Config.rb
|
23
|
+
- lib/rservicebus/ConfigureAppResource.rb
|
23
24
|
- lib/rservicebus/ErrorMessage.rb
|
24
25
|
- lib/rservicebus/HandlerLoader.rb
|
25
26
|
- lib/rservicebus/helper_functions.rb
|