From 8d9233659ff290f2ebdcdd1d332a0c0a37a8b467 Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Wed, 31 Jan 2024 23:04:51 -0500 Subject: [PATCH] Use `dotnet-dump` when dumping OpenDream Closes #1750 --- build/Version.props | 6 +- src/Tgstation.Server.Api/Models/ErrorCode.cs | 18 +- .../Components/Engine/ByondInstallation.cs | 3 + .../Components/Engine/EngineExecutableLock.cs | 3 + .../Engine/EngineInstallationBase.cs | 3 + .../Components/Engine/EngineManager.cs | 35 ++- .../Components/Engine/IEngineInstallation.cs | 5 + .../Engine/OpenDreamInstallation.cs | 3 + .../Components/Engine/OpenDreamInstaller.cs | 17 +- .../Components/InstanceFactory.cs | 16 +- .../Components/Session/SessionController.cs | 19 +- .../Session/SessionControllerFactory.cs | 10 + src/Tgstation.Server.Host/Core/Application.cs | 7 +- .../Extensions/IOManagerExtensions.cs | 31 +++ .../System/DotnetDumpService.cs | 225 ++++++++++++++++++ .../System/DotnetHelper.cs | 47 ++++ .../System/IDotnetDumpService.cs | 28 +++ .../System/PosixProcessFeatures.cs | 2 +- .../Live/Instance/WatchdogTest.cs | 2 +- 19 files changed, 445 insertions(+), 35 deletions(-) create mode 100644 src/Tgstation.Server.Host/Extensions/IOManagerExtensions.cs create mode 100644 src/Tgstation.Server.Host/System/DotnetDumpService.cs create mode 100644 src/Tgstation.Server.Host/System/DotnetHelper.cs create mode 100644 src/Tgstation.Server.Host/System/IDotnetDumpService.cs diff --git a/build/Version.props b/build/Version.props index d244ecd7fa..c58250e7c5 100644 --- a/build/Version.props +++ b/build/Version.props @@ -5,10 +5,10 @@ 6.1.5 5.1.0 - 10.0.0 + 10.1.0 7.0.0 - 13.0.1 - 15.0.1 + 14.0.0 + 16.0.0 7.0.2 5.8.0 1.4.1 diff --git a/src/Tgstation.Server.Api/Models/ErrorCode.cs b/src/Tgstation.Server.Api/Models/ErrorCode.cs index 28f39a7638..1e1a42ce4d 100644 --- a/src/Tgstation.Server.Api/Models/ErrorCode.cs +++ b/src/Tgstation.Server.Api/Models/ErrorCode.cs @@ -528,10 +528,10 @@ namespace Tgstation.Server.Api.Models MissingGCore, /// - /// Non-zero gcore exit code. + /// Non-zero gcore/dotnet-dump exit code. /// - [Description("Could not create dump as gcore exited with a non-zero exit code!")] - GCoreFailure, + [Description("Could not create dump as the dumping process exited with a non-zero exit code!")] + DumpProcessFailure, /// /// Attempted to test merge with an invalid remote repository. @@ -636,15 +636,21 @@ namespace Tgstation.Server.Api.Models BroadcastFailure, /// - /// Could not compile OpenDream due to a missing dotnet executable. + /// Unable to locate the dotnet executable for a necessary operation. /// - [Description("OpenDream could not be compiled due to being unable to locate the dotnet executable!")] - OpenDreamCantFindDotnet, + [Description("Unable to locate the dotnet executable!")] + CantFindDotnet, /// /// Could not install OpenDream due to it not meeting the minimum version requirements. /// [Description("The specified OpenDream version is too old!")] OpenDreamTooOld, + + /// + /// Could not locally install the dotnet-dump tool. + /// + [Description("Could not locally install the dotnet-dump tool!")] + CantInstallDotnetDump, } } diff --git a/src/Tgstation.Server.Host/Components/Engine/ByondInstallation.cs b/src/Tgstation.Server.Host/Components/Engine/ByondInstallation.cs index 781add748f..b3ddd1cc1a 100644 --- a/src/Tgstation.Server.Host/Components/Engine/ByondInstallation.cs +++ b/src/Tgstation.Server.Host/Components/Engine/ByondInstallation.cs @@ -33,6 +33,9 @@ namespace Tgstation.Server.Host.Components.Engine /// public override bool PreferFileLogging => false; + /// + public override bool UseDotnetDump => false; + /// public override Task InstallationTask { get; } diff --git a/src/Tgstation.Server.Host/Components/Engine/EngineExecutableLock.cs b/src/Tgstation.Server.Host/Components/Engine/EngineExecutableLock.cs index 3e1a92be14..3136e10aef 100644 --- a/src/Tgstation.Server.Host/Components/Engine/EngineExecutableLock.cs +++ b/src/Tgstation.Server.Host/Components/Engine/EngineExecutableLock.cs @@ -36,6 +36,9 @@ namespace Tgstation.Server.Host.Components.Engine /// public Task InstallationTask => Instance.InstallationTask; + /// + public bool UseDotnetDump => Instance.UseDotnetDump; + /// public void DoNotDeleteThisSession() => DangerousDropReference(); diff --git a/src/Tgstation.Server.Host/Components/Engine/EngineInstallationBase.cs b/src/Tgstation.Server.Host/Components/Engine/EngineInstallationBase.cs index 77666748c7..22c1c14987 100644 --- a/src/Tgstation.Server.Host/Components/Engine/EngineInstallationBase.cs +++ b/src/Tgstation.Server.Host/Components/Engine/EngineInstallationBase.cs @@ -39,6 +39,9 @@ namespace Tgstation.Server.Host.Components.Engine /// public abstract bool PromptsForNetworkAccess { get; } + /// + public abstract bool UseDotnetDump { get; } + /// public abstract Task InstallationTask { get; } diff --git a/src/Tgstation.Server.Host/Components/Engine/EngineManager.cs b/src/Tgstation.Server.Host/Components/Engine/EngineManager.cs index 00290840f0..362099251e 100644 --- a/src/Tgstation.Server.Host/Components/Engine/EngineManager.cs +++ b/src/Tgstation.Server.Host/Components/Engine/EngineManager.cs @@ -14,6 +14,7 @@ using Tgstation.Server.Common.Extensions; using Tgstation.Server.Host.Components.Events; using Tgstation.Server.Host.IO; using Tgstation.Server.Host.Jobs; +using Tgstation.Server.Host.System; using Tgstation.Server.Host.Utils; namespace Tgstation.Server.Host.Components.Engine @@ -59,6 +60,11 @@ namespace Tgstation.Server.Host.Components.Engine /// readonly IEventConsumer eventConsumer; + /// + /// The for the . + /// + readonly IDotnetDumpService dotnetDumpService; + /// /// The for the . /// @@ -100,12 +106,14 @@ namespace Tgstation.Server.Host.Components.Engine /// The value of . /// The value of . /// The value of . + /// The value of . /// The value of . - public EngineManager(IIOManager ioManager, IEngineInstaller engineInstaller, IEventConsumer eventConsumer, ILogger logger) + public EngineManager(IIOManager ioManager, IEngineInstaller engineInstaller, IEventConsumer eventConsumer, IDotnetDumpService dotnetDumpService, ILogger logger) { this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); this.engineInstaller = engineInstaller ?? throw new ArgumentNullException(nameof(engineInstaller)); this.eventConsumer = eventConsumer ?? throw new ArgumentNullException(nameof(eventConsumer)); + this.dotnetDumpService = dotnetDumpService ?? throw new ArgumentNullException(nameof(dotnetDumpService)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); installedVersions = new Dictionary>(); @@ -380,6 +388,23 @@ namespace Tgstation.Server.Host.Components.Engine await ioManager.DeleteFile(ActiveVersionFileName, cancellationToken); } } + + bool needsDotnetDump; + lock (installedVersions) + needsDotnetDump = installedVersions.Values.Any(container => container.Instance.UseDotnetDump); + + if (needsDotnetDump) + { + logger.LogDebug("One or more engine installations uses dotnet-dump. Ensuring installation..."); + try + { + await dotnetDumpService.EnsureInstalled(true, cancellationToken); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Failed to install dotnet-dump! Engine versions that use it will instead use standard process dumps!"); + } + } } /// @@ -473,6 +498,14 @@ namespace Tgstation.Server.Host.Components.Engine var versionString = version.ToString(); await eventConsumer.HandleEvent(EventType.EngineInstallStart, new List { versionString }, false, cancellationToken); + if (installLock.UseDotnetDump) + { + if (progressReporter != null) + progressReporter.StageName = "Installing dotnet-dump"; + + await dotnetDumpService.EnsureInstalled(false, cancellationToken); + } + await InstallVersionFiles(progressReporter, version, customVersionStream, cancellationToken); ourTcs.SetResult(); diff --git a/src/Tgstation.Server.Host/Components/Engine/IEngineInstallation.cs b/src/Tgstation.Server.Host/Components/Engine/IEngineInstallation.cs index 402eac4a76..bdcfe2bf90 100644 --- a/src/Tgstation.Server.Host/Components/Engine/IEngineInstallation.cs +++ b/src/Tgstation.Server.Host/Components/Engine/IEngineInstallation.cs @@ -46,6 +46,11 @@ namespace Tgstation.Server.Host.Components.Engine /// bool PreferFileLogging { get; } + /// + /// If dotnet-dump should be used to create process dumps for this installation. + /// + bool UseDotnetDump { get; } + /// /// The that completes when the BYOND version finished installing. /// diff --git a/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstallation.cs b/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstallation.cs index ba400090e0..c522b9cedd 100644 --- a/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstallation.cs +++ b/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstallation.cs @@ -44,6 +44,9 @@ namespace Tgstation.Server.Host.Components.Engine /// public override bool PreferFileLogging => true; + /// + public override bool UseDotnetDump => true; + /// public override Task InstallationTask { get; } diff --git a/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstaller.cs b/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstaller.cs index dbb5577bc8..e4ee5c47d6 100644 --- a/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstaller.cs +++ b/src/Tgstation.Server.Host/Components/Engine/OpenDreamInstaller.cs @@ -9,7 +9,6 @@ using Microsoft.Extensions.Options; using Tgstation.Server.Api.Models; using Tgstation.Server.Common.Extensions; using Tgstation.Server.Common.Http; -using Tgstation.Server.Host.Common; using Tgstation.Server.Host.Components.Repository; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.IO; @@ -232,21 +231,7 @@ namespace Tgstation.Server.Host.Components.Engine await Task.WhenAll(dirsMoveTasks.Concat(filesMoveTask)); } - var dotnetPaths = DotnetHelper.GetPotentialDotnetPaths(platformIdentifier.IsWindows) - .ToList(); - var tasks = dotnetPaths - .Select(path => IOManager.FileExists(path, cancellationToken)) - .ToList(); - - await Task.WhenAll(tasks); - - var selectedPathIndex = tasks.FindIndex(pathValidTask => pathValidTask.Result); - - if (selectedPathIndex == -1) - throw new JobException(ErrorCode.OpenDreamCantFindDotnet); - - var dotnetPath = dotnetPaths[selectedPathIndex]; - + var dotnetPath = await DotnetHelper.GetDotnetPath(platformIdentifier, IOManager, cancellationToken); const string DeployDir = "tgs_deploy"; int? buildExitCode = null; await HandleExtremelyLongPathOperation( diff --git a/src/Tgstation.Server.Host/Components/InstanceFactory.cs b/src/Tgstation.Server.Host/Components/InstanceFactory.cs index 2b24bcb6cb..53dc6bbc5d 100644 --- a/src/Tgstation.Server.Host/Components/InstanceFactory.cs +++ b/src/Tgstation.Server.Host/Components/InstanceFactory.cs @@ -135,6 +135,11 @@ namespace Tgstation.Server.Host.Components /// readonly IAsyncDelayer asyncDelayer; + /// + /// The for the . + /// + readonly IDotnetDumpService dotnetDumpService; + /// /// The for the . /// @@ -177,6 +182,7 @@ namespace Tgstation.Server.Host.Components /// The value of . /// The value of . /// The value of . + /// The value of . /// The containing the value of . /// The containing the value of . public InstanceFactory( @@ -201,6 +207,7 @@ namespace Tgstation.Server.Host.Components IFileTransferTicketProvider fileTransferService, IRemoteDeploymentManagerFactory remoteDeploymentManagerFactory, IAsyncDelayer asyncDelayer, + IDotnetDumpService dotnetDumpService, IOptions generalConfigurationOptions, IOptions sessionConfigurationOptions) { @@ -225,6 +232,7 @@ namespace Tgstation.Server.Host.Components this.fileTransferService = fileTransferService ?? throw new ArgumentNullException(nameof(fileTransferService)); this.remoteDeploymentManagerFactory = remoteDeploymentManagerFactory ?? throw new ArgumentNullException(nameof(remoteDeploymentManagerFactory)); this.asyncDelayer = asyncDelayer ?? throw new ArgumentNullException(nameof(asyncDelayer)); + this.dotnetDumpService = dotnetDumpService ?? throw new ArgumentNullException(nameof(dotnetDumpService)); generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions)); sessionConfiguration = sessionConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(sessionConfigurationOptions)); } @@ -271,7 +279,12 @@ namespace Tgstation.Server.Host.Components var repoManager = repositoryManagerFactory.CreateRepositoryManager(repoIoManager, eventConsumer); try { - var engineManager = new EngineManager(byondIOManager, engineInstaller, eventConsumer, loggerFactory.CreateLogger()); + var engineManager = new EngineManager( + byondIOManager, + engineInstaller, + eventConsumer, + dotnetDumpService, + loggerFactory.CreateLogger()); var dmbFactory = new DmbFactory( databaseContextFactory, @@ -309,6 +322,7 @@ namespace Tgstation.Server.Host.Components serverPortProvider, eventConsumer, asyncDelayer, + dotnetDumpService, loggerFactory, loggerFactory.CreateLogger(), sessionConfiguration, diff --git a/src/Tgstation.Server.Host/Components/Session/SessionController.cs b/src/Tgstation.Server.Host/Components/Session/SessionController.cs index 2b0ec82db2..0abbe96b1a 100644 --- a/src/Tgstation.Server.Host/Components/Session/SessionController.cs +++ b/src/Tgstation.Server.Host/Components/Session/SessionController.cs @@ -149,6 +149,11 @@ namespace Tgstation.Server.Host.Components.Session /// readonly IAsyncDelayer asyncDelayer; + /// + /// The for the . + /// + readonly IDotnetDumpService dotnetDumpService; + /// /// The that completes when DD makes it's first bridge request. /// @@ -236,7 +241,8 @@ namespace Tgstation.Server.Host.Components.Session /// The value of . /// The value of . /// The for the . - /// The for the . + /// The value of . + /// The value of . /// The value of . /// The returning a to be run after the ends. /// The optional time to wait before failing the . @@ -253,6 +259,7 @@ namespace Tgstation.Server.Host.Components.Session IChatManager chat, IAssemblyInformationProvider assemblyInformationProvider, IAsyncDelayer asyncDelayer, + IDotnetDumpService dotnetDumpService, ILogger logger, Func postLifetimeCallback, uint? startupTimeout, @@ -272,6 +279,7 @@ namespace Tgstation.Server.Host.Components.Session ArgumentNullException.ThrowIfNull(assemblyInformationProvider); this.asyncDelayer = asyncDelayer ?? throw new ArgumentNullException(nameof(asyncDelayer)); + this.dotnetDumpService = dotnetDumpService ?? throw new ArgumentNullException(nameof(dotnetDumpService)); apiValidationSession = apiValidate; @@ -474,7 +482,14 @@ namespace Tgstation.Server.Host.Components.Session cancellationToken); /// - public ValueTask CreateDump(string outputFile, CancellationToken cancellationToken) => process.CreateDump(outputFile, cancellationToken); + public async ValueTask CreateDump(string outputFile, CancellationToken cancellationToken) + { + if (engineLock.UseDotnetDump + && await dotnetDumpService.Dump(process, outputFile, cancellationToken)) + return; + + await process.CreateDump(outputFile, cancellationToken); + } /// /// The for . diff --git a/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs b/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs index 41c087b22f..4c7fab19b9 100644 --- a/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs +++ b/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs @@ -106,6 +106,11 @@ namespace Tgstation.Server.Host.Components.Session /// readonly IAsyncDelayer asyncDelayer; + /// + /// The for the . + /// + readonly IDotnetDumpService dotnetDumpService; + /// /// The for the . /// @@ -178,6 +183,7 @@ namespace Tgstation.Server.Host.Components.Session /// The value of . /// The value of . /// The value of . + /// The value of . /// The value of . /// The value of . /// The value of . @@ -196,6 +202,7 @@ namespace Tgstation.Server.Host.Components.Session IServerPortProvider serverPortProvider, IEventConsumer eventConsumer, IAsyncDelayer asyncDelayer, + IDotnetDumpService dotnetDumpService, ILoggerFactory loggerFactory, ILogger logger, SessionConfiguration sessionConfiguration, @@ -215,6 +222,7 @@ namespace Tgstation.Server.Host.Components.Session this.serverPortProvider = serverPortProvider ?? throw new ArgumentNullException(nameof(serverPortProvider)); this.eventConsumer = eventConsumer ?? throw new ArgumentNullException(nameof(eventConsumer)); this.asyncDelayer = asyncDelayer ?? throw new ArgumentNullException(nameof(asyncDelayer)); + this.dotnetDumpService = dotnetDumpService ?? throw new ArgumentNullException(nameof(dotnetDumpService)); this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); this.sessionConfiguration = sessionConfiguration ?? throw new ArgumentNullException(nameof(sessionConfiguration)); @@ -346,6 +354,7 @@ namespace Tgstation.Server.Host.Components.Session chat, assemblyInformationProvider, asyncDelayer, + dotnetDumpService, loggerFactory.CreateLogger(), () => LogDDOutput( process, @@ -436,6 +445,7 @@ namespace Tgstation.Server.Host.Components.Session chat, assemblyInformationProvider, asyncDelayer, + dotnetDumpService, loggerFactory.CreateLogger(), () => ValueTask.CompletedTask, null, diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index d533a167a0..bad47cde0b 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -365,11 +365,9 @@ namespace Tgstation.Server.Host.Core } // only global repo manager should be for the OD repo + // god help me if we need more var openDreamRepositoryDirectory = ioManager.ConcatPath( - Environment.GetFolderPath( - Environment.SpecialFolder.LocalApplicationData, - Environment.SpecialFolderOption.DoNotVerify), - assemblyInformationProvider.VersionPrefix, + ioManager.GetPathInLocalDirectory(assemblyInformationProvider), "OpenDreamRepository"); services.AddSingleton( services => services @@ -416,6 +414,7 @@ namespace Tgstation.Server.Host.Core services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); // configure misc services services.AddSingleton(); diff --git a/src/Tgstation.Server.Host/Extensions/IOManagerExtensions.cs b/src/Tgstation.Server.Host/Extensions/IOManagerExtensions.cs new file mode 100644 index 0000000000..1992eeb435 --- /dev/null +++ b/src/Tgstation.Server.Host/Extensions/IOManagerExtensions.cs @@ -0,0 +1,31 @@ +using System; + +using Tgstation.Server.Host.IO; +using Tgstation.Server.Host.System; + +namespace Tgstation.Server.Host.Extensions +{ + /// + /// Extension methods for . + /// + static class IOManagerExtensions + { + /// + /// Gets the local application data folder used by TGS. + /// + /// The to use. + /// The to use. + /// The path to the local application data directory used by TGS. + public static string GetPathInLocalDirectory(this IIOManager ioManager, IAssemblyInformationProvider assemblyInformationProvider) + { + ArgumentNullException.ThrowIfNull(ioManager); + ArgumentNullException.ThrowIfNull(assemblyInformationProvider); + + return ioManager.ConcatPath( + Environment.GetFolderPath( + Environment.SpecialFolder.LocalApplicationData, // we use local application data here instead of comman application data because we store stuff here we don't want other users interfering with + Environment.SpecialFolderOption.DoNotVerify), + assemblyInformationProvider.VersionPrefix); + } + } +} diff --git a/src/Tgstation.Server.Host/System/DotnetDumpService.cs b/src/Tgstation.Server.Host/System/DotnetDumpService.cs new file mode 100644 index 0000000000..1c9d632606 --- /dev/null +++ b/src/Tgstation.Server.Host/System/DotnetDumpService.cs @@ -0,0 +1,225 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +using Tgstation.Server.Api.Models; +using Tgstation.Server.Host.Configuration; +using Tgstation.Server.Host.Extensions; +using Tgstation.Server.Host.IO; +using Tgstation.Server.Host.Jobs; +using Tgstation.Server.Host.Utils; + +namespace Tgstation.Server.Host.System +{ + /// + sealed class DotnetDumpService : IDotnetDumpService, IDisposable + { + /// + /// The for the . + /// + readonly IProcessExecutor processExecutor; + + /// + /// The for the . + /// + readonly IIOManager ioManager; + + /// + /// The for the . + /// + readonly IAssemblyInformationProvider assemblyInformationProvider; + + /// + /// The for the . + /// + readonly IPlatformIdentifier platformIdentifier; + + /// + /// The for the . + /// + readonly ILogger logger; + + /// + /// The for the . + /// + readonly SessionConfiguration sessionConfiguration; + + /// + /// used for checking for the presence of and installing dotnet-dump. + /// + readonly SemaphoreSlim installCheckSemaphore; + + /// + /// The result of the last installation check. means installed. means not installed. means the check was never run. + /// + bool? lastInstallCheckResult; + + /// + /// Initializes a new instance of the class. + /// + /// The value of . + /// The value of . + /// The value of . + /// The value of . + /// The value of . + /// The containing the value of . + public DotnetDumpService( + IProcessExecutor processExecutor, + IIOManager ioManager, + IAssemblyInformationProvider assemblyInformationProvider, + IPlatformIdentifier platformIdentifier, + ILogger logger, + IOptions sessionConfigurationOptions) + { + this.processExecutor = processExecutor ?? throw new ArgumentNullException(nameof(processExecutor)); + this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); + this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider)); + this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier)); + this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); + sessionConfiguration = sessionConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(sessionConfigurationOptions)); + + installCheckSemaphore = new SemaphoreSlim(1); + } + + /// + public void Dispose() => installCheckSemaphore.Dispose(); + + /// + public async ValueTask EnsureInstalled(bool deploymentPipeline, CancellationToken cancellationToken) + { + logger.LogTrace("EnsureInstalled"); + + if (lastInstallCheckResult == true) + return; + + using (await SemaphoreSlimContext.Lock(installCheckSemaphore, cancellationToken)) + { + var installDir = await CheckInstalled(cancellationToken); + if (lastInstallCheckResult == true) + return; + + await Install(installDir ?? GetDirectoryPath(), deploymentPipeline, cancellationToken); + } + } + + /// + public async ValueTask Dump(IProcess process, string outputFile, CancellationToken cancellationToken) + { + logger.LogTrace("dotnet-dump requested..."); + string? installDir = null; + if (!lastInstallCheckResult.HasValue) + using (await SemaphoreSlimContext.Lock(installCheckSemaphore, cancellationToken)) + installDir = await CheckInstalled(cancellationToken); + + if (lastInstallCheckResult != true) + return false; + + installDir ??= GetDirectoryPath(); + var exeExtension = platformIdentifier.IsWindows + ? ".exe" + : String.Empty; + + var resolvedInstallDir = ioManager.ResolvePath(installDir); + + var executablePath = ioManager.ConcatPath( + resolvedInstallDir, + $"dotnet-dump{exeExtension}"); + + await using var dumpProcess = processExecutor.LaunchProcess( + executablePath, + resolvedInstallDir, + $"collect -p {process.Id} -o \"{outputFile}\"", + readStandardHandles: true, + noShellExecute: true); + + int? exitCode; + using (cancellationToken.Register(() => dumpProcess.Terminate())) + exitCode = await dumpProcess.Lifetime; + + var output = await dumpProcess.GetCombinedOutput(cancellationToken); + + if (exitCode != 0) + throw new JobException( + ErrorCode.DumpProcessFailure, + new JobException( + $"Exit Code: {exitCode}{Environment.NewLine}Output:{Environment.NewLine}{output}")); + + logger.LogDebug("dotnet-dump output:{newline}{output}", Environment.NewLine, output); + + return true; + } + + /// + /// Sets if it is . + /// + /// The for the operation. + /// if was not . The result of otherwise. + async ValueTask CheckInstalled(CancellationToken cancellationToken) + { + if (lastInstallCheckResult.HasValue) + return null; + + logger.LogTrace("Checking if dotnet-dump is installed..."); + + var directory = GetDirectoryPath(); + lastInstallCheckResult = await ioManager.DirectoryExists(directory, cancellationToken); + + logger.LogTrace("dotnet-dump installed: {result}", lastInstallCheckResult.Value); + + return directory; + } + + /// + /// Locally install the dotnet-dump tool. + /// + /// The directory to install dotnet dump in. + /// If this operation is part of the deployment pipeline. + /// The for the operation. + /// A representing the running operation. + async ValueTask Install(string installDir, bool deploymentPipeline, CancellationToken cancellationToken) + { + var dotnetPath = await DotnetHelper.GetDotnetPath(platformIdentifier, ioManager, cancellationToken); + + logger.LogTrace("Ensuring installation directory is gone..."); + await ioManager.DeleteDirectory(installDir, cancellationToken); + + var resolvedInstallDir = ioManager.ResolvePath(installDir); + + logger.LogTrace("Installing dotnet-dump..."); + await using var installProcess = processExecutor.LaunchProcess( + dotnetPath, + ioManager.ResolvePath(), + $"tool install --tool-path \"{resolvedInstallDir}\" dotnet-dump", + readStandardHandles: true, + noShellExecute: true); + + if (deploymentPipeline && sessionConfiguration.LowPriorityDeploymentProcesses) + installProcess.AdjustPriority(false); + + int? exitCode; + using (cancellationToken.Register(() => installProcess.Terminate())) + exitCode = await installProcess.Lifetime; + + var output = await installProcess.GetCombinedOutput(cancellationToken); + + if (exitCode != 0) + throw new JobException( + ErrorCode.CantInstallDotnetDump, + new JobException( + $"Exit Code: {exitCode}{Environment.NewLine}Output:{Environment.NewLine}{output}")); + + logger.LogDebug("dotnet tool install output:{newline}{output}", Environment.NewLine, output); + } + + /// + /// Get the path to the dotnet-dump installation directory TGS uses. + /// + /// The path to the dotnet-dump installation directory. + string GetDirectoryPath() => ioManager.ConcatPath( + ioManager.GetPathInLocalDirectory(assemblyInformationProvider), + "dotnet-dump"); + } +} diff --git a/src/Tgstation.Server.Host/System/DotnetHelper.cs b/src/Tgstation.Server.Host/System/DotnetHelper.cs new file mode 100644 index 0000000000..9894e98a04 --- /dev/null +++ b/src/Tgstation.Server.Host/System/DotnetHelper.cs @@ -0,0 +1,47 @@ +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +using Tgstation.Server.Api.Models; +using Tgstation.Server.Host.IO; +using Tgstation.Server.Host.Jobs; + +namespace Tgstation.Server.Host.System +{ + /// + /// Helper methods for working with the dotnet executable. + /// + static class DotnetHelper + { + /// + /// Locate a dotnet executable to use. + /// + /// The to use. + /// The to use. + /// The for the operation. + /// A dotnet executable path to use. + public static async ValueTask GetDotnetPath(IPlatformIdentifier platformIdentifier, IIOManager ioManager, CancellationToken cancellationToken) + { + ArgumentNullException.ThrowIfNull(platformIdentifier); + ArgumentNullException.ThrowIfNull(ioManager); + + var dotnetPaths = Common.DotnetHelper.GetPotentialDotnetPaths(platformIdentifier.IsWindows) + .ToList(); + var tasks = dotnetPaths + .Select(path => ioManager.FileExists(path, cancellationToken)) + .ToList(); + + await Task.WhenAll(tasks); + + var selectedPathIndex = tasks.FindIndex(pathValidTask => pathValidTask.Result); + + if (selectedPathIndex == -1) + throw new JobException(ErrorCode.CantFindDotnet); + + var dotnetPath = dotnetPaths[selectedPathIndex]; + + return dotnetPath; + } + } +} diff --git a/src/Tgstation.Server.Host/System/IDotnetDumpService.cs b/src/Tgstation.Server.Host/System/IDotnetDumpService.cs new file mode 100644 index 0000000000..c8a1263e0b --- /dev/null +++ b/src/Tgstation.Server.Host/System/IDotnetDumpService.cs @@ -0,0 +1,28 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace Tgstation.Server.Host.System +{ + /// + /// Service for managing the dotnet-dump installation. + /// + public interface IDotnetDumpService + { + /// + /// Attempt to install dotnet-dump if it is not installed. + /// + /// If this operation is part of the deployment pipeline. + /// The for the operation. + /// A representing the running operation. + ValueTask EnsureInstalled(bool deploymentPipeline, CancellationToken cancellationToken); + + /// + /// Attempt to dump a given . + /// + /// The to dump. + /// The path to the output dump file. + /// The for the operation. + /// if the dump proceeded, if dotnet-dump was not installed. + ValueTask Dump(IProcess process, string outputFile, CancellationToken cancellationToken); + } +} diff --git a/src/Tgstation.Server.Host/System/PosixProcessFeatures.cs b/src/Tgstation.Server.Host/System/PosixProcessFeatures.cs index 8077577aaf..695fc776c5 100644 --- a/src/Tgstation.Server.Host/System/PosixProcessFeatures.cs +++ b/src/Tgstation.Server.Host/System/PosixProcessFeatures.cs @@ -104,7 +104,7 @@ namespace Tgstation.Server.Host.System if (exitCode != 0) throw new JobException( - ErrorCode.GCoreFailure, + ErrorCode.DumpProcessFailure, new JobException( $"Exit Code: {exitCode}{Environment.NewLine}Output:{Environment.NewLine}{output}")); diff --git a/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs b/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs index 4d5e1685a5..c963d90fa9 100644 --- a/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs +++ b/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs @@ -575,7 +575,7 @@ namespace Tgstation.Server.Tests.Live.Instance await WaitForJob(restartJob, 20, false, null, cancellationToken); } - Assert.IsTrue(job.ErrorCode == ErrorCode.GameServerOffline || job.ErrorCode == ErrorCode.GCoreFailure, $"{job.ErrorCode}: {job.ExceptionDetails}"); + Assert.IsTrue(job.ErrorCode == ErrorCode.GameServerOffline || job.ErrorCode == ErrorCode.DumpProcessFailure, $"{job.ErrorCode}: {job.ExceptionDetails}"); var restartJob2 = await instanceClient.DreamDaemon.Restart(cancellationToken); await WaitForJob(restartJob2, 20, false, null, cancellationToken);