diff --git a/Runner/JobBase.cs b/Runner/JobBase.cs index dc026f6..4b0baec 100644 --- a/Runner/JobBase.cs +++ b/Runner/JobBase.cs @@ -474,6 +474,24 @@ private async Task PostAsJsonAsync(string path, object? value, string? queryArgs } } + protected static string GetRoughSizeString(long size) + { + double kb = size / 1024d; + double mb = kb / 1024d; + + if (mb >= 1) + { + return $"{(int)mb} MB"; + } + + if (kb >= 1) + { + return $"{(int)kb} KB"; + } + + return $"{size} B"; + } + protected int GetTotalSystemMemoryGB() { var memory = _hardwareInfo?.MemoryStatus; diff --git a/Runner/Jobs/JitDiffJob.cs b/Runner/Jobs/JitDiffJob.cs index dc151c0..bb2a71f 100644 --- a/Runner/Jobs/JitDiffJob.cs +++ b/Runner/Jobs/JitDiffJob.cs @@ -1,4 +1,7 @@ -namespace Runner.Jobs; +using Azure.Storage.Blobs.Models; +using System.IO.Compression; + +namespace Runner.Jobs; internal sealed class JitDiffJob : JobBase { @@ -15,8 +18,12 @@ protected override async Task RunJobCoreAsync() await CloneRuntimeAndSetupToolsAsync(this); + await TryDownloadPreviousBuildArtifactsAsync(); + await BuildAndCopyRuntimeBranchBitsAsync(this, "main"); + await UploadBuildArtifactsAsync(); + await RunProcessAsync("git", "switch pr", workDir: "runtime"); await BuildAndCopyRuntimeBranchBitsAsync(this, "pr"); @@ -96,6 +103,45 @@ public static async Task BuildAndCopyRuntimeBranchBitsAsync(JobBase job, string await copyReleaseBitsTask; } + private async Task TryDownloadPreviousBuildArtifactsAsync() + { + try + { + var artifactsBlob = PersistentStateClient.GetBlobClient("runtime-artifacts-main.zip"); + if (await artifactsBlob.ExistsAsync(JobTimeout)) + { + await LogAsync($"Downloading previous artifacts ..."); + var content = (await artifactsBlob.DownloadContentAsync(JobTimeout)).Value; + + await LogAsync($"Extracting previous artifacts ({GetRoughSizeString(content.Details.ContentLength)}) ..."); + ZipFile.ExtractToDirectory(content.Content.ToStream(), "runtime/artifacts"); + } + } + catch (Exception ex) + { + await LogAsync($"Failed to download previous artifacts: {ex}"); + } + } + + private async Task UploadBuildArtifactsAsync() + { + try + { + await LogAsync($"Compressing artifacts directory ..."); + using var ms = new MemoryStream(512 * 1024 * 1024); + ZipFile.CreateFromDirectory("runtime/artifacts", ms, CompressionLevel.Optimal, includeBaseDirectory: false); + ms.Position = 0; + + await LogAsync($"Uploading artifacts directory ({GetRoughSizeString(ms.Length)}) ..."); + var artifactsBlob = PersistentStateClient.GetBlobClient("runtime-artifacts-main.zip"); + await artifactsBlob.UploadAsync(ms, overwrite: true, JobTimeout); + } + catch (Exception ex) + { + await LogAsync($"Failed to upload artifacts: {ex}"); + } + } + private async Task CollectFrameworksDiffsAsync() { try