2019-12-26 23:01:54 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2016-12-08 08:52:44 -05:00
2016-09-09 20:13:41 -04:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
2024-05-22 13:17:22 -04:00
using System.Threading.Tasks ;
2016-09-09 20:13:41 -04:00
using System.Xml ;
2020-12-21 23:07:37 -04:00
using EpicGames.Core ;
2023-03-08 14:32:15 -05:00
using Microsoft.Extensions.Logging ;
2024-05-22 13:17:22 -04:00
using UnrealBuildTool ;
2016-09-09 20:13:41 -04:00
2021-12-10 16:56:09 -05:00
namespace AutomationTool.Tasks
2016-09-09 20:13:41 -04:00
{
2018-11-14 19:05:13 -05:00
/// <summary>
/// Parameters for a task that purges data from a symbol store after a given age
/// </summary>
public class AgeStoreTaskParameters
{
/// <summary>
/// The target platform to age symbols for.
/// </summary>
[TaskParameter]
2024-05-21 20:54:09 -04:00
public UnrealTargetPlatform Platform { get ; set ; }
2016-09-09 20:13:41 -04:00
2018-11-14 19:05:13 -05:00
/// <summary>
/// The symbol server directory.
/// </summary>
[TaskParameter]
2024-05-21 20:54:09 -04:00
public string StoreDir { get ; set ; }
2016-09-09 20:13:41 -04:00
2018-11-14 19:05:13 -05:00
/// <summary>
/// Number of days worth of symbols to keep.
/// </summary>
[TaskParameter]
2024-05-21 20:54:09 -04:00
public int Days { get ; set ; }
2016-09-09 20:13:41 -04:00
2019-01-08 11:38:48 -05:00
/// <summary>
2019-10-11 16:59:16 -04:00
/// The root of the build directory to check for existing buildversion named directories.
2019-01-08 11:38:48 -05:00
/// </summary>
[TaskParameter(Optional = true)]
2024-05-21 20:54:09 -04:00
public string BuildDir { get ; set ; }
2019-01-08 11:38:48 -05:00
2018-11-14 19:05:13 -05:00
/// <summary>
/// A substring to match in directory file names before deleting symbols. This allows the "age store" task
/// to avoid deleting symbols from other builds in the case where multiple builds share the same symbol server.
2019-10-11 16:59:16 -04:00
/// Specific use of the filter value is determined by the symbol server structure defined by the platform toolchain.
2018-11-14 19:05:13 -05:00
/// </summary>
[TaskParameter(Optional = true)]
2024-05-21 20:54:09 -04:00
public string Filter { get ; set ; }
2018-11-14 19:05:13 -05:00
}
2016-09-09 20:13:41 -04:00
2018-11-14 19:05:13 -05:00
/// <summary>
2019-10-11 16:59:16 -04:00
/// Task that strips symbols from a set of files. This task is named after the AGESTORE utility that comes with the Microsoft debugger tools SDK, but is actually a separate implementation. The main
2018-11-14 19:05:13 -05:00
/// difference is that it uses the last modified time rather than last access time to determine which files to delete.
/// </summary>
[TaskElement("AgeStore", typeof(AgeStoreTaskParameters))]
2021-12-10 15:36:47 -05:00
public class AgeStoreTask : BgTaskImpl
2018-11-14 19:05:13 -05:00
{
/// <summary>
/// Parameters for this task
/// </summary>
2024-05-22 13:17:22 -04:00
readonly AgeStoreTaskParameters _parameters ;
2016-09-09 20:13:41 -04:00
2018-11-14 19:05:13 -05:00
/// <summary>
/// Construct a spawn task
/// </summary>
2024-05-22 13:17:22 -04:00
/// <param name="parameters">Parameters for the task</param>
public AgeStoreTask ( AgeStoreTaskParameters parameters )
2018-11-14 19:05:13 -05:00
{
2024-05-22 13:17:22 -04:00
_parameters = parameters ;
2018-11-14 19:05:13 -05:00
}
2016-09-09 20:13:41 -04:00
2024-05-22 13:17:22 -04:00
private static void TryDelete ( DirectoryInfo directory )
2018-11-14 19:05:13 -05:00
{
try
2018-05-14 09:49:35 -04:00
{
2024-05-22 13:17:22 -04:00
directory . Delete ( true ) ;
Logger . LogInformation ( "Removed '{Arg0}'" , directory . FullName ) ;
2018-11-14 19:05:13 -05:00
}
catch
{
2024-05-22 13:17:22 -04:00
Logger . LogWarning ( "Couldn't delete '{Arg0}' - skipping" , directory . FullName ) ;
2018-11-14 19:05:13 -05:00
}
}
2024-05-22 13:17:22 -04:00
private static void TryDelete ( FileInfo file )
2018-11-14 19:05:13 -05:00
{
try
{
2024-05-22 13:17:22 -04:00
file . Delete ( ) ;
Logger . LogInformation ( "Removed '{Arg0}'" , file . FullName ) ;
2018-11-14 19:05:13 -05:00
}
catch
{
2024-05-22 13:17:22 -04:00
Logger . LogWarning ( "Couldn't delete '{Arg0}' - skipping" , file . FullName ) ;
2018-11-14 19:05:13 -05:00
}
}
2019-01-08 11:38:48 -05:00
// Checks if an existing build has a version file, returns false to NOT delete if it exists
2024-05-22 13:17:22 -04:00
private static bool CheckCanDeleteFromVersionFile ( HashSet < string > existingBuilds , DirectoryInfo directory , FileInfo individualFile = null )
2019-01-08 11:38:48 -05:00
{
// check for any existing version files
2024-05-22 13:17:22 -04:00
foreach ( FileInfo buildVersionFile in directory . EnumerateFiles ( "*.version" ) )
2019-01-08 11:38:48 -05:00
{
// If the buildversion matches one of the directories in build share provided, don't delete no matter the age.
2024-05-22 13:17:22 -04:00
string buildVersion = Path . GetFileNameWithoutExtension ( buildVersionFile . Name ) ;
if ( existingBuilds . Contains ( buildVersion ) )
2019-01-08 11:38:48 -05:00
{
// if checking for an individual file, see if the filename matches what's in the .version file.
// these file names won't have extensions.
2024-05-22 13:17:22 -04:00
if ( individualFile ! = null )
2019-01-08 11:38:48 -05:00
{
2024-05-22 13:17:22 -04:00
string individualFilePath = individualFile . FullName ;
string filePointerName = File . ReadAllText ( buildVersionFile . FullName ) . Trim ( ) ;
if ( filePointerName = = Path . GetFileNameWithoutExtension ( individualFilePath ) )
2019-01-08 11:38:48 -05:00
{
2024-05-22 13:17:22 -04:00
Logger . LogInformation ( "Found existing build {BuildVersion} in the BuildDir with matching individual file {IndividualFilePath} - skipping." , buildVersion , individualFilePath ) ;
2019-01-08 11:38:48 -05:00
return false ;
}
}
// otherwise it's okay to just mark the entire folder for delete
else
{
2024-05-22 13:17:22 -04:00
Logger . LogInformation ( "Found existing build {BuildVersion} in the BuildDir - skipping." , buildVersion ) ;
2019-01-08 11:38:48 -05:00
return false ;
}
}
}
return true ;
}
2024-05-22 13:17:22 -04:00
private static void RecurseDirectory ( DateTime expireTimeUtc , DirectoryInfo currentDirectory , string [ ] directoryStructure , int level , string filter , HashSet < string > existingBuilds , bool deleteIndividualFiles )
2018-11-14 19:05:13 -05:00
{
// Do a file search at the last level.
2024-05-22 13:17:22 -04:00
if ( level = = directoryStructure . Length )
2018-11-14 19:05:13 -05:00
{
2024-05-22 13:17:22 -04:00
if ( deleteIndividualFiles )
2018-11-14 19:05:13 -05:00
{
// Delete any file in the directory that is out of date.
2024-05-22 13:17:22 -04:00
foreach ( FileInfo outdatedFile in currentDirectory . EnumerateFiles ( ) . Where ( x = > x . LastWriteTimeUtc < expireTimeUtc & & x . Extension ! = ".version" ) )
2018-11-14 19:05:13 -05:00
{
2019-01-08 11:38:48 -05:00
// check to make sure this file is valid to delete
2024-05-22 13:17:22 -04:00
if ( CheckCanDeleteFromVersionFile ( existingBuilds , currentDirectory , outdatedFile ) )
2019-01-08 11:38:48 -05:00
{
2024-05-22 13:17:22 -04:00
TryDelete ( outdatedFile ) ;
2019-01-08 11:38:48 -05:00
}
2018-11-14 19:05:13 -05:00
}
}
2019-01-08 11:38:48 -05:00
// If all files are out of date, delete the directory...
2024-05-22 13:17:22 -04:00
else if ( currentDirectory . EnumerateFiles ( ) . Where ( x = > x . Extension ! = ".version" ) . All ( x = > x . LastWriteTimeUtc < expireTimeUtc ) & & CheckCanDeleteFromVersionFile ( existingBuilds , currentDirectory ) )
2019-01-08 11:38:48 -05:00
{
2024-05-22 13:17:22 -04:00
TryDelete ( currentDirectory ) ;
2019-01-08 11:38:48 -05:00
}
2018-11-14 19:05:13 -05:00
}
else
{
2024-05-22 13:17:22 -04:00
string [ ] patterns = directoryStructure [ level ] . Split ( ';' ) ;
foreach ( var pattern in patterns )
2018-11-14 19:05:13 -05:00
{
2024-05-22 13:17:22 -04:00
string replacedPattern = string . Format ( pattern , filter ) ;
2018-11-14 19:05:13 -05:00
2024-05-22 13:17:22 -04:00
foreach ( var childDirectory in currentDirectory . GetDirectories ( replacedPattern , SearchOption . TopDirectoryOnly ) )
2018-11-14 19:05:13 -05:00
{
2024-05-22 13:17:22 -04:00
RecurseDirectory ( expireTimeUtc , childDirectory , directoryStructure , level + 1 , filter , existingBuilds , deleteIndividualFiles ) ;
2018-11-14 19:05:13 -05:00
}
}
// Delete this directory if it is empty, and it is not the root directory.
2024-05-22 13:17:22 -04:00
if ( level > 0 & & ! currentDirectory . EnumerateFileSystemInfos ( ) . Any ( ) )
2024-05-22 16:39:16 -04:00
{
2024-05-22 13:17:22 -04:00
TryDelete ( currentDirectory ) ;
2024-05-22 16:39:16 -04:00
}
2018-11-14 19:05:13 -05:00
}
}
/// <summary>
2024-05-22 13:17:22 -04:00
/// ExecuteAsync the task.
2018-11-14 19:05:13 -05:00
/// </summary>
2024-05-22 13:17:22 -04:00
/// <param name="job">Information about the current job</param>
/// <param name="buildProducts">Set of build products produced by this node.</param>
/// <param name="tagNameToFileSet">Mapping from tag names to the set of files they include</param>
public override Task ExecuteAsync ( JobContext job , HashSet < FileReference > buildProducts , Dictionary < string , HashSet < FileReference > > tagNameToFileSet )
2018-11-14 19:05:13 -05:00
{
// Get the list of symbol file name patterns from the platform.
2024-05-22 13:17:22 -04:00
Platform targetPlatform = Platform . GetPlatform ( _parameters . Platform ) ;
string [ ] directoryStructure = targetPlatform . SymbolServerDirectoryStructure ;
if ( directoryStructure = = null )
2018-11-14 19:05:13 -05:00
{
throw new AutomationException ( "Platform does not specify the symbol server structure. Cannot age the symbol server." ) ;
}
2024-05-22 13:17:22 -04:00
string filter = string . IsNullOrWhiteSpace ( _parameters . Filter )
2018-11-14 19:05:13 -05:00
? string . Empty
2024-05-22 13:17:22 -04:00
: _parameters . Filter . Trim ( ) ;
2018-11-14 19:05:13 -05:00
2019-01-08 11:38:48 -05:00
// Eumerate the root directory of builds for buildversions to check against
// Folder names in the root directory should match the name of the .version files
2024-05-22 13:17:22 -04:00
HashSet < string > existingBuilds = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
if ( ! string . IsNullOrWhiteSpace ( _parameters . BuildDir ) )
2019-01-08 11:38:48 -05:00
{
2024-05-22 13:17:22 -04:00
DirectoryReference buildDir = new DirectoryReference ( _parameters . BuildDir ) ;
if ( DirectoryReference . Exists ( buildDir ) )
2019-01-08 11:38:48 -05:00
{
2024-05-22 13:17:22 -04:00
foreach ( string buildName in DirectoryReference . EnumerateDirectories ( buildDir ) . Select ( build = > build . GetDirectoryName ( ) ) )
2019-01-08 11:38:48 -05:00
{
2024-05-22 13:17:22 -04:00
existingBuilds . Add ( buildName ) ;
2019-01-08 11:38:48 -05:00
}
}
else
{
2024-05-22 13:17:22 -04:00
Logger . LogWarning ( "BuildDir of {Arg0} was provided but it doesn't exist! Will not check buildversions against it." , _parameters . BuildDir ) ;
2019-01-08 11:38:48 -05:00
}
}
2018-11-14 19:05:13 -05:00
// Get the time at which to expire files
2024-05-22 13:17:22 -04:00
DateTime expireTimeUtc = DateTime . UtcNow - TimeSpan . FromDays ( _parameters . Days ) ;
Logger . LogInformation ( "Expiring all files before {ExpireTimeUtc}..." , expireTimeUtc ) ;
2018-11-14 19:05:13 -05:00
// Scan the store directory and delete old symbol files
2024-05-22 13:17:22 -04:00
DirectoryReference symbolServerDirectory = ResolveDirectory ( _parameters . StoreDir ) ;
CommandUtils . OptionallyTakeLock ( targetPlatform . SymbolServerRequiresLock , symbolServerDirectory , TimeSpan . FromMinutes ( 15 ) , ( ) = >
2018-11-14 19:05:13 -05:00
{
2024-05-22 13:17:22 -04:00
RecurseDirectory ( expireTimeUtc , new DirectoryInfo ( symbolServerDirectory . FullName ) , directoryStructure , 0 , filter , existingBuilds , targetPlatform . SymbolServerDeleteIndividualFiles ) ;
2018-05-14 09:49:35 -04:00
} ) ;
2021-12-10 15:36:47 -05:00
return Task . CompletedTask ;
2018-11-14 19:05:13 -05:00
}
2016-09-09 20:13:41 -04:00
2018-11-14 19:05:13 -05:00
/// <summary>
/// Output this task out to an XML writer.
/// </summary>
2024-05-22 13:17:22 -04:00
public override void Write ( XmlWriter writer )
2018-11-14 19:05:13 -05:00
{
2024-05-22 13:17:22 -04:00
Write ( writer , _parameters ) ;
2018-11-14 19:05:13 -05:00
}
2016-09-09 20:13:41 -04:00
2018-11-14 19:05:13 -05:00
/// <summary>
/// Find all the tags which are used as inputs to this task
/// </summary>
/// <returns>The tag names which are read by this task</returns>
public override IEnumerable < string > FindConsumedTagNames ( )
{
yield break ;
}
2016-09-09 20:13:41 -04:00
2018-11-14 19:05:13 -05:00
/// <summary>
/// Find all the tags which are modified by this task
/// </summary>
/// <returns>The tag names which are modified by this task</returns>
public override IEnumerable < string > FindProducedTagNames ( )
{
yield break ;
}
}
2016-09-09 20:13:41 -04:00
}