rservicebus 0.1.15 → 0.1.16
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rservicebus/Transporter.rb +115 -128
- metadata +1 -1
@@ -3,153 +3,140 @@ require 'rservicebus'
|
|
3
3
|
require 'net/ssh/gateway'
|
4
4
|
|
5
5
|
module RServiceBus
|
6
|
-
|
7
|
-
class CouldNotConnectToDestination<StandardError
|
8
|
-
end
|
9
|
-
|
10
|
-
|
11
|
-
#ToDo: Poison Message? Can I bury with timeout in beanstalk ?
|
12
|
-
#Needs to end up on an error queue, destination queue may be down.
|
13
|
-
|
14
|
-
|
15
|
-
class Transporter
|
16
6
|
|
17
|
-
|
18
|
-
if verbose == false ||
|
19
|
-
( !ENV["VERBOSE"].nil? && ENV["VERBOSE"].upcase == "TRUE") then
|
20
|
-
puts string
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def getValue( name, default=nil )
|
25
|
-
value = ( ENV[name].nil? || ENV[name] == "" ) ? default : ENV[name];
|
26
|
-
log "Env value: #{name}: #{value}"
|
27
|
-
return value
|
7
|
+
class CouldNotConnectToDestination<StandardError
|
28
8
|
end
|
29
9
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
log "Connected to, #{sourceQueueName}@#{sourceUrl}"
|
10
|
+
|
11
|
+
#ToDo: Poison Message? Can I bury with timeout in beanstalk ?
|
12
|
+
#Needs to end up on an error queue, destination queue may be down.
|
13
|
+
|
14
|
+
|
15
|
+
class Transporter
|
37
16
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
puts "***If you still get this error, check beanstalk is running at, #{sourceUrl}"
|
44
|
-
else
|
45
|
-
puts e.message
|
46
|
-
puts e.backtrace
|
17
|
+
def log( string, verbose=false )
|
18
|
+
if verbose == false ||
|
19
|
+
( !ENV["VERBOSE"].nil? && ENV["VERBOSE"].upcase == "TRUE") then
|
20
|
+
puts string
|
21
|
+
end
|
47
22
|
end
|
48
|
-
abort();
|
49
|
-
end
|
50
|
-
|
51
|
-
def connectToRemoteSSH( remoteHostName )
|
52
|
-
return if @remoteHostName == remoteHostName
|
53
23
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
@gateway = Net::SSH::Gateway.new(remoteHostName, @remoteUserName)
|
24
|
+
def getValue( name, default=nil )
|
25
|
+
value = ( ENV[name].nil? || ENV[name] == "" ) ? default : ENV[name];
|
26
|
+
log "Env value: #{name}: #{value}"
|
27
|
+
return value
|
28
|
+
end
|
60
29
|
|
61
|
-
|
62
|
-
|
63
|
-
|
30
|
+
def connectToSourceBeanstalk
|
31
|
+
sourceQueueName = getValue( 'SOURCE_QUEUE_NAME', "transport-out" )
|
32
|
+
sourceUrl = getValue( 'SOURCE_URL', "127.0.0.1:11300" )
|
33
|
+
@source = Beanstalk::Pool.new([sourceUrl])
|
34
|
+
@source.watch sourceQueueName
|
35
|
+
|
36
|
+
log "Connected to, #{sourceQueueName}@#{sourceUrl}"
|
37
|
+
|
38
|
+
rescue Exception => e
|
39
|
+
puts "Error connecting to Beanstalk"
|
40
|
+
puts "Host string, #{sourceUrl}"
|
41
|
+
if e.message == "Beanstalk::NotConnected" then
|
42
|
+
puts "***Most likely, beanstalk is not running. Start beanstalk, and try running this again."
|
43
|
+
puts "***If you still get this error, check beanstalk is running at, #{sourceUrl}"
|
44
|
+
else
|
45
|
+
puts e.message
|
46
|
+
puts e.backtrace
|
47
|
+
end
|
48
|
+
abort();
|
49
|
+
end
|
64
50
|
|
65
|
-
return @gateway
|
66
|
-
end
|
67
|
-
|
68
|
-
def disconnectFromRemoteSSH
|
69
|
-
return if @gateway.nil?
|
70
51
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
52
|
+
def disconnect
|
53
|
+
log "Disconnect from, #{@remoteUserName}@#{@remoteHostName}/#{@remoteQueueName}"
|
54
|
+
@gateway.shutdown! unless @gateway.nil?
|
55
|
+
@gateway = nil
|
56
|
+
@remoteHostName = nil
|
57
|
+
|
58
|
+
@destination.close unless @destination.nil?
|
59
|
+
@destination = nil
|
60
|
+
|
61
|
+
@remoteUserName = nil
|
62
|
+
@remoteQueueName = nil
|
63
|
+
end
|
80
64
|
|
81
|
-
#Test connection
|
82
|
-
return if @remoteQueueName == remoteQueueName
|
83
65
|
|
66
|
+
def connect( remoteHostName )
|
67
|
+
log "connect called, #{remoteHostName}", true
|
68
|
+
if @gateway.nil? || remoteHostName != @remoteHostName || @destination.nil? then
|
69
|
+
self.disconnect
|
70
|
+
end
|
84
71
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
72
|
+
if @gateway.nil? then
|
73
|
+
#Get destination url from job
|
74
|
+
@remoteHostName = remoteHostName
|
75
|
+
@remoteUserName = getValue( "REMOTE_USER_#{remoteHostName.upcase}", "beanstalk" )
|
76
|
+
|
77
|
+
log "Connect SSH, #{@remoteUserName}@#{@remoteHostName}"
|
78
|
+
# Open port 27018 to forward to 127.0.0.11300 on the remote host
|
79
|
+
@gateway = Net::SSH::Gateway.new(@remoteHostName, @remoteUserName)
|
80
|
+
@gateway.open('127.0.0.1', 11300, 27018)
|
81
|
+
log "Connected to SSH, #{@remoteUserName}@#{@remoteHostName}"
|
82
|
+
|
83
|
+
begin
|
84
|
+
destinationUrl = '127.0.0.1:27018'
|
85
|
+
log "Connect to Remote Beanstalk, #{destinationUrl}", true
|
86
|
+
@destination = Beanstalk::Pool.new([destinationUrl])
|
87
|
+
log "Connected to Remote Beanstalk, #{destinationUrl}"
|
88
|
+
rescue Exception => e
|
89
|
+
if e.message == "Beanstalk::NotConnected" then
|
90
|
+
puts "***Could not connect to destination, check beanstalk is running at, #{destinationUrl}"
|
91
|
+
raise CouldNotConnectToDestination.new
|
92
|
+
end
|
93
|
+
raise
|
94
|
+
end
|
93
95
|
end
|
94
|
-
raise
|
95
96
|
end
|
96
97
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
98
|
+
def process
|
99
|
+
#Get the next job from the source queue
|
100
|
+
job = @source.reserve @timeout
|
101
|
+
msg = YAML::load(job.body)
|
102
|
+
|
103
|
+
self.connect( msg.remoteHostName )
|
104
|
+
|
105
|
+
@remoteQueueName = msg.remoteQueueName
|
106
|
+
log "Put msg, #{msg.remoteQueueName}", true
|
107
|
+
@destination.use( msg.remoteQueueName )
|
108
|
+
@destination.put( job.body )
|
109
|
+
log "Msg put, #{msg.remoteQueueName}"
|
110
|
+
|
111
|
+
if !ENV['AUDIT_QUEUE_NAME'].nil? then
|
112
|
+
@source.use ENV['AUDIT_QUEUE_NAME']
|
113
|
+
@source.put job.body
|
114
|
+
end
|
115
|
+
#removeJob
|
116
|
+
job.delete
|
117
|
+
|
118
|
+
log "Job sent to, #{@remoteUserName}@#{@remoteHostName}/#{@remoteQueueName}"
|
119
|
+
|
120
|
+
|
121
|
+
rescue Exception => e
|
122
|
+
self.disconnect
|
123
|
+
if e.message == "TIMED_OUT" then
|
124
|
+
log "No Msg", true
|
125
|
+
return
|
126
|
+
end
|
127
|
+
raise e
|
127
128
|
end
|
128
|
-
#removeJob
|
129
|
-
job.delete
|
130
129
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
return
|
130
|
+
def Run
|
131
|
+
@timeout = getValue( 'TIMEOUT', 5 )
|
132
|
+
connectToSourceBeanstalk
|
133
|
+
while true
|
134
|
+
process
|
135
|
+
end
|
136
|
+
self.disconnectFromRemoteSSH
|
139
137
|
end
|
140
|
-
raise e
|
141
138
|
end
|
142
139
|
|
143
|
-
|
144
|
-
@timeout = getValue( 'TIMEOUT', 5 )
|
145
|
-
connectToSourceBeanstalk
|
146
|
-
while true
|
147
|
-
process
|
148
|
-
end
|
149
|
-
self.disconnectFromRemoteSSH
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
|
140
|
+
|
154
141
|
end
|
155
142
|
|