rservicebus 0.1.15 → 0.1.16

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.
Files changed (2) hide show
  1. data/lib/rservicebus/Transporter.rb +115 -128
  2. 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
- def log( string, verbose=false )
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
- 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}"
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
- 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
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
- self.disconnectFromRemoteSSH
55
-
56
- #Get destination url from job
57
- @remoteHostName = remoteHostName
58
- @remoteUserName = getValue( "REMOTE_USER_#{remoteHostName.upcase}", "beanstalk" )
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
- # Open port 27018 to forward to 127.0.0.11300 on the remote host
62
- @gateway.open('127.0.0.1', 11300, 27018)
63
- log "Connect to Remote SSH, #{@remoteHostName}"
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
- log "Disconnect from Remote SSH, #{@remoteHostName}"
72
- @gateway.shutdown!
73
- @remoteHostName = nil
74
- @gateway = nil
75
- end
76
-
77
-
78
- def connectToRemoteBeanstalk( remoteHostName, remoteQueueName )
79
- self.connectToRemoteSSH( remoteHostName )
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
- log "Connect to Remote Beanstalk, #{remoteQueueName}"
86
- begin
87
- destinationUrl = '127.0.0.1:27018'
88
- @destination = Beanstalk::Pool.new([destinationUrl])
89
- rescue Exception => e
90
- if e.message == "Beanstalk::NotConnected" then
91
- puts "***Could not connect to destination, check beanstalk is running at, #{destinationUrl}"
92
- raise CouldNotConnectToDestination.new
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
- log "Use queue, #{remoteQueueName}", true
98
- @destination.use( remoteQueueName )
99
- @remoteQueueName = remoteQueueName
100
- end
101
-
102
- def disconnectFromRemoteBeanstalk
103
- self.disconnectFromRemoteSSH
104
- return if @destination.nil?
105
-
106
- log "Disconnect from Remote Beanstalk, #{@remoteQueueName}"
107
- @destination.close
108
- @remoteQueueName = nil
109
- end
110
-
111
-
112
- def process
113
- #Get the next job from the source queue
114
- job = @source.reserve @timeout
115
- msg = YAML::load(job.body)
116
-
117
- # log "job: #{job.body}", true
118
-
119
- self.connectToRemoteBeanstalk( msg.remoteHostName, msg.remoteQueueName )
120
-
121
- log "Put msg", true
122
- @destination.put( job.body )
123
-
124
- if !ENV['AUDIT_QUEUE_NAME'].nil? then
125
- @source.use ENV['AUDIT_QUEUE_NAME']
126
- @source.put job.body
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
- log "Job sent to, #{@remoteUserName}@#{@remoteHostName}/#{@remoteQueueName}"
132
-
133
-
134
- rescue Exception => e
135
- self.disconnectFromRemoteSSH
136
- if e.message == "TIMED_OUT" then
137
- log "No Msg", true
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
- def Run
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
 
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.1.15
4
+ version: 0.1.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: