mirror of
https://github.com/Readarr/Readarr
synced 2026-02-14 18:53:04 +01:00
Fixed: Restarting windows service from UI
(cherry picked from commit 3ae1ccc5e25eb16420c1f4ab627f42e3f478b22e)
This commit is contained in:
parent
04e575903f
commit
05d24821f7
6 changed files with 23 additions and 19 deletions
|
|
@ -3,6 +3,8 @@
|
|||
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,14 @@ public void event_handlers_should_be_unique()
|
|||
.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 @@ public class RuntimeInfo : IRuntimeInfo
|
|||
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;
|
||||
|
||||
//Guarded to avoid issues when running in a non-managed process
|
||||
var entry = Process.GetCurrentProcess().MainModule;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="DryIoc.dll" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" />
|
||||
<PackageReference Include="Newtonsoft.Json" />
|
||||
<PackageReference Include="NLog" />
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
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 @@ public void SetUp()
|
|||
{
|
||||
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);
|
||||
// set up a dummy broadcaster and lifetime to allow tests to resolve
|
||||
container.RegisterInstance<IHostLifetime>(new Mock<IHostLifetime>().Object);
|
||||
container.RegisterInstance<IBroadcastSignalRMessage>(new Mock<IBroadcastSignalRMessage>().Object);
|
||||
|
||||
_container = container.GetServiceProvider();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ private void OnAppStarted()
|
|||
|
||||
private void OnAppStopped()
|
||||
{
|
||||
if (_runtimeInfo.RestartPending)
|
||||
if (_runtimeInfo.RestartPending && !_runtimeInfo.IsWindowsService)
|
||||
{
|
||||
var restartArgs = GetRestartArgs();
|
||||
|
||||
|
|
|
|||
|
|
@ -180,15 +180,17 @@ public static ApplicationModes GetApplicationMode(IStartupContext startupContext
|
|||
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…
Reference in a new issue