diff --git a/source/Superluminal-Service-Discovery-Examples/SuperluminalServiceDiscoveryExample.class.st b/source/Superluminal-Service-Discovery-Examples/SuperluminalServiceDiscoveryExample.class.st index 31e8ebe..ff833d7 100644 --- a/source/Superluminal-Service-Discovery-Examples/SuperluminalServiceDiscoveryExample.class.st +++ b/source/Superluminal-Service-Discovery-Examples/SuperluminalServiceDiscoveryExample.class.st @@ -39,28 +39,10 @@ SuperluminalServiceDiscoveryExample class >> version [ { #category : #'private - activation' } SuperluminalServiceDiscoveryExample >> basicStartWithin: context [ - | consulAgentLocation echoServiceLocation | - - consulAgentLocation := self configuration consulAgentLocation. - LaunchpadLogRecord emitInfo: 'Discovering dependencies' during: [ - echoServiceLocation := Retry - value: [ - ( ConsulAgentHttpAPIBasedDiscoveryClient queryingAgentOn: - consulAgentLocation ) withLocationOfService: #echo - do: [ :location | location ] - ifUnable: [ Error signal: 'Cannot discover #echo service' ] - ] - configuredBy: [ :retry | - retry - upTo: 3 timesEvery: self configuration retryDelayInMs; - on: Error evaluating: [ :attemptNumber :exception | - LaunchpadLogRecord emitWarning: - ( 'Attempt #<1p> failed with error: <2s>' expandMacrosWith: - attemptNumber - with: exception messageText ) - ] - ] - ]. + | echoServiceLocation | + + LaunchpadLogRecord emitInfo: 'Discovering dependencies' + during: [ echoServiceLocation := self discoverEchoServiceQueryingConsulAgent ]. RESTfulAPIClient cachingOnLocalMemory get: ( echoServiceLocation @@ -69,11 +51,36 @@ SuperluminalServiceDiscoveryExample >> basicStartWithin: context [ addPathSegment: self configuration message; yourself ) withSuccessfulResponseDo: [ :response | - response = self configuration message asUppercase ifFalse: [ Error signal: 'Invalid response received' ] ]. - + response = self configuration message asUppercase ifFalse: [ + Error signal: 'Invalid response received' ] + ]. self exitSuccess ] +{ #category : #'private - activation' } +SuperluminalServiceDiscoveryExample >> discoverEchoServiceQueryingConsulAgent [ + + ^ ( ConsulAgentHttpAPIBasedDiscoveryClient queryingAgentOn: self configuration consulAgentLocation + configuredBy: [ :options | options at: #retry put: self retryOptions ] ) + withLocationOfService: #echo + do: [ :location | location ] + ifUnable: [ Error signal: 'Cannot discover #echo service' ] +] + +{ #category : #'private - activation' } +SuperluminalServiceDiscoveryExample >> retryOptions [ + + ^ [ :retry | + retry + upTo: 3 timesEvery: self configuration retryDelayInMs; + on: Error evaluating: [ :attemptNumber :exception | + LaunchpadLogRecord emitWarning: + ( 'Attempt #<1p> failed with error: <2s>' expandMacrosWith: attemptNumber + with: exception messageText ) + ] + ] +] + { #category : #'error handling' } SuperluminalServiceDiscoveryExample >> stackTraceDumper [ diff --git a/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st b/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st index fba224e..5a7a07f 100644 --- a/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st +++ b/source/Superluminal-Service-Discovery/ConsulAgentHttpAPIBasedDiscoveryClient.class.st @@ -8,14 +8,24 @@ Class { #name : #ConsulAgentHttpAPIBasedDiscoveryClient, #superclass : #ServiceDiscoveryClient, #instVars : [ - 'agentLocation', 'fallbackClient', - 'apiClient' + 'apiClient', + 'options' ], #category : #'Superluminal-Service-Discovery' } -{ #category : #'Instance creation' } +{ #category : #'private - instance creation' } +ConsulAgentHttpAPIBasedDiscoveryClient class >> configuredBy: aBlock chainedWith: aServiceDiscoveryClient [ + + | options | + + options := Dictionary new. + aBlock cull: options. + ^ self new initializeConfiguredBy: options chainedWith: aServiceDiscoveryClient +] + +{ #category : #'instance creation' } ConsulAgentHttpAPIBasedDiscoveryClient class >> queryingAgentOn: aConsulAgentLocation [ ^ self queryingAgentOn: aConsulAgentLocation chainedWith: NullServiceDiscoveryClient new @@ -24,7 +34,33 @@ ConsulAgentHttpAPIBasedDiscoveryClient class >> queryingAgentOn: aConsulAgentLoc { #category : #'instance creation' } ConsulAgentHttpAPIBasedDiscoveryClient class >> queryingAgentOn: aConsulAgentLocation chainedWith: aServiceDiscoveryClient [ - ^ self new initializeQueryingAgentOn: aConsulAgentLocation chainedWith: aServiceDiscoveryClient + ^ self configuredBy: [ :options | options at: #agentLocation put: aConsulAgentLocation ] + chainedWith: aServiceDiscoveryClient +] + +{ #category : #'instance creation' } +ConsulAgentHttpAPIBasedDiscoveryClient class >> queryingAgentOn: aConsulAgentLocation configuredBy: aBlock [ + + ^ self queryingAgentOn: aConsulAgentLocation + configuredBy: aBlock + chainedWith: NullServiceDiscoveryClient new +] + +{ #category : #'instance creation' } +ConsulAgentHttpAPIBasedDiscoveryClient class >> queryingAgentOn: aConsulAgentLocation configuredBy: aBlock chainedWith: aServiceDiscoveryClient [ + + ^ self + configuredBy: [ :options | + options at: #agentLocation put: aConsulAgentLocation. + aBlock cull: options + ] + chainedWith: aServiceDiscoveryClient +] + +{ #category : #private } +ConsulAgentHttpAPIBasedDiscoveryClient >> agentLocation [ + + ^ options at: #agentLocation ] { #category : #private } @@ -33,7 +69,7 @@ ConsulAgentHttpAPIBasedDiscoveryClient >> discoverLocationOf: serviceName ifFoun | serviceHealthList | ^ apiClient - getAt: agentLocation / 'v1' / 'health' / 'service' / serviceName + getAt: self agentLocation / 'v1' / 'health' / 'service' / serviceName configuredBy: [ :request | ( request queryString: [ :queryString | queryString fieldNamed: 'filter' pairedTo: 'Checks.Status == passing' ] ) @@ -50,9 +86,9 @@ ConsulAgentHttpAPIBasedDiscoveryClient >> discoverLocationOf: serviceName ifFoun ] { #category : #initialization } -ConsulAgentHttpAPIBasedDiscoveryClient >> initializeQueryingAgentOn: aConsulAgentLocation chainedWith: aServiceDiscoveryClient [ +ConsulAgentHttpAPIBasedDiscoveryClient >> initializeConfiguredBy: anOptionsDictionary chainedWith: aServiceDiscoveryClient [ - agentLocation := aConsulAgentLocation. + options := anOptionsDictionary. fallbackClient := aServiceDiscoveryClient. apiClient := RESTfulAPIClient cachingOnLocalMemory ] @@ -64,7 +100,8 @@ ConsulAgentHttpAPIBasedDiscoveryClient >> withLocationOfService: serviceName do: configuredBy: [ :retry | retry upTo: 2; - on: NetworkError , HTTPError + on: NetworkError , HTTPError. + options at: #retry ifPresent: [ :action | action value: retry ] ] ]