Fixed: Restarting windows service from UI

pull/650/head v0.1.5.1164
ta264 3 years ago committed by Qstick
parent 2e103d6dba
commit 02ecc04526

@ -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<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>>()
.Select(c => c.GetType().FullName);

@ -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

@ -7,6 +7,7 @@
<PackageReference Include="DryIoc.dll" Version="4.8.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.7.2" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.7.9" />
<PackageReference Include="Sentry" Version="3.8.3" />

@ -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;
@ -33,16 +34,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<IBroadcastSignalRMessage>();
var container = new Container(rules => rules.WithNzbDroneRules())
.AutoAddServices(Bootstrap.ASSEMBLIES)
.AddNzbDroneLogger()
.AddDummyDatabase()
.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();
}

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

@ -171,15 +171,17 @@ namespace NzbDrone.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)

Loading…
Cancel
Save