Skip to content

Commit

Permalink
print out disk usage in maintenance job. (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang authored Apr 14, 2017
1 parent d85cfab commit 518d205
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Agent.Worker/Agent.Worker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
<PackageReference Include="System.Diagnostics.TraceSource" Version="4.3.0" />
<PackageReference Include="System.Linq" Version="4.3.0" />
<PackageReference Include="System.IO.FileSystem.DriveInfo" Version="4.3.0" />
<PackageReference Include="System.IO.Compression.ZipFile" Version="4.3.0" />
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.0" />
<PackageReference Include="System.Security.Cryptography.Primitives" Version="4.3.0" />
Expand Down
60 changes: 46 additions & 14 deletions src/Agent.Worker/Build/TrackingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public void MarkExpiredForGarbageCollection(IExecutionContext executionContext,
public void DisposeCollectedGarbage(IExecutionContext executionContext)
{
Trace.Entering();
PrintOutDiskUsage(executionContext);

string gcDirectory = Path.Combine(
HostContext.GetDirectory(WellKnownDirectory.Work),
Expand All @@ -252,25 +253,56 @@ public void DisposeCollectedGarbage(IExecutionContext executionContext)
}

Trace.Info($"Find {gcTrackingFiles.Count()} GC tracking files.");
foreach (string gcFile in gcTrackingFiles)

if (gcTrackingFiles.Count() > 0)
{
try
foreach (string gcFile in gcTrackingFiles)
{
var gcConfig = LoadIfExists(executionContext, gcFile) as TrackingConfig;
ArgUtil.NotNull(gcConfig, nameof(TrackingConfig));
try
{
var gcConfig = LoadIfExists(executionContext, gcFile) as TrackingConfig;
ArgUtil.NotNull(gcConfig, nameof(TrackingConfig));

string fullPath = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), gcConfig.BuildDirectory);
executionContext.Output(StringUtil.Loc("Deleting", fullPath));
IOUtil.DeleteDirectory(fullPath, CancellationToken.None);
string fullPath = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), gcConfig.BuildDirectory);
executionContext.Output(StringUtil.Loc("Deleting", fullPath));
IOUtil.DeleteDirectory(fullPath, executionContext.CancellationToken);

executionContext.Output(StringUtil.Loc("DeleteGCTrackingFile", fullPath));
IOUtil.DeleteFile(gcFile);
}
catch (Exception ex)
{
executionContext.Error(StringUtil.Loc("ErrorDuringBuildGCDelete", gcFile));
executionContext.Error(ex);
executionContext.Output(StringUtil.Loc("DeleteGCTrackingFile", fullPath));
IOUtil.DeleteFile(gcFile);
}
catch (Exception ex)
{
executionContext.Error(StringUtil.Loc("ErrorDuringBuildGCDelete", gcFile));
executionContext.Error(ex);
}
}

PrintOutDiskUsage(executionContext);
}
}

private void PrintOutDiskUsage(IExecutionContext context)
{
// Print disk usage should be best effort, since DriveInfo can't detect usage of UNC share.
try
{
context.Output($"Disk usage for working directory: {HostContext.GetDirectory(WellKnownDirectory.Work)}");
var workDirectoryDrive = new DriveInfo(HostContext.GetDirectory(WellKnownDirectory.Work));
long freeSpace = workDirectoryDrive.AvailableFreeSpace;
long totalSpace = workDirectoryDrive.TotalSize;
#if OS_WINDOWS
context.Output($"Working directory belongs to drive: '{workDirectoryDrive.Name}'");
#else
context.Output($"Information about file system on which working directory resides.");
#endif
context.Output($"Total size: '{totalSpace / 1024.0 / 1024.0} MB'");
context.Output($"Available space: '{freeSpace / 1024.0 / 1024.0} MB'");
}
catch (Exception ex)
{
context.Warning($"Unable inspect disk usage for working directory {HostContext.GetDirectory(WellKnownDirectory.Work)}.");
Trace.Error(ex);
context.Debug(ex.ToString());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Misc/layoutbin/en-US/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"FileUploadRetryInSecond": "Retry file upload after {0} seconds.",
"FileUploadRetrySucceed": "File upload succeed after retry.",
"FileUploadSucceed": "File upload succeed.",
"FinishMaintenance": "Maintenance finsihed: {0}",
"FinishMaintenance": "Maintenance finished: {0}",
"GCBuildDir": "Delete orphan and stale build directory.",
"GCBuildDirNotEnabled": "Delete orphan and stale build directory option is not enabled.",
"GCDirIsEmpty": "No build directory need to be GC. '{0}' doesn't have any tracking file.",
Expand Down

0 comments on commit 518d205

Please sign in to comment.