Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit fdb8d16

Browse files
authored
getting remote branches often fails (#33)
* getting remote branches often fails * fake commit
1 parent 92d2937 commit fdb8d16

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

Common/GitRepository.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
@@ -316,7 +316,7 @@ public IList<Branch> GetRemoteBranches()
316316
{
317317
log.Info($"{"[" + ModuleName + "]",-30}Get remote branches");
318318
var sw = Stopwatch.StartNew();
319-
var exitCode = runner.RunInDirectory(RepoPath, "git ls-remote --heads", TimoutHelper.GetStartTimeout());
319+
var exitCode = runner.RunInDirectory(RepoPath, "git ls-remote --heads", TimoutHelper.GetStartTimeout(), RetryStrategy.IfTimeoutOrFailed);
320320

321321
sw.Stop();
322322
if (sw.Elapsed > TimeSpan.FromSeconds(10))

Common/ShellRunner.cs

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
@@ -75,8 +75,7 @@ private string ReadStream(StreamReader output, ReadLineEvent evt)
7575
while (!output.EndOfStream)
7676
{
7777
var line = output.ReadLine();
78-
if (evt != null)
79-
evt(line);
78+
evt?.Invoke(line);
8079
result.Append(line + "\n");
8180
}
8281
return result.ToString();
@@ -92,19 +91,29 @@ private void ReadCmdError(Process pr)
9291
Errors = ReadStream(pr.StandardError, OnErrorsChange);
9392
}
9493

95-
private int RunThreeTimes(string commandWithArguments, string workingDirectory, TimeSpan timeout)
94+
private int RunThreeTimes(string commandWithArguments, string workingDirectory, TimeSpan timeout, RetryStrategy retryStrategy = RetryStrategy.IfTimeout)
9695
{
9796
int result = RunOnce(commandWithArguments, workingDirectory, timeout);
98-
if (!HasTimeout)
99-
return result;
100-
101-
timeout = TimoutHelper.IncreaceTimeout(timeout);
10297
int times = 2;
103-
while (times-- > 0 && HasTimeout)
98+
99+
while (times-- > 0 && NeedRunAgain(retryStrategy))
100+
{
101+
if (HasTimeout)
102+
timeout = TimoutHelper.IncreaceTimeout(timeout);
104103
result = RunOnce(commandWithArguments, workingDirectory, timeout);
104+
}
105105
return result;
106106
}
107107

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+
108117
public int RunOnce(string commandWithArguments, string workingDirectory, TimeSpan timeout)
109118
{
110119
BeforeRun();
@@ -208,31 +217,38 @@ public int RunInDirectory(string path, string commandWithArguments)
208217
return RunInDirectory(path, commandWithArguments, DefaultTimeout);
209218
}
210219

211-
public int RunInDirectory(string path, string commandWithArguments, TimeSpan timeout)
220+
public int RunInDirectory(string path, string commandWithArguments, TimeSpan timeout, RetryStrategy retryStrategy = RetryStrategy.IfTimeout)
212221
{
213-
return RunThreeTimes(commandWithArguments, path, timeout);
222+
return RunThreeTimes(commandWithArguments, path, timeout, retryStrategy);
214223
}
215224
}
216225

226+
public enum RetryStrategy
227+
{
228+
None,
229+
IfTimeout,
230+
IfTimeoutOrFailed
231+
}
232+
217233
public static class TimoutHelper
218234
{
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);
221237
private const int TimesForUseBigDefault = 1;
222238

223239
private static int badTimes;
224240

225241
public static TimeSpan IncreaceTimeout(TimeSpan was)
226242
{
227243
badTimes++;
228-
return was < bigTimeout ? bigTimeout : was;
244+
return was < BigTimeout ? BigTimeout : was;
229245
}
230246

231247
public static TimeSpan GetStartTimeout()
232248
{
233249
if (badTimes > TimesForUseBigDefault)
234-
return bigTimeout;
235-
return smallTimeout;
250+
return BigTimeout;
251+
return SmallTimeout;
236252
}
237253
}
238254
}

0 commit comments

Comments
 (0)