Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Release] Fix flaky fetch step #313

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions Scalar.Common/Git/GitProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,21 @@ public Result BackgroundFetch(string remote)
userInteractive: false);
}

public string[] GetRemotes()
public bool TryGetRemotes(out string[] remotes, out string error)
{
return this.InvokeGitInWorkingDirectoryRoot("remote", fetchMissingObjects: false)
.Output
.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
GitProcess.Result result = this.InvokeGitInWorkingDirectoryRoot("remote", fetchMissingObjects: false);

if (result.ExitCodeIsFailure)
{
remotes = null;
error = result.Errors;
return false;
}

remotes = result.Output
.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
error = null;
return true;
}

public Result SparseCheckoutSet(List<string> foldersToSet)
Expand Down Expand Up @@ -754,6 +764,9 @@ protected virtual Result InvokeGitImpl(

this.executingProcess.Start();

this.executingProcess.BeginOutputReadLine();
this.executingProcess.BeginErrorReadLine();

try
{
if (this.LowerPriority)
Expand All @@ -771,9 +784,10 @@ protected virtual Result InvokeGitImpl(
{
// This is thrown if the process completes before we can set a property.
}

this.executingProcess.BeginOutputReadLine();
this.executingProcess.BeginErrorReadLine();
catch (Win32Exception)
{
// This is thrown if the process completes before we can set a property.
}

if (!this.executingProcess.WaitForExit(timeoutMs))
{
Expand Down Expand Up @@ -865,7 +879,8 @@ private Result InvokeGitInWorkingDirectoryRoot(
fetchMissingObjects: fetchMissingObjects,
writeStdIn: writeStdIn,
parseStdOutLine: parseStdOutLine,
timeoutMs: -1);
timeoutMs: -1,
userInteractive: userInteractive);
}

/// <summary>
Expand Down
15 changes: 14 additions & 1 deletion Scalar.Common/Maintenance/FetchStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,26 @@ private bool TryFetchUsingGitProtocol(GitProcess gitProcess, out string error)

using (ITracer activity = this.Context.Tracer.StartActivity(nameof(GitProcess.BackgroundFetch), EventLevel.LogAlways))
{
string[] remotes = gitProcess.GetRemotes();
if (!gitProcess.TryGetRemotes(out string[] remotes, out string errors))
{
error = $"Failed to load remotes with error: {errors}";
activity.RelatedError(error);
return false;
}

bool response = true;

error = "";
foreach (string remote in remotes)
{
activity.RelatedInfo($"Running fetch for remote '{remote}'");
GitProcess.Result result = gitProcess.BackgroundFetch(remote);

if (!string.IsNullOrWhiteSpace(result.Output))
{
activity.RelatedError($"Background fetch from '{remote}' completed with stdout: {result.Output}");
}

if (!string.IsNullOrWhiteSpace(result.Errors))
{
error += result.Errors;
Expand All @@ -176,6 +188,7 @@ private bool TryFetchUsingGitProtocol(GitProcess gitProcess, out string error)
{
response = false;
// Keep going through other remotes, but the overall result will still be false.
activity.RelatedError($"Background fetch from '{remote}' failed");
}
}

Expand Down