diff --git a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs index f697d997d..5a1930b62 100644 --- a/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs +++ b/src/NzbDrone.Common.Test/ServiceFactoryFixture.cs @@ -3,6 +3,8 @@ using DryIoc; using DryIoc.Microsoft.DependencyInjection; using FluentAssertions; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Moq; using NUnit.Framework; using NzbDrone.Common.Composition.Extensions; using NzbDrone.Common.EnvironmentInfo; @@ -25,12 +27,15 @@ namespace NzbDrone.Common.Test .AddNzbDroneLogger() .AutoAddServices(Bootstrap.ASSEMBLIES) .AddDummyDatabase() - .AddStartupContext(new StartupContext("first", "second")) - .GetServiceProvider(); + .AddStartupContext(new StartupContext("first", "second")); - container.GetRequiredService().Register(); + container.RegisterInstance(new Mock().Object); - Mocker.SetConstant(container); + var serviceProvider = container.GetServiceProvider(); + + serviceProvider.GetRequiredService().Register(); + + Mocker.SetConstant(serviceProvider); var handlers = Subject.BuildAll>() .Select(c => c.GetType().FullName); diff --git a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs index 913a6c801..464862e98 100644 --- a/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs +++ b/src/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs @@ -1,9 +1,9 @@ using System; using System.Diagnostics; using System.IO; -using System.Reflection; using System.Security.Principal; -using System.ServiceProcess; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Hosting.WindowsServices; using NLog; using NzbDrone.Common.Processes; @@ -14,14 +14,11 @@ namespace NzbDrone.Common.EnvironmentInfo private readonly Logger _logger; private readonly DateTime _startTime = DateTime.UtcNow; - public RuntimeInfo(IServiceProvider serviceProvider, Logger logger) + public RuntimeInfo(IHostLifetime hostLifetime, Logger logger) { _logger = logger; - IsWindowsService = !IsUserInteractive && - OsInfo.IsWindows && - serviceProvider.ServiceExist(ServiceProvider.SERVICE_NAME) && - serviceProvider.GetStatus(ServiceProvider.SERVICE_NAME) == ServiceControllerStatus.StartPending; + IsWindowsService = hostLifetime is WindowsServiceLifetime; // net6.0 will return Radarr.dll for entry assembly, we need the actual // executable name (Radarr on linux). On mono this will return the location of diff --git a/src/NzbDrone.Common/Radarr.Common.csproj b/src/NzbDrone.Common/Radarr.Common.csproj index 453fa79a9..72300e36c 100644 --- a/src/NzbDrone.Common/Radarr.Common.csproj +++ b/src/NzbDrone.Common/Radarr.Common.csproj @@ -6,6 +6,7 @@ + diff --git a/src/NzbDrone.Host.Test/ContainerFixture.cs b/src/NzbDrone.Host.Test/ContainerFixture.cs index a84a06cc5..90ed204aa 100644 --- a/src/NzbDrone.Host.Test/ContainerFixture.cs +++ b/src/NzbDrone.Host.Test/ContainerFixture.cs @@ -4,6 +4,7 @@ using DryIoc; using DryIoc.Microsoft.DependencyInjection; using FluentAssertions; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Moq; using NUnit.Framework; using NzbDrone.Common; @@ -13,9 +14,7 @@ using NzbDrone.Common.Instrumentation.Extensions; using NzbDrone.Core.Datastore.Extensions; using NzbDrone.Core.Download; using NzbDrone.Core.Download.TrackedDownloads; -using NzbDrone.Core.ImportLists.CouchPotato; using NzbDrone.Core.Indexers; -using NzbDrone.Core.Jobs; using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Events; @@ -36,16 +35,15 @@ namespace NzbDrone.App.Test { var args = new StartupContext("first", "second"); - // set up a dummy broadcaster to allow tests to resolve - var mockBroadcaster = new Mock(); - var container = new Container(rules => rules.WithNzbDroneRules()) .AutoAddServices(Bootstrap.ASSEMBLIES) .AddNzbDroneLogger() .AddDummyDatabase() .AddStartupContext(args); - container.RegisterInstance(mockBroadcaster.Object); + // dummy lifetime and broadcaster so tests resolve + container.RegisterInstance(new Mock().Object); + container.RegisterInstance(new Mock().Object); _container = container.GetServiceProvider(); } diff --git a/src/NzbDrone.Host/AppLifetime.cs b/src/NzbDrone.Host/AppLifetime.cs index 9f29a76a2..fc15cfc4a 100644 --- a/src/NzbDrone.Host/AppLifetime.cs +++ b/src/NzbDrone.Host/AppLifetime.cs @@ -69,7 +69,7 @@ namespace NzbDrone.Host private void OnAppStopped() { - if (_runtimeInfo.RestartPending) + if (_runtimeInfo.RestartPending && !_runtimeInfo.IsWindowsService) { var restartArgs = GetRestartArgs(); diff --git a/src/NzbDrone.Host/Bootstrap.cs b/src/NzbDrone.Host/Bootstrap.cs index 050cdacea..e140e5faa 100644 --- a/src/NzbDrone.Host/Bootstrap.cs +++ b/src/NzbDrone.Host/Bootstrap.cs @@ -176,15 +176,17 @@ namespace Radarr.Host return ApplicationModes.UninstallService; } + Logger.Debug("Getting windows service status"); + // IsWindowsService can throw sometimes, so wrap it - bool isWindowsService = false; + var isWindowsService = false; try { isWindowsService = WindowsServiceHelpers.IsWindowsService(); } - catch + catch (Exception e) { - // don't care + Logger.Error(e, "Failed to get service status"); } if (OsInfo.IsWindows && isWindowsService)