Fixed: Restarting windows service from UI

This commit is contained in:
ta264 2021-11-22 21:14:29 +00:00
parent 6befbec381
commit 399f242f87
6 changed files with 24 additions and 21 deletions

View File

@ -3,6 +3,8 @@ using DryIoc;
using DryIoc.Microsoft.DependencyInjection; using DryIoc.Microsoft.DependencyInjection;
using FluentAssertions; using FluentAssertions;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Composition.Extensions; using NzbDrone.Common.Composition.Extensions;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
@ -25,12 +27,15 @@ namespace NzbDrone.Common.Test
.AddNzbDroneLogger() .AddNzbDroneLogger()
.AutoAddServices(Bootstrap.ASSEMBLIES) .AutoAddServices(Bootstrap.ASSEMBLIES)
.AddDummyDatabase() .AddDummyDatabase()
.AddStartupContext(new StartupContext("first", "second")) .AddStartupContext(new StartupContext("first", "second"));
.GetServiceProvider();
container.GetRequiredService<IAppFolderFactory>().Register(); container.RegisterInstance<IHostLifetime>(new Mock<IHostLifetime>().Object);
Mocker.SetConstant<System.IServiceProvider>(container); var serviceProvider = container.GetServiceProvider();
serviceProvider.GetRequiredService<IAppFolderFactory>().Register();
Mocker.SetConstant<System.IServiceProvider>(serviceProvider);
var handlers = Subject.BuildAll<IHandle<ApplicationStartedEvent>>() var handlers = Subject.BuildAll<IHandle<ApplicationStartedEvent>>()
.Select(c => c.GetType().FullName); .Select(c => c.GetType().FullName);

View File

@ -1,9 +1,9 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Reflection;
using System.Security.Principal; using System.Security.Principal;
using System.ServiceProcess; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using NLog; using NLog;
using NzbDrone.Common.Processes; using NzbDrone.Common.Processes;
@ -14,14 +14,11 @@ namespace NzbDrone.Common.EnvironmentInfo
private readonly Logger _logger; private readonly Logger _logger;
private readonly DateTime _startTime = DateTime.UtcNow; private readonly DateTime _startTime = DateTime.UtcNow;
public RuntimeInfo(IServiceProvider serviceProvider, Logger logger) public RuntimeInfo(IHostLifetime hostLifetime, Logger logger)
{ {
_logger = logger; _logger = logger;
IsWindowsService = !IsUserInteractive && IsWindowsService = hostLifetime is WindowsServiceLifetime;
OsInfo.IsWindows &&
serviceProvider.ServiceExist(ServiceProvider.SERVICE_NAME) &&
serviceProvider.GetStatus(ServiceProvider.SERVICE_NAME) == ServiceControllerStatus.StartPending;
// net6.0 will return Radarr.dll for entry assembly, we need the actual // 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 // executable name (Radarr on linux). On mono this will return the location of

View File

@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DryIoc.dll" Version="4.8.4" /> <PackageReference Include="DryIoc.dll" Version="4.8.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NLog" Version="4.7.12" /> <PackageReference Include="NLog" Version="4.7.12" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.7.4" /> <PackageReference Include="NLog.Extensions.Logging" Version="1.7.4" />

View File

@ -4,6 +4,7 @@ using DryIoc;
using DryIoc.Microsoft.DependencyInjection; using DryIoc.Microsoft.DependencyInjection;
using FluentAssertions; using FluentAssertions;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common; using NzbDrone.Common;
@ -13,9 +14,7 @@ using NzbDrone.Common.Instrumentation.Extensions;
using NzbDrone.Core.Datastore.Extensions; using NzbDrone.Core.Datastore.Extensions;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.TrackedDownloads; using NzbDrone.Core.Download.TrackedDownloads;
using NzbDrone.Core.ImportLists.CouchPotato;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
@ -36,16 +35,15 @@ namespace NzbDrone.App.Test
{ {
var args = new StartupContext("first", "second"); var args = new StartupContext("first", "second");
// set up a dummy broadcaster to allow tests to resolve
var mockBroadcaster = new Mock<IBroadcastSignalRMessage>();
var container = new Container(rules => rules.WithNzbDroneRules()) var container = new Container(rules => rules.WithNzbDroneRules())
.AutoAddServices(Bootstrap.ASSEMBLIES) .AutoAddServices(Bootstrap.ASSEMBLIES)
.AddNzbDroneLogger() .AddNzbDroneLogger()
.AddDummyDatabase() .AddDummyDatabase()
.AddStartupContext(args); .AddStartupContext(args);
container.RegisterInstance<IBroadcastSignalRMessage>(mockBroadcaster.Object); // dummy lifetime and broadcaster so tests resolve
container.RegisterInstance<IHostLifetime>(new Mock<IHostLifetime>().Object);
container.RegisterInstance<IBroadcastSignalRMessage>(new Mock<IBroadcastSignalRMessage>().Object);
_container = container.GetServiceProvider(); _container = container.GetServiceProvider();
} }

View File

@ -69,7 +69,7 @@ namespace NzbDrone.Host
private void OnAppStopped() private void OnAppStopped()
{ {
if (_runtimeInfo.RestartPending) if (_runtimeInfo.RestartPending && !_runtimeInfo.IsWindowsService)
{ {
var restartArgs = GetRestartArgs(); var restartArgs = GetRestartArgs();

View File

@ -176,15 +176,17 @@ namespace Radarr.Host
return ApplicationModes.UninstallService; return ApplicationModes.UninstallService;
} }
Logger.Debug("Getting windows service status");
// IsWindowsService can throw sometimes, so wrap it // IsWindowsService can throw sometimes, so wrap it
bool isWindowsService = false; var isWindowsService = false;
try try
{ {
isWindowsService = WindowsServiceHelpers.IsWindowsService(); isWindowsService = WindowsServiceHelpers.IsWindowsService();
} }
catch catch (Exception e)
{ {
// don't care Logger.Error(e, "Failed to get service status");
} }
if (OsInfo.IsWindows && isWindowsService) if (OsInfo.IsWindows && isWindowsService)