Skip to content

Commit

Permalink
Try moving the NuGet cache to ram
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Jan 17, 2025
1 parent 16d8388 commit b6f146a
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions Runner/JobBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public abstract class JobBase
private readonly Stopwatch _lastLogEntry = new();
private HardwareInfo? _hardwareInfo;
private volatile bool _completed;
private readonly List<(string, string)> _sharedEnvVars = [];

protected readonly HttpClient HttpClient;
protected readonly DateTime StartTime;
Expand Down Expand Up @@ -342,12 +343,11 @@ public async Task<int> RunProcessAsync(
WorkingDirectory = workDir ?? string.Empty,
};

if (envVars is not null)
envVars = [.. _sharedEnvVars, .. envVars ?? []];

foreach ((string key, string value) in envVars)
{
foreach ((string key, string value) in envVars)
{
startInfo.EnvironmentVariables.Add(key, value);
}
startInfo.EnvironmentVariables.Add(key, value);
}

using var process = new Process
Expand Down Expand Up @@ -498,6 +498,17 @@ protected int GetTotalSystemMemoryGB()
}

protected async Task ChangeWorkingDirectoryToRamOrFastestDiskAsync()
{
string? newLocation = await ChangeWorkingDirectoryToRamOrFastestDiskAsyncCore();
if (newLocation is not null)
{
string cachePath = Path.Combine(newLocation, "NuGetPackagesCache");
Directory.CreateDirectory(cachePath);
_sharedEnvVars.Add(("NUGET_PACKAGES", cachePath));
}
}

private async Task<string?> ChangeWorkingDirectoryToRamOrFastestDiskAsyncCore()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand All @@ -508,7 +519,7 @@ protected async Task ChangeWorkingDirectoryToRamOrFastestDiskAsync()
.ToArray();

await LogAsync($"Drives available: {string.Join(", ", drives.Select(
d => $"Ready={d.IsReady} AvailableGB={d.AvailableFreeSpace >> 30} Path={d.RootDirectory.FullName}"))}");
d => $"AvailableGB={d.AvailableFreeSpace >> 30} Path={d.RootDirectory.FullName}"))}");

if (drives.Length > 1)
{
Expand All @@ -520,13 +531,16 @@ await LogAsync($"Drives available: {string.Join(", ", drives.Select(
Environment.CurrentDirectory = newWorkDir;

await LogAsync($"Changed working directory from {OriginalWorkingDirectory} to {newWorkDir}");

return newWorkDir;
}
}
catch (Exception ex)
{
await LogAsync($"Failed to apply new working directory: {ex}");
}
return;

return null;
}

Stopwatch s = Stopwatch.StartNew();
Expand Down Expand Up @@ -558,12 +572,16 @@ await LogAsync($"Drives available: {string.Join(", ", drives.Select(
Environment.CurrentDirectory = NewWorkDir;

await LogAsync($"Changed working directory from {OriginalWorkingDirectory} to {NewWorkDir}");

return NewWorkDir;
}
catch (Exception ex)
{
await LogAsync($"Failed to apply new working directory: {ex}");
}
}

return null;
}

private async Task StreamSystemHardwareInfoAsync()
Expand Down

0 comments on commit b6f146a

Please sign in to comment.