Skip to content

Commit

Permalink
Merge pull request #82 from guitarrapc/feature/logfile_wait
Browse files Browse the repository at this point in the history
feat: first time unity launch hits log not found exception.
  • Loading branch information
guitarrapc authored Sep 26, 2023
2 parents 0949847 + cafa1c1 commit 749e5f7
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/UnityBuildRunner.Core/DefaultBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,39 @@ public async Task BuildAsync(CancellationToken ct = default)
}

var unityProcessExitCode = 0;
var logFileFound = false;
try
{
// wait for log file generated.
var waitingLongTime = false;
while (!process.HasExited)
{
logFileFound = File.Exists(settings.LogFilePath);
if (logFileFound) break;
ct.ThrowIfCancellationRequested();

// retry for 10 seconds.
if (sw.Elapsed.TotalSeconds < 10)
if (File.Exists(settings.LogFilePath)) break;

// Log waiting message.
if (sw.Elapsed.TotalSeconds > 10 && !waitingLongTime)
{
await Task.Delay(TimeSpan.FromMilliseconds(10), ct).ConfigureAwait(false);
waitingLongTime = true;
logger.LogWarning("Waiting Unity creates log file takes long time, still waiting.");
}

// Some large repository's first Unity launch takes huge wait time until log file generated. However waiting more than 5min would be too slow and unnatural.
if (sw.Elapsed.TotalMinutes <= 5)
{
await Task.Delay(TimeSpan.FromMilliseconds(100), ct).ConfigureAwait(false);
}
else
{
throw new BuildLogNotFoundException($"Unity Process not created logfile.", settings.LogFilePath, Path.Combine(settings.WorkingDirectory, settings.LogFilePath));
// No log file means build not started.
throw new BuildLogNotFoundException($"Unity Process not created logfile. Hint: This might be log file permission issue or temporary failure. Re-run build and see reproduce or not.", settings.LogFilePath, Path.Combine(settings.WorkingDirectory, settings.LogFilePath));
}
}

// log file generated but process immediately exited.
// log file generated but process immediately exited. This is unexpected but unity may have some trouble with.
if (process.HasExited)
{
throw new OperationCanceledException($"Unity process started but build unexpectedly finished.");
throw new OperationCanceledException($"Unity process started but unexpectedly finished before build.");
}

using (var file = File.Open(settings.LogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Expand All @@ -107,6 +116,8 @@ public async Task BuildAsync(CancellationToken ct = default)
// read logs and redirect to stdout
while (!process.HasExited)
{
ct.ThrowIfCancellationRequested();

ReadAndFilterLog(reader, errorFilter);
await Task.Delay(TimeSpan.FromMilliseconds(500), ct).ConfigureAwait(false);
}
Expand Down

0 comments on commit 749e5f7

Please sign in to comment.