@@ -35,6 +35,7 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
3535 {
3636 try
3737 {
38+ logger . LogInformation ( $ "Performing sync for { nameof ( HeartbeatEndpointSettingsSyncHostedService ) } ") ;
3839 await PerformSync ( cancellationToken ) ;
3940 }
4041 catch ( Exception ex ) when ( ex is not OperationCanceledException )
@@ -62,8 +63,9 @@ async Task PerformSync(CancellationToken cancellationToken)
6263
6364 async Task PurgeMonitoringDataThatDoesNotNeedToBeTracked ( CancellationToken cancellationToken )
6465 {
65- ILookup < string , Guid > monitorEndpointsLookup = endpointInstanceMonitoring . GetEndpoints ( )
66- . Where ( view => view . IsNotSendingHeartbeats )
66+ EndpointsView [ ] endpointsViews = endpointInstanceMonitoring . GetEndpoints ( ) ;
67+ ILookup < string , Guid > monitorEndpointsLookup = endpointsViews
68+ . Where ( view => ! view . IsSendingHeartbeats )
6769 . ToLookup ( view => view . Name , view => view . Id ) ;
6870 await foreach ( EndpointSettings endpointSetting in endpointSettingsStore . GetAllEndpointSettings ( )
6971 . WithCancellation ( cancellationToken ) )
@@ -72,10 +74,13 @@ async Task PurgeMonitoringDataThatDoesNotNeedToBeTracked(CancellationToken cance
7274 {
7375 if ( monitorEndpointsLookup . Contains ( endpointSetting . Name ) )
7476 {
75- foreach ( Guid endpointId in monitorEndpointsLookup [ endpointSetting . Name ] )
77+ // We leave one dead instance behind, so that in ServicePulse we still display the endpoint as unhealthy, and is up to the user to manually delete it.
78+ // Otherwise, we would delete all dead instances and it could be that the endpoint should be alive but all instances are down and then we display nothing in ServicePulse which is no good!
79+ foreach ( Guid endpointId in monitorEndpointsLookup [ endpointSetting . Name ] . SkipLast ( 1 ) )
7680 {
7781 endpointInstanceMonitoring . RemoveEndpoint ( endpointId ) ;
7882 await monitoringDataStore . Delete ( endpointId ) ;
83+ logger . LogInformation ( $ "Removed endpoint '{ endpointSetting . Name } ' from monitoring data.") ;
7984 }
8085 }
8186 }
@@ -102,6 +107,8 @@ async Task InitialiseSettings(HashSet<string> monitorEndpoints, CancellationToke
102107 if ( ! monitorEndpoints . Contains ( endpointSetting . Name ) )
103108 {
104109 await endpointSettingsStore . Delete ( endpointSetting . Name , cancellationToken ) ;
110+ logger . LogInformation (
111+ $ "Removed EndpointTracking setting for '{ endpointSetting . Name } ' endpoint, since this endpoint is no longer monitored.") ;
105112 }
106113
107114 settingsNames . Add ( endpointSetting . Name ) ;
@@ -113,6 +120,8 @@ async Task InitialiseSettings(HashSet<string> monitorEndpoints, CancellationToke
113120 await endpointSettingsStore . UpdateEndpointSettings (
114121 new EndpointSettings { Name = string . Empty , TrackInstances = userSetTrackInstances } ,
115122 cancellationToken ) ;
123+ logger . LogInformation (
124+ $ "Initialized default value of EndpointTracking to { ( userSetTrackInstances ? "tracking" : "not tracking" ) } .") ;
116125 }
117126
118127 // Initialise settings for any missing endpoint
@@ -121,6 +130,8 @@ await endpointSettingsStore.UpdateEndpointSettings(
121130 await endpointSettingsStore . UpdateEndpointSettings (
122131 new EndpointSettings { Name = name , TrackInstances = userSetTrackInstances } ,
123132 cancellationToken ) ;
133+ logger . LogInformation (
134+ $ "Initialized '{ name } ' value of EndpointTracking to { ( userSetTrackInstances ? "tracking" : "not tracking" ) } .") ;
124135 }
125136 }
126137}
0 commit comments