From 518d205815ee4884d212cf637a975bda1cf89cfc Mon Sep 17 00:00:00 2001 From: Ting Date: Fri, 14 Apr 2017 17:15:31 -0400 Subject: [PATCH] print out disk usage in maintenance job. (#900) --- src/Agent.Worker/Agent.Worker.csproj | 1 + src/Agent.Worker/Build/TrackingManager.cs | 60 +++++++++++++++++------ src/Misc/layoutbin/en-US/strings.json | 2 +- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/Agent.Worker/Agent.Worker.csproj b/src/Agent.Worker/Agent.Worker.csproj index 9bac5b77b6..4216b3727f 100644 --- a/src/Agent.Worker/Agent.Worker.csproj +++ b/src/Agent.Worker/Agent.Worker.csproj @@ -16,6 +16,7 @@ + diff --git a/src/Agent.Worker/Build/TrackingManager.cs b/src/Agent.Worker/Build/TrackingManager.cs index 471c644981..11ce6c715c 100644 --- a/src/Agent.Worker/Build/TrackingManager.cs +++ b/src/Agent.Worker/Build/TrackingManager.cs @@ -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), @@ -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()); } } diff --git a/src/Misc/layoutbin/en-US/strings.json b/src/Misc/layoutbin/en-US/strings.json index 7b592b4a98..9565535221 100644 --- a/src/Misc/layoutbin/en-US/strings.json +++ b/src/Misc/layoutbin/en-US/strings.json @@ -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.",