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

Magic cache integration #3

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/Runner.Common/HostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ public enum ShutdownReason
{
UserCancelled = 0,
OperatingSystemShutdown = 1,
InfrastructureInterrupted = 2,
// RunsOn: Spot interruption
InfrastructureInterrupted = 11,
}
}
3 changes: 3 additions & 0 deletions src/Runner.Worker/Handlers/ContainerActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ public async Task RunAsync(ActionRunStage stage)
Environment["ACTIONS_RESULTS_URL"] = resultsUrl;
}

// RunsOn: Magic Cache
await ConfigureMagicCache();

foreach (var variable in this.Environment)
{
container.ContainerEnvironmentVariables[variable.Key] = container.TranslateToContainerPath(variable.Value);
Expand Down
49 changes: 49 additions & 0 deletions src/Runner.Worker/Handlers/Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using GitHub.Runner.Common.Util;
using GitHub.Runner.Sdk;
using Pipelines = GitHub.DistributedTask.Pipelines;
using System.Net.Http;

namespace GitHub.Runner.Worker.Handlers
{
Expand All @@ -33,6 +34,11 @@ public abstract class Handler : RunnerService
private const int _environmentVariableMaximumSize = 32766;
#endif

private static readonly HttpClient _httpClient = new()
{
Timeout = TimeSpan.FromSeconds(5)
};

protected IActionCommandManager ActionCommandManager { get; private set; }

public Pipelines.ActionStepDefinitionReference Action { get; set; }
Expand Down Expand Up @@ -231,5 +237,48 @@ protected void AddPrependPathToEnvironment()
AddEnvironmentVariable(Constants.PathVariable, newPath);
}
}

// RunsOn: Magic Cache
protected async Task ConfigureMagicCache()
{
var magicCacheUrl = System.Environment.GetEnvironmentVariable("RUNS_ON_MAGIC_CACHE_URL");
if (!string.IsNullOrEmpty(magicCacheUrl))
{
var configParams = new List<KeyValuePair<string, string>>();
if (Environment.ContainsKey("ACTIONS_CACHE_SERVICE_V2"))
{
Environment["ACTIONS_CACHE_SERVICE_V2_ORIGINAL"] = Environment["ACTIONS_CACHE_SERVICE_V2"];
configParams.Add(new KeyValuePair<string, string>("ACTIONS_CACHE_SERVICE_V2", Environment["ACTIONS_CACHE_SERVICE_V2_ORIGINAL"]));
}
if (Environment.ContainsKey("ACTIONS_CACHE_URL"))
{
Environment["ACTIONS_CACHE_URL_ORIGINAL"] = Environment["ACTIONS_CACHE_URL"];
configParams.Add(new KeyValuePair<string, string>("ACTIONS_CACHE_URL", Environment["ACTIONS_CACHE_URL_ORIGINAL"]));
}
if (Environment.ContainsKey("ACTIONS_RESULTS_URL"))
{
Environment["ACTIONS_RESULTS_URL_ORIGINAL"] = Environment["ACTIONS_RESULTS_URL"];
configParams.Add(new KeyValuePair<string, string>("ACTIONS_RESULTS_URL", Environment["ACTIONS_RESULTS_URL_ORIGINAL"]));
}

// always use v2 with Magic Cache
Environment["ACTIONS_CACHE_SERVICE_V2"] = bool.TrueString;
Environment["ACTIONS_CACHE_URL"] = magicCacheUrl;
Environment["ACTIONS_RESULTS_URL"] = magicCacheUrl;

// Configure magic cache with original results URL
try
{
using var content = new FormUrlEncodedContent(configParams);
using var response = await _httpClient.PutAsync($"{magicCacheUrl}config", content);
response.EnsureSuccessStatusCode();
ExecutionContext.Debug($"Magic cache config update status: {response.StatusCode}");
}
catch (Exception ex)
{
ExecutionContext.Warning($"Failed to configure magic cache: {ex.Message}");
}
}
}
}
}
9 changes: 9 additions & 0 deletions src/Runner.Worker/Handlers/NodeScriptActionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using GitHub.Runner.Sdk;
using GitHub.Runner.Worker.Container;
using GitHub.Runner.Worker.Container.ContainerHooks;
using System.Net.Http;

namespace GitHub.Runner.Worker.Handlers
{
Expand All @@ -25,6 +26,11 @@ public sealed class NodeScriptActionHandler : Handler, INodeScriptActionHandler
{
public NodeJSActionExecutionData Data { get; set; }

private static readonly HttpClient _httpClient = new()
{
Timeout = TimeSpan.FromSeconds(5)
};

public async Task RunAsync(ActionRunStage stage)
{
// Validate args.
Expand Down Expand Up @@ -77,6 +83,9 @@ public async Task RunAsync(ActionRunStage stage)
Environment["ACTIONS_CACHE_SERVICE_V2"] = bool.TrueString;
}

// RunsOn: Magic Cache
await ConfigureMagicCache();

// Resolve the target script.
string target = null;
if (stage == ActionRunStage.Main)
Expand Down
7 changes: 5 additions & 2 deletions src/Runner.Worker/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public sealed class JobRunner : RunnerService, IJobRunner
private RunnerSettings _runnerSettings;
private ITempDirectoryManager _tempDirectoryManager;
private readonly CancellationTokenSource _interruptedHookTokenSource = new();
// RunsOn: Spot interruption
private Task _interruptedHookTask;

public async Task<TaskResult> RunAsync(AgentJobRequestMessage message, CancellationToken jobRequestCancellationToken)
Expand Down Expand Up @@ -133,6 +134,7 @@ public async Task<TaskResult> RunAsync(AgentJobRequestMessage message, Cancellat
case ShutdownReason.OperatingSystemShutdown:
errorMessage = $"Operating system is shutting down for computer '{Environment.MachineName}'";
break;
// RunsOn: Spot interruption
case ShutdownReason.InfrastructureInterrupted:
errorMessage = "This job was interrupted by the runner infrastructure. Results may be incomplete.";
break;
Expand Down Expand Up @@ -221,10 +223,10 @@ public async Task<TaskResult> RunAsync(AgentJobRequestMessage message, Cancellat
await Task.WhenAny(_jobServerQueue.JobRecordUpdated.Task, Task.Delay(1000));
}

// Start monitoring for interrupted hook file
// RunsOn: Spot interruption
_interruptedHookTask = Task.Run(async () =>
{
string interruptedHookPath = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Root), "runs-on-interrupted");
string interruptedHookPath = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Root), "_runs-on", "interrupted");
while (!_interruptedHookTokenSource.Token.IsCancellationRequested)
{
try
Expand Down Expand Up @@ -288,6 +290,7 @@ public async Task<TaskResult> RunAsync(AgentJobRequestMessage message, Cancellat
runnerShutdownRegistration = null;
}

// RunsOn: Spot interruption
if (_interruptedHookTask != null)
{
_interruptedHookTokenSource.Cancel();
Expand Down
2 changes: 2 additions & 0 deletions src/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ function package ()

rm -Rf "${LAYOUT_DIR:?}/_diag"
find "${LAYOUT_DIR}/bin" -type f -name '*.pdb' -delete
# hint that this is a custom agent built for https://runs-on.com
mkdir -p "${LAYOUT_DIR}/_runs-on"

mkdir -p "$PACKAGE_DIR"
rm -Rf "${PACKAGE_DIR:?}"/*
Expand Down