@@ -23,6 +23,8 @@ public class PeriodicPoller
2323
2424 private bool initialised = false ;
2525
26+ private int errorCount = 0 ;
27+
2628 private Timer Timer
2729 {
2830 get ; set ;
@@ -38,6 +40,11 @@ private IDataSource DataSource
3840 get ; set ;
3941 }
4042
43+ private int MaxErrors
44+ {
45+ get ; set ;
46+ }
47+
4148 private static CancellationTokenSource CancellationTokenSource ;
4249
4350 public PeriodicPoller ( IConfiguration configuration , IDataSource dataProducer , IDataTarget dataConsumer , ILogger < PeriodicPoller > logger )
@@ -46,6 +53,21 @@ public PeriodicPoller(IConfiguration configuration, IDataSource dataProducer, ID
4653 this . DataSource = dataProducer ;
4754 this . DataTarget = dataConsumer ;
4855
56+ try
57+ {
58+ this . MaxErrors = configuration . GetValue < int > ( Constants . MAX_ERRORS ) ;
59+ if ( this . MaxErrors == 0 )
60+ {
61+ logger . LogError ( "Failed to read the MaxErrors configuration for how many times to have an error before forcing a restart. Using a default of 5." ) ;
62+ this . MaxErrors = 2 ;
63+ }
64+ }
65+ catch ( Exception ex )
66+ {
67+ logger . LogError ( ex , "Failed to read the MaxErrors configuration for how many times to have an error before forcing a restart. Using a default of 5." ) ;
68+ this . MaxErrors = 2 ;
69+ }
70+
4971 this . logger = logger ;
5072 }
5173
@@ -137,6 +159,7 @@ private void PollGenerationStatistics_Elapsed(object sender, ElapsedEventArgs e)
137159 }
138160 else
139161 {
162+ errorCount ++ ;
140163 bool sentErrorDataSuccess = DataTarget . SendErrorData ( pushData ) . GetAwaiter ( ) . GetResult ( ) ;
141164 if ( sentErrorDataSuccess )
142165 {
@@ -146,6 +169,12 @@ private void PollGenerationStatistics_Elapsed(object sender, ElapsedEventArgs e)
146169 {
147170 logger . LogError ( "Failed to send the error data to {0}." , DataTarget . Name ) ;
148171 }
172+ if ( errorCount >= MaxErrors )
173+ {
174+ logger . LogWarning ( "The maximum number of errors has occurred and the system is being reset." ) ;
175+ DataSource . Restart ( CancellationTokenSource ) ;
176+ errorCount = 0 ;
177+ }
149178 }
150179 }
151180 }
0 commit comments