From 9f1f0ffe548dc5a473ed2b5bd021189f208d0799 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Mon, 15 Jun 2020 11:34:07 -0400 Subject: [PATCH 1/4] Add event for watchdog launches - Fix events triggered by the watchdog not executing their script --- .../Components/Events/EventType.cs | 6 ++++++ .../Components/InstanceFactory.cs | 1 + .../Components/Watchdog/BasicWatchdog.cs | 4 ++++ .../Components/Watchdog/ExperimentalWatchdog.cs | 4 ++++ .../Components/Watchdog/IWatchdogFactory.cs | 3 +++ .../Components/Watchdog/WatchdogBase.cs | 17 +++++++++++++++-- .../Components/Watchdog/WatchdogFactory.cs | 7 +++++++ .../Components/Watchdog/WindowsWatchdog.cs | 4 ++++ .../Watchdog/WindowsWatchdogFactory.cs | 3 +++ 9 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Events/EventType.cs b/src/Tgstation.Server.Host/Components/Events/EventType.cs index 419fb9ec3b..9b648382c9 100644 --- a/src/Tgstation.Server.Host/Components/Events/EventType.cs +++ b/src/Tgstation.Server.Host/Components/Events/EventType.cs @@ -106,5 +106,11 @@ /// [EventScript("WatchdogDetach")] WatchdogDetach, + + /// + /// Before the watchdog launches. No parameters. + /// + [EventScript("WatchdogLaunch")] + WatchdogLaunch } } diff --git a/src/Tgstation.Server.Host/Components/InstanceFactory.cs b/src/Tgstation.Server.Host/Components/InstanceFactory.cs index 15876d33cd..cad5c38fa7 100644 --- a/src/Tgstation.Server.Host/Components/InstanceFactory.cs +++ b/src/Tgstation.Server.Host/Components/InstanceFactory.cs @@ -250,6 +250,7 @@ namespace Tgstation.Server.Host.Components sessionControllerFactory, gameIoManager, diagnosticsIOManager, + eventConsumer, metadata.CloneMetadata(), metadata.DreamDaemonSettings); eventConsumer.SetWatchdog(watchdog); diff --git a/src/Tgstation.Server.Host/Components/Watchdog/BasicWatchdog.cs b/src/Tgstation.Server.Host/Components/Watchdog/BasicWatchdog.cs index 73d7a74d22..63cdb20557 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/BasicWatchdog.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/BasicWatchdog.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Chat; using Tgstation.Server.Host.Components.Deployment; +using Tgstation.Server.Host.Components.Events; using Tgstation.Server.Host.Components.Session; using Tgstation.Server.Host.Core; using Tgstation.Server.Host.Database; @@ -51,6 +52,7 @@ namespace Tgstation.Server.Host.Components.Watchdog /// The for the . /// The for the . /// The for the . + /// The for the . /// The for the . /// The for the . /// The for the . @@ -65,6 +67,7 @@ namespace Tgstation.Server.Host.Components.Watchdog IServerControl serverControl, IAsyncDelayer asyncDelayer, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, ILogger logger, DreamDaemonLaunchParameters initialLaunchParameters, Api.Models.Instance instance, @@ -79,6 +82,7 @@ namespace Tgstation.Server.Host.Components.Watchdog serverControl, asyncDelayer, diagnosticsIOManager, + eventConsumer, logger, initialLaunchParameters, instance, diff --git a/src/Tgstation.Server.Host/Components/Watchdog/ExperimentalWatchdog.cs b/src/Tgstation.Server.Host/Components/Watchdog/ExperimentalWatchdog.cs index 0addffef36..95d2fc12b2 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/ExperimentalWatchdog.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/ExperimentalWatchdog.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Chat; using Tgstation.Server.Host.Components.Deployment; +using Tgstation.Server.Host.Components.Events; using Tgstation.Server.Host.Components.Session; using Tgstation.Server.Host.Core; using Tgstation.Server.Host.Database; @@ -62,6 +63,7 @@ namespace Tgstation.Server.Host.Components.Watchdog /// The for the . /// The for the . /// The for the . + /// The for the . /// The for the . /// The for the . /// The for the . @@ -76,6 +78,7 @@ namespace Tgstation.Server.Host.Components.Watchdog IServerControl serverControl, IAsyncDelayer asyncDelayer, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, ILogger logger, DreamDaemonLaunchParameters initialLaunchParameters, Api.Models.Instance instance, bool autoStart) @@ -89,6 +92,7 @@ namespace Tgstation.Server.Host.Components.Watchdog serverControl, asyncDelayer, diagnosticsIOManager, + eventConsumer, logger, initialLaunchParameters, instance, diff --git a/src/Tgstation.Server.Host/Components/Watchdog/IWatchdogFactory.cs b/src/Tgstation.Server.Host/Components/Watchdog/IWatchdogFactory.cs index 63f30ec362..134628b460 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/IWatchdogFactory.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/IWatchdogFactory.cs @@ -1,6 +1,7 @@ using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Chat; using Tgstation.Server.Host.Components.Deployment; +using Tgstation.Server.Host.Components.Events; using Tgstation.Server.Host.Components.Session; using Tgstation.Server.Host.IO; @@ -20,6 +21,7 @@ namespace Tgstation.Server.Host.Components.Watchdog /// The for the /// The pointing to the Game directory for the . /// The pointing to the Diagnostics directory for the . + /// The for the . /// The for the /// The initial for the /// A new @@ -30,6 +32,7 @@ namespace Tgstation.Server.Host.Components.Watchdog ISessionControllerFactory sessionControllerFactory, IIOManager gameIOManager, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, Api.Models.Instance instance, DreamDaemonSettings settings); } diff --git a/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs b/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs index d8e6139afa..ee3fe60946 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs @@ -120,6 +120,11 @@ namespace Tgstation.Server.Host.Components.Watchdog /// readonly IIOManager diagnosticsIOManager; + /// + /// The that is not the + /// + readonly IEventConsumer eventConsumer; + /// /// used for . /// @@ -177,6 +182,7 @@ namespace Tgstation.Server.Host.Components.Watchdog /// The to populate with /// The value of . /// The value of . + /// The value of . /// The value of /// The initial value of . May be modified /// The value of @@ -191,6 +197,7 @@ namespace Tgstation.Server.Host.Components.Watchdog IServerControl serverControl, IAsyncDelayer asyncDelayer, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, ILogger logger, DreamDaemonLaunchParameters initialLaunchParameters, Api.Models.Instance instance, @@ -204,6 +211,7 @@ namespace Tgstation.Server.Host.Components.Watchdog this.jobManager = jobManager ?? throw new ArgumentNullException(nameof(jobManager)); AsyncDelayer = asyncDelayer ?? throw new ArgumentNullException(nameof(asyncDelayer)); this.diagnosticsIOManager = diagnosticsIOManager ?? throw new ArgumentNullException(nameof(diagnosticsIOManager)); + this.eventConsumer = eventConsumer ?? throw new ArgumentNullException(nameof(eventConsumer)); Logger = logger ?? throw new ArgumentNullException(nameof(logger)); ActiveLaunchParameters = initialLaunchParameters ?? throw new ArgumentNullException(nameof(initialLaunchParameters)); this.instance = instance ?? throw new ArgumentNullException(nameof(instance)); @@ -255,7 +263,7 @@ namespace Tgstation.Server.Host.Components.Watchdog return; if (!graceful) { - var eventTask = HandleEvent(releaseServers ? EventType.WatchdogDetach : EventType.WatchdogShutdown, null, cancellationToken); + var eventTask = eventConsumer.HandleEvent(releaseServers ? EventType.WatchdogDetach : EventType.WatchdogShutdown, null, cancellationToken); var chatTask = announce ? Chat.SendWatchdogMessage("Shutting down...", false, cancellationToken) : Task.CompletedTask; @@ -357,7 +365,11 @@ namespace Tgstation.Server.Host.Components.Watchdog if (startMonitor && await StopMonitor().ConfigureAwait(false)) chatTask = Chat.SendWatchdogMessage("Automatic retry sequence cancelled by manual launch. Restarting...", false, cancellationToken); else if (announce) + { chatTask = Chat.SendWatchdogMessage(reattachInfo == null ? "Launching..." : "Reattaching...", false, cancellationToken); // simple announce + if (reattachInfo == null) + await eventConsumer.HandleEvent(EventType.WatchdogLaunch, Enumerable.Empty(), cancellationToken).ConfigureAwait(false); + } else chatTask = Task.CompletedTask; // no announce @@ -751,8 +763,9 @@ namespace Tgstation.Server.Host.Components.Watchdog } /// - public async Task HandleEvent(EventType eventType, IEnumerable parameters, CancellationToken cancellationToken) + async Task IEventConsumer.HandleEvent(EventType eventType, IEnumerable parameters, CancellationToken cancellationToken) { + // Method explicitly implemented to prevent accidental calls when this.eventConsumer should be used. var activeServer = GetActiveController(); // Server may have ended diff --git a/src/Tgstation.Server.Host/Components/Watchdog/WatchdogFactory.cs b/src/Tgstation.Server.Host/Components/Watchdog/WatchdogFactory.cs index b7fcaaa3bd..abbe180c96 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/WatchdogFactory.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/WatchdogFactory.cs @@ -4,6 +4,7 @@ using System; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Chat; using Tgstation.Server.Host.Components.Deployment; +using Tgstation.Server.Host.Components.Events; using Tgstation.Server.Host.Components.Session; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.Core; @@ -79,6 +80,7 @@ namespace Tgstation.Server.Host.Components.Watchdog ISessionControllerFactory sessionControllerFactory, IIOManager gameIOManager, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, Api.Models.Instance instance, DreamDaemonSettings settings) { @@ -93,6 +95,7 @@ namespace Tgstation.Server.Host.Components.Watchdog ServerControl, AsyncDelayer, diagnosticsIOManager, + eventConsumer, LoggerFactory.CreateLogger(), settings, instance, @@ -105,6 +108,7 @@ namespace Tgstation.Server.Host.Components.Watchdog sessionControllerFactory, gameIOManager, diagnosticsIOManager, + eventConsumer, instance, settings); } @@ -118,6 +122,7 @@ namespace Tgstation.Server.Host.Components.Watchdog /// The for the /// The pointing to the Game directory for the . /// The pointing to the Diagnostics directory for the . + /// The for the . /// The for the /// The initial for the /// A new @@ -128,6 +133,7 @@ namespace Tgstation.Server.Host.Components.Watchdog ISessionControllerFactory sessionControllerFactory, IIOManager gameIOManager, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, Api.Models.Instance instance, DreamDaemonSettings settings) => new BasicWatchdog( @@ -140,6 +146,7 @@ namespace Tgstation.Server.Host.Components.Watchdog ServerControl, AsyncDelayer, diagnosticsIOManager, + eventConsumer, LoggerFactory.CreateLogger(), settings, instance, diff --git a/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdog.cs b/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdog.cs index 206995886c..aa59fc3d35 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdog.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdog.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Chat; using Tgstation.Server.Host.Components.Deployment; +using Tgstation.Server.Host.Components.Events; using Tgstation.Server.Host.Components.Session; using Tgstation.Server.Host.Core; using Tgstation.Server.Host.Database; @@ -55,6 +56,7 @@ namespace Tgstation.Server.Host.Components.Watchdog /// The for the . /// The for the . /// The for the . + /// The for the . /// The value of . /// The value of . /// The for the . @@ -71,6 +73,7 @@ namespace Tgstation.Server.Host.Components.Watchdog IServerControl serverControl, IAsyncDelayer asyncDelayer, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, IIOManager gameIOManager, ISymlinkFactory symlinkFactory, ILogger logger, @@ -86,6 +89,7 @@ namespace Tgstation.Server.Host.Components.Watchdog serverControl, asyncDelayer, diagnosticsIOManager, + eventConsumer, logger, initialLaunchParameters, instance, diff --git a/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdogFactory.cs b/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdogFactory.cs index d8c4314280..f1f23e22f6 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdogFactory.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/WindowsWatchdogFactory.cs @@ -4,6 +4,7 @@ using System; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Chat; using Tgstation.Server.Host.Components.Deployment; +using Tgstation.Server.Host.Components.Events; using Tgstation.Server.Host.Components.Session; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.Core; @@ -60,6 +61,7 @@ namespace Tgstation.Server.Host.Components.Watchdog ISessionControllerFactory sessionControllerFactory, IIOManager gameIOManager, IIOManager diagnosticsIOManager, + IEventConsumer eventConsumer, Api.Models.Instance instance, DreamDaemonSettings settings) => new WindowsWatchdog( @@ -72,6 +74,7 @@ namespace Tgstation.Server.Host.Components.Watchdog ServerControl, AsyncDelayer, diagnosticsIOManager, + eventConsumer, gameIOManager, symlinkFactory, LoggerFactory.CreateLogger(), From c1aa9ec8cf22bc97bb1df5a1ec84a8ab697f991e Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Mon, 15 Jun 2020 11:36:44 -0400 Subject: [PATCH 2/4] DMAPI comment about event 17 --- src/DMAPI/tgs.dm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DMAPI/tgs.dm b/src/DMAPI/tgs.dm index 4493aef72c..d0cf1618a6 100644 --- a/src/DMAPI/tgs.dm +++ b/src/DMAPI/tgs.dm @@ -95,6 +95,8 @@ #define TGS_EVENT_WATCHDOG_SHUTDOWN 15 /// Before the watchdog detaches for a TGS update/restart. No parameters. #define TGS_EVENT_WATCHDOG_DETACH 16 +// We don't actually implement this value as the DMAPI can never receive it +// #define TGS_EVENT_WATCHDOG_LAUNCH 17 // OTHER ENUMS From 372a0d70511769e01d4d8a566019a6adaaa1c32f Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Mon, 15 Jun 2020 11:41:04 -0400 Subject: [PATCH 3/4] Minor code cleanup --- .../Components/Watchdog/WatchdogBase.cs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs b/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs index ee3fe60946..f95eb8111b 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/WatchdogBase.cs @@ -361,17 +361,19 @@ namespace Tgstation.Server.Host.Components.Watchdog throw new JobException(ErrorCode.WatchdogCompileJobCorrupted); // this is necessary, the monitor could be in it's sleep loop trying to restart, if so cancel THAT monitor and start our own with blackjack and hookers - Task chatTask; + Task announceTask; if (startMonitor && await StopMonitor().ConfigureAwait(false)) - chatTask = Chat.SendWatchdogMessage("Automatic retry sequence cancelled by manual launch. Restarting...", false, cancellationToken); + announceTask = Chat.SendWatchdogMessage("Automatic retry sequence cancelled by manual launch. Restarting...", false, cancellationToken); else if (announce) { - chatTask = Chat.SendWatchdogMessage(reattachInfo == null ? "Launching..." : "Reattaching...", false, cancellationToken); // simple announce + announceTask = Chat.SendWatchdogMessage(reattachInfo == null ? "Launching..." : "Reattaching...", false, cancellationToken); // simple announce if (reattachInfo == null) - await eventConsumer.HandleEvent(EventType.WatchdogLaunch, Enumerable.Empty(), cancellationToken).ConfigureAwait(false); + announceTask = Task.WhenAll( + eventConsumer.HandleEvent(EventType.WatchdogLaunch, Enumerable.Empty(), cancellationToken), + announceTask); } else - chatTask = Task.CompletedTask; // no announce + announceTask = Task.CompletedTask; // no announce // since neither server is running, this is safe to do LastLaunchParameters = ActiveLaunchParameters; @@ -381,11 +383,11 @@ namespace Tgstation.Server.Host.Components.Watchdog var recursiveCallToHappen = false; try { - await InitControllers(() => recursiveCallToHappen = true, chatTask, reattachInfo, cancellationToken).ConfigureAwait(false); + await InitControllers(() => recursiveCallToHappen = true, announceTask, reattachInfo, cancellationToken).ConfigureAwait(false); if (recursiveCallToHappen) return; - await chatTask.ConfigureAwait(false); + await announceTask.ConfigureAwait(false); Logger.LogInformation("Launched servers successfully"); Running = true; @@ -401,14 +403,14 @@ namespace Tgstation.Server.Host.Components.Watchdog // don't try to send chat tasks or warning logs if were suppressing exceptions or cancelled if (!recursiveCallToHappen && !cancellationToken.IsCancellationRequested) { - var originalChatTask = chatTask; + var originalChatTask = announceTask; async Task ChainChatTaskWithErrorMessage() { await originalChatTask.ConfigureAwait(false); await Chat.SendWatchdogMessage("Startup failed!", false, cancellationToken).ConfigureAwait(false); } - chatTask = ChainChatTaskWithErrorMessage(); + announceTask = ChainChatTaskWithErrorMessage(); Logger.LogWarning("Failed to start watchdog: {0}", e.ToString()); } @@ -419,9 +421,12 @@ namespace Tgstation.Server.Host.Components.Watchdog // finish the chat task that's in flight try { - await chatTask.ConfigureAwait(false); + await announceTask.ConfigureAwait(false); + } + catch (OperationCanceledException) + { + Logger.LogTrace("Announcement task canceled!"); } - catch (OperationCanceledException) { } } } From 1383bdd1e86adc9154aad631d3b67e523a834ad5 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Mon, 15 Jun 2020 11:41:51 -0400 Subject: [PATCH 4/4] Remove unused member variable --- .../Components/InstanceClient.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Tgstation.Server.Client/Components/InstanceClient.cs b/src/Tgstation.Server.Client/Components/InstanceClient.cs index 2bf76eca71..f5f31907ec 100644 --- a/src/Tgstation.Server.Client/Components/InstanceClient.cs +++ b/src/Tgstation.Server.Client/Components/InstanceClient.cs @@ -33,19 +33,16 @@ namespace Tgstation.Server.Client.Components /// public IJobsClient Jobs { get; } - /// - /// The for the - /// - readonly IApiClient apiClient; - /// /// Construct a /// - /// The value of + /// The used to construct component clients. /// The value of public InstanceClient(IApiClient apiClient, Instance instance) { - this.apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient)); + if (apiClient == null) + throw new ArgumentNullException(nameof(apiClient)); + Metadata = instance ?? throw new ArgumentNullException(nameof(instance)); Byond = new ByondClient(apiClient, instance);