public void MarkExpiredForGarbageCollection(IExecutionContext executionContext, TimeSpan expiration)
        {
            Trace.Entering();
            Trace.Info("Scan all SourceFolder tracking files.");
            string searchRoot = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), Constants.Release.Path.RootMappingDirectory);

            if (!Directory.Exists(searchRoot))
            {
                executionContext.Output(StringUtil.Loc("GCDirNotExist", searchRoot));
                return;
            }

            var allTrackingFiles = Directory.EnumerateFiles(searchRoot, Constants.Release.Path.TrackingConfigFile, SearchOption.AllDirectories);

            Trace.Verbose($"Find {allTrackingFiles.Count()} tracking files.");

            executionContext.Output(StringUtil.Loc("DirExpireLimit", expiration.TotalDays));
            executionContext.Output(StringUtil.Loc("CurrentUTC", DateTime.UtcNow.ToString("o")));

            // scan all sourcefolder tracking file, find which folder has never been used since UTC-expiration
            // the scan and garbage discovery should be best effort.
            // if the tracking file is in old format, just delete the folder since the first time the folder been use we will convert the tracking file to new format.
            foreach (var trackingFile in allTrackingFiles)
            {
                try
                {
                    executionContext.Output(StringUtil.Loc("EvaluateReleaseTrackingFile", trackingFile));
                    ReleaseTrackingConfig tracking = LoadIfExists(executionContext, trackingFile);

                    if (tracking.LastRunOn == null)
                    {
                        Trace.Verbose($"{trackingFile} is a old format tracking file.");

                        executionContext.Output(StringUtil.Loc("GCOldFormatTrackingFile", trackingFile));
                        MarkForGarbageCollection(executionContext, tracking);
                        IOUtil.DeleteFile(trackingFile);
                    }
                    else
                    {
                        Trace.Verbose($"{trackingFile} is a new format tracking file.");
                        ArgUtil.NotNull(tracking.LastRunOn, nameof(tracking.LastRunOn));
                        executionContext.Output(StringUtil.Loc("ReleaseDirLastUseTime", Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), tracking.ReleaseDirectory), tracking.LastRunOn?.ToString("u")));
                        if (DateTime.UtcNow - expiration > tracking.LastRunOn)
                        {
                            executionContext.Output(StringUtil.Loc("GCUnusedTrackingFile", trackingFile, expiration.TotalDays));
                            MarkForGarbageCollection(executionContext, tracking);
                            IOUtil.DeleteFile(trackingFile);
                        }
                    }
                }
                catch (Exception ex)
                {
                    executionContext.Error(StringUtil.Loc("ErrorDuringReleaseGC", trackingFile));
                    executionContext.Error(ex);
                }
            }
        }
        private void MarkForGarbageCollection(IExecutionContext executionContext, ReleaseTrackingConfig config)
        {
            Trace.Entering();

            // Write a copy of the tracking config to the GC folder.
            string gcDirectory = Path.Combine(
                HostContext.GetDirectory(WellKnownDirectory.Work),
                Constants.Release.Path.RootMappingDirectory,
                Constants.Release.Path.GarbageCollectionDirectory);
            string file = Path.Combine(
                gcDirectory,
                StringUtil.Format("{0}.json", Guid.NewGuid()));

            WriteToFile(file, config);
        }
Example #3
0
        public ReleaseTrackingConfig PrepareArtifactsDirectory(
            string workingDirectory,
            string collectionId,
            string projectId,
            string releaseDefinition)
        {
            Trace.Entering();

            ArgUtil.NotNull(workingDirectory, nameof(workingDirectory));
            ArgUtil.NotNull(collectionId, nameof(collectionId));
            ArgUtil.NotNull(projectId, nameof(projectId));
            ArgUtil.NotNull(releaseDefinition, nameof(releaseDefinition));

            ReleaseTrackingConfig trackingConfig;
            string trackingConfigFile = Path.Combine(
                workingDirectory,
                Constants.Release.Path.RootMappingDirectory,
                collectionId,
                projectId,
                releaseDefinition,
                Constants.Release.Path.DefinitionMapping);

            Trace.Verbose($"Mappings file: {trackingConfigFile}");
            trackingConfig = LoadIfExists(trackingConfigFile);
            if (trackingConfig == null || trackingConfig.LastRunOn == null)
            {
                Trace.Verbose("Mappings file does not exist or in older format. A new mapping file will be created");
                var releaseDirectorySuffix = ComputeFolderInteger(workingDirectory);
                trackingConfig = new ReleaseTrackingConfig();
                trackingConfig.ReleaseDirectory = string.Format(
                    "{0}{1}",
                    Constants.Release.Path.ReleaseDirectoryPrefix,
                    releaseDirectorySuffix);
                trackingConfig.UpdateJobRunProperties();
                WriteToFile(trackingConfigFile, trackingConfig);
                Trace.Verbose($"Created a new mapping file: {trackingConfigFile}");
            }

            return(trackingConfig);
        }