1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
4
using System . IO ;
@@ -75,8 +75,7 @@ private string ReadStream(StreamReader output, ReadLineEvent evt)
75
75
while ( ! output . EndOfStream )
76
76
{
77
77
var line = output . ReadLine ( ) ;
78
- if ( evt != null )
79
- evt ( line ) ;
78
+ evt ? . Invoke ( line ) ;
80
79
result . Append ( line + "\n " ) ;
81
80
}
82
81
return result . ToString ( ) ;
@@ -92,19 +91,29 @@ private void ReadCmdError(Process pr)
92
91
Errors = ReadStream ( pr . StandardError , OnErrorsChange ) ;
93
92
}
94
93
95
- private int RunThreeTimes ( string commandWithArguments , string workingDirectory , TimeSpan timeout )
94
+ private int RunThreeTimes ( string commandWithArguments , string workingDirectory , TimeSpan timeout , RetryStrategy retryStrategy = RetryStrategy . IfTimeout )
96
95
{
97
96
int result = RunOnce ( commandWithArguments , workingDirectory , timeout ) ;
98
- if ( ! HasTimeout )
99
- return result ;
100
-
101
- timeout = TimoutHelper . IncreaceTimeout ( timeout ) ;
102
97
int times = 2 ;
103
- while ( times -- > 0 && HasTimeout )
98
+
99
+ while ( times -- > 0 && NeedRunAgain ( retryStrategy ) )
100
+ {
101
+ if ( HasTimeout )
102
+ timeout = TimoutHelper . IncreaceTimeout ( timeout ) ;
104
103
result = RunOnce ( commandWithArguments , workingDirectory , timeout ) ;
104
+ }
105
105
return result ;
106
106
}
107
107
108
+ private bool NeedRunAgain ( RetryStrategy retryStrategy )
109
+ {
110
+ if ( retryStrategy == RetryStrategy . IfTimeout )
111
+ return HasTimeout ;
112
+ if ( retryStrategy == RetryStrategy . IfTimeoutOrFailed )
113
+ return true ;
114
+ return false ;
115
+ }
116
+
108
117
public int RunOnce ( string commandWithArguments , string workingDirectory , TimeSpan timeout )
109
118
{
110
119
BeforeRun ( ) ;
@@ -208,31 +217,38 @@ public int RunInDirectory(string path, string commandWithArguments)
208
217
return RunInDirectory ( path , commandWithArguments , DefaultTimeout ) ;
209
218
}
210
219
211
- public int RunInDirectory ( string path , string commandWithArguments , TimeSpan timeout )
220
+ public int RunInDirectory ( string path , string commandWithArguments , TimeSpan timeout , RetryStrategy retryStrategy = RetryStrategy . IfTimeout )
212
221
{
213
- return RunThreeTimes ( commandWithArguments , path , timeout ) ;
222
+ return RunThreeTimes ( commandWithArguments , path , timeout , retryStrategy ) ;
214
223
}
215
224
}
216
225
226
+ public enum RetryStrategy
227
+ {
228
+ None ,
229
+ IfTimeout ,
230
+ IfTimeoutOrFailed
231
+ }
232
+
217
233
public static class TimoutHelper
218
234
{
219
- private static readonly TimeSpan smallTimeout = TimeSpan . FromSeconds ( 30 ) ;
220
- private static readonly TimeSpan bigTimeout = TimeSpan . FromMinutes ( 10 ) ;
235
+ private static readonly TimeSpan SmallTimeout = TimeSpan . FromSeconds ( 30 ) ;
236
+ private static readonly TimeSpan BigTimeout = TimeSpan . FromMinutes ( 10 ) ;
221
237
private const int TimesForUseBigDefault = 1 ;
222
238
223
239
private static int badTimes ;
224
240
225
241
public static TimeSpan IncreaceTimeout ( TimeSpan was )
226
242
{
227
243
badTimes ++ ;
228
- return was < bigTimeout ? bigTimeout : was ;
244
+ return was < BigTimeout ? BigTimeout : was ;
229
245
}
230
246
231
247
public static TimeSpan GetStartTimeout ( )
232
248
{
233
249
if ( badTimes > TimesForUseBigDefault )
234
- return bigTimeout ;
235
- return smallTimeout ;
250
+ return BigTimeout ;
251
+ return SmallTimeout ;
236
252
}
237
253
}
238
254
}
0 commit comments