Skip to content

Commit

Permalink
Merge pull request #40 from ba-st/connection_retry
Browse files Browse the repository at this point in the history
Add retry logic to RabbitMQClient
  • Loading branch information
gcotelli authored Jun 10, 2022
2 parents a4e4135 + bbca002 commit feb3f3d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
17 changes: 12 additions & 5 deletions source/Ansible-RabbitMQ-Tests/RabbitMQTextReverserTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ RabbitMQTextReverserTest >> setUp [

super setUp.
reversedTexts := OrderedCollection new.
workerProcess := [ ( RabbitMQTextReverser workingWith: self ) start ] newProcess.
workerProcess := [
| worker |
worker := RabbitMQTextReverser workingWith: self.
[ worker start ] ensure: [ worker stop ]
] newProcess.
workerProcess
name: 'Text reverser worker';
priority: Processor userBackgroundPriority
Expand Down Expand Up @@ -103,11 +107,14 @@ RabbitMQTextReverserTest >> withLocalhostConnectionDo: block [
| connection |

connection := AmqpConnectionBuilder usingAMQP091Protocol build.
connection open.

connection
whenConnected: [
block value: connection.
connection close
connection open.
[ block value: connection ] ensure: [ connection close ]
]
whenNot: [ :error |
connection hardClose.
self fail: error messageText
]
whenNot: [ :error | self fail: error messageText ]
]
66 changes: 60 additions & 6 deletions source/Ansible-RabbitMQ/RabbitMQClient.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ RabbitMQClient class >> isAbstract [
^ self = RabbitMQClient
]

{ #category : #private }
{ #category : #'private - configuring' }
RabbitMQClient >> configureConnection: builder [

self subclassResponsibility
]

{ #category : #'private - configuring' }
RabbitMQClient >> connectivityErrors [

^ NetworkError
]

{ #category : #initialization }
RabbitMQClient >> initializeConnection [

Expand All @@ -36,13 +42,38 @@ RabbitMQClient >> initializeConnection [
connection := builder build
]

{ #category : #private }
{ #category : #'private - connecting' }
RabbitMQClient >> logFailedConnectionAttempt: attemptNumber dueTo: error [

LaunchpadLogRecord emitError: ( 'Attempt #<1p>/<2p> to connect to RabbitMQ failed: <3s>'
expandMacrosWith: attemptNumber
with: self retryCount + 1
with: error messageText )
]

{ #category : #'private - connecting' }
RabbitMQClient >> openConnection [

connection
whenConnected: [
LaunchpadLogRecord emitInfo: 'Connecting to RabbitMQ' during: [ connection open ] ]
whenNot: [ :error | error signal ]
self withSuccessfulConnectionDo: [ :succesfulConnection |
LaunchpadLogRecord emitInfo: 'Connecting to RabbitMQ' during: [
self
try: [ succesfulConnection open ]
onConnectivityErrorDo: [ :attemptNumber :error |
self logFailedConnectionAttempt: attemptNumber dueTo: error ]
]
]
]

{ #category : #'private - configuring' }
RabbitMQClient >> options [

^ Dictionary new
]

{ #category : #'private - configuring' }
RabbitMQClient >> retryCount [

^ self options at: #retryCount ifAbsent: [ 2 ]
]

{ #category : #controlling }
Expand Down Expand Up @@ -74,3 +105,26 @@ RabbitMQClient >> stop [
whenConnected: [ connection close ]
whenNot: [ LaunchpadLogRecord emitWarning: 'RabbitMQ connection was already closed.' ]
]

{ #category : #'private - connecting' }
RabbitMQClient >> try: aBlock onConnectivityErrorDo: failBlock [

Retry value: aBlock configuredBy: [ :retry |
retry
upTo: self retryCount;
on: self connectivityErrors evaluating: failBlock.
self options at: #retry ifPresent: [ :action | action value: retry ]
]
]

{ #category : #'private - connecting' }
RabbitMQClient >> withSuccessfulConnectionDo: aBlock [

self
try: [ connection whenConnected: [ aBlock value: connection ] whenNot: [ :error | error signal ] ]
onConnectivityErrorDo: [ :attemptNumber :error |
self logFailedConnectionAttempt: attemptNumber dueTo: error.
LaunchpadLogRecord emitWarning: 'Reconnecting socket to RabbitMQ'.
connection initializeSocketConnection
]
]
8 changes: 6 additions & 2 deletions source/BaselineOfAnsible/BaselineOfAnsible.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ BaselineOfAnsible >> setUpDependencies: spec [

spec
baseline: 'Buoy' with: [ spec repository: 'github://ba-st/Buoy:v6' ];
project: 'Buoy-SUnit' copyFrom: 'Buoy' with: [ spec loads: 'Dependent-SUnit-Extensions' ]
project: 'Buoy-SUnit' copyFrom: 'Buoy' with: [ spec loads: 'Dependent-SUnit-Extensions' ].

spec
baseline: 'Hyperspace' with: [ spec repository: 'github://ba-st/Hyperspace:v4' ];
project: 'Hyperspace-Deployment' copyFrom: 'Hyperspace' with: [ spec loads: 'Deployment' ]
]

{ #category : #baselines }
Expand All @@ -61,7 +65,7 @@ BaselineOfAnsible >> setUpDeploymentPackages: spec [

spec
package: 'Ansible-RabbitMQ'
with: [ spec requires: #( 'Ansible-Protocol-091' 'Launchpad-Deployment' ) ];
with: [ spec requires: #( 'Ansible-Protocol-091' 'Launchpad-Deployment' 'Hyperspace-Deployment' ) ];
group: 'RabbitMQ' with: 'Ansible-RabbitMQ'
]

Expand Down

0 comments on commit feb3f3d

Please sign in to comment.