Skip to content

Commit

Permalink
add warning when running out of disk.
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang committed Jan 5, 2021
1 parent fc3ca9b commit a550df4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Runner.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,14 @@ public static class ReturnCode
public const int RunOnceRunnerUpdating = 4;
}

public static class Features
{
public static readonly string DiskSpaceWarning = "runner.diskspace.warning";
}

public static readonly string InternalTelemetryIssueDataKey = "_internal_telemetry";
public static readonly string WorkerCrash = "WORKER_CRASH";
public static readonly string LowDiskSpace = "LOW_DISK_SPACE";
public static readonly string UnsupportedCommand = "UNSUPPORTED_COMMAND";
public static readonly string UnsupportedCommandMessageDisabled = "The `{0}` command is disabled. Please upgrade to using Environment Files or opt into unsecure command execution by setting the `ACTIONS_ALLOW_UNSECURE_COMMANDS` environment variable to `true`. For more information see: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/";
}
Expand Down
51 changes: 49 additions & 2 deletions src/Runner.Worker/JobExtension.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using GitHub.DistributedTask.Expressions2;
using GitHub.DistributedTask.ObjectTemplating.Tokens;
Expand Down Expand Up @@ -41,6 +42,8 @@ public sealed class JobExtension : RunnerService, IJobExtension
private readonly HashSet<string> _existingProcesses = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
private bool _processCleanup;
private string _processLookupId = $"github_{Guid.NewGuid()}";
private CancellationTokenSource _diskSpaceCheckToken = new CancellationTokenSource();
private Task _diskSpaceCheckTask = null;

// Download all required actions.
// Make sure all condition inputs are valid.
Expand Down Expand Up @@ -325,6 +328,12 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
}
}

jobContext.Global.EnvironmentVariables.TryGetValue(Constants.Runner.Features.DiskSpaceWarning, out var enableWarning);
if (StringUtil.ConvertToBoolean(enableWarning, defaultValue: true))
{
_diskSpaceCheckTask = CheckDiskSpaceAsync(context, _diskSpaceCheckToken.Token);
}

return steps;
}
catch (OperationCanceledException ex) when (jobContext.CancellationToken.IsCancellationRequested)
Expand All @@ -335,7 +344,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel
context.Result = TaskResult.Canceled;
throw;
}
catch (FailedToResolveActionDownloadInfoException ex)
catch (FailedToResolveActionDownloadInfoException ex)
{
// Log the error and fail the JobExtension Initialization.
Trace.Error($"Caught exception from JobExtenion Initialization: {ex}");
Expand Down Expand Up @@ -529,6 +538,11 @@ public void FinalizeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestM
}
}
}

if (_diskSpaceCheckTask != null)
{
_diskSpaceCheckToken.Cancel();
}
}
catch (Exception ex)
{
Expand All @@ -544,6 +558,39 @@ public void FinalizeJob(IExecutionContext jobContext, Pipelines.AgentJobRequestM
}
}

private async Task CheckDiskSpaceAsync(IExecutionContext context, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
// Add warning when disk is lower than 100 MB or system.runner.lowdiskspacethreshold from service
var lowDiskSpaceThreshold = context.Global.Variables.GetInt(WellKnownDistributedTaskVariables.RunnerLowDiskspaceThreshold);
if (lowDiskSpaceThreshold == null)
{
Trace.Info($"Low diskspace warning is not enabled.");
return;
}
var workDirRoot = Directory.GetDirectoryRoot(HostContext.GetDirectory(WellKnownDirectory.Work));
var driveInfo = new DriveInfo(workDirRoot);
var freeSpaceInMB = driveInfo.AvailableFreeSpace / 1024 / 1024;
if (freeSpaceInMB < lowDiskSpaceThreshold)
{
var issue = new Issue() { Type = IssueType.Warning, Message = $"You are running out of disk space. The runner will stop working when the machine runs out of disk space. Free space left: {freeSpaceInMB} MB" };
issue.Data[Constants.Runner.InternalTelemetryIssueDataKey] = Constants.Runner.LowDiskSpace;
context.AddIssue(issue);
return;
}

try
{
await Task.Delay(10 * 1000, token);
}
catch (TaskCanceledException)
{
// ignore
}
}
}

private Dictionary<int, Process> SnapshotProcesses()
{
Dictionary<int, Process> snapshot = new Dictionary<int, Process>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace GitHub.DistributedTask.WebApi
public static class WellKnownDistributedTaskVariables
{
public static readonly String JobId = "system.jobId";
public static readonly String RunnerLowDiskspaceThreshold = "system.runner.lowdiskspacethreshold";
}
}

0 comments on commit a550df4

Please sign in to comment.