-
Notifications
You must be signed in to change notification settings - Fork 979
/
TempDirectoryManager.cs
62 lines (57 loc) · 2.05 KB
/
TempDirectoryManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using GitHub.Runner.Common.Util;
using System;
using System.IO;
using System.Threading;
using GitHub.Runner.Common;
using GitHub.Runner.Sdk;
namespace GitHub.Runner.Worker
{
[ServiceLocator(Default = typeof(TempDirectoryManager))]
public interface ITempDirectoryManager : IRunnerService
{
void InitializeTempDirectory(IExecutionContext jobContext);
void CleanupTempDirectory();
}
public sealed class TempDirectoryManager : RunnerService, ITempDirectoryManager
{
private string _tempDirectory;
public override void Initialize(IHostContext hostContext)
{
base.Initialize(hostContext);
_tempDirectory = HostContext.GetDirectory(WellKnownDirectory.Temp);
}
public void InitializeTempDirectory(IExecutionContext jobContext)
{
ArgUtil.NotNull(jobContext, nameof(jobContext));
ArgUtil.NotNullOrEmpty(_tempDirectory, nameof(_tempDirectory));
jobContext.SetRunnerContext("temp", _tempDirectory);
jobContext.Debug($"Cleaning runner temp folder: {_tempDirectory}");
try
{
IOUtil.DeleteDirectory(_tempDirectory, contentsOnly: true, continueOnContentDeleteError: true, cancellationToken: jobContext.CancellationToken);
}
catch (Exception ex)
{
Trace.Error(ex);
}
finally
{
// make sure folder exists
Directory.CreateDirectory(_tempDirectory);
}
}
public void CleanupTempDirectory()
{
ArgUtil.NotNullOrEmpty(_tempDirectory, nameof(_tempDirectory));
Trace.Info($"Cleaning runner temp folder: {_tempDirectory}");
try
{
IOUtil.DeleteDirectory(_tempDirectory, contentsOnly: true, continueOnContentDeleteError: true, cancellationToken: CancellationToken.None);
}
catch (Exception ex)
{
Trace.Error(ex);
}
}
}
}