Kill NzbDrone process if service couldn't be stopped.

better Process/Service handling.
pull/6/head
Keivan Beigi 12 years ago
parent 6e3aef8ab2
commit c1a75604fd

@ -1,4 +1,5 @@
using System.ServiceProcess;
using System;
using System.ServiceProcess;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Test.Common;
@ -16,23 +17,28 @@ namespace NzbDrone.Common.Test
public void Setup()
{
WindowsOnly();
if (Subject.ServiceExist(TEMP_SERVICE_NAME))
{
Subject.UnInstall(TEMP_SERVICE_NAME);
}
CleanupService();
}
[TearDown]
public void TearDown()
{
WindowsOnly();
CleanupService();
}
private void CleanupService()
{
if (Subject.ServiceExist(TEMP_SERVICE_NAME))
{
Subject.UnInstall(TEMP_SERVICE_NAME);
}
if (Subject.IsServiceRunning(ALWAYS_INSTALLED_SERVICE))
{
Subject.Stop(ALWAYS_INSTALLED_SERVICE);
}
}
[Test]
@ -86,6 +92,19 @@ namespace NzbDrone.Common.Test
.Should().Be(ServiceControllerStatus.Stopped);
}
[Test]
public void should_throw_if_starting_a_running_serivce()
{
Subject.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().NotBe(ServiceControllerStatus.Running);
Subject.Start(ALWAYS_INSTALLED_SERVICE);
Assert.Throws<InvalidOperationException>(() => Subject.Start(ALWAYS_INSTALLED_SERVICE));
ExceptionVerification.ExpectedWarns(1);
}
[Test]
public void Should_log_warn_if_on_stop_if_service_is_already_stopped()
{

@ -148,14 +148,14 @@
<Compile Include="Contract\ParseErrorReport.cs" />
<Compile Include="Model\AuthenticationType.cs" />
<Compile Include="PathExtensions.cs" />
<Compile Include="IDiskProvider.cs" />
<Compile Include="DiskProvider.cs" />
<Compile Include="EnvironmentInfo\AppFolderInfo.cs" />
<Compile Include="Model\ProcessInfo.cs" />
<Compile Include="IProcessProvider.cs" />
<Compile Include="ProcessProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\SharedAssemblyInfo.cs" />
<Compile Include="RestProvider.cs" />
<Compile Include="IServiceProvider.cs" />
<Compile Include="ServiceProvider.cs" />
<Compile Include="TinyIoC.cs" />
<Compile Include="TryParseExtension.cs" />
</ItemGroup>

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NLog;
@ -12,10 +11,9 @@ namespace NzbDrone.Common
ProcessInfo GetCurrentProcess();
ProcessInfo GetProcessById(int id);
IEnumerable<ProcessInfo> GetProcessByName(string name);
void Start(string path);
Process Start(string path);
Process Start(ProcessStartInfo startInfo);
void WaitForExit(Process process);
void Kill(int processId);
void SetPriority(int processId, ProcessPriorityClass priority);
void KillAll(string processName);
}
@ -55,9 +53,9 @@ namespace NzbDrone.Common
return Process.GetProcessesByName(name).Select(ConvertToProcessInfo).Where(p => p != null);
}
public void Start(string path)
public Process Start(string path)
{
Process.Start(path);
return Start(new ProcessStartInfo(path));
}
public Process Start(ProcessStartInfo startInfo)
@ -69,6 +67,7 @@ namespace NzbDrone.Common
StartInfo = startInfo
};
process.Start();
return process;
}
@ -78,27 +77,7 @@ namespace NzbDrone.Common
process.WaitForExit();
}
public void Kill(int processId)
{
if (processId == 0 || Process.GetProcesses().All(p => p.Id != processId))
{
Logger.Warn("Cannot find process with id: {0}", processId);
return;
}
var process = Process.GetProcessById(processId);
if (process.HasExited)
{
return;
}
Logger.Info("[{0}]: Killing process", process.Id);
process.Kill();
Logger.Info("[{0}]: Waiting for exit", process.Id);
process.WaitForExit();
Logger.Info("[{0}]: Process terminated successfully", process.Id);
}
public void SetPriority(int processId, ProcessPriorityClass priority)
{
@ -134,5 +113,27 @@ namespace NzbDrone.Common
Kill(processInfo.Id);
}
}
private void Kill(int processId)
{
if (processId == 0 || Process.GetProcesses().All(p => p.Id != processId))
{
Logger.Warn("Cannot find process with id: {0}", processId);
return;
}
var process = Process.GetProcessById(processId);
if (process.HasExited)
{
return;
}
Logger.Info("[{0}]: Killing process", process.Id);
process.Kill();
Logger.Info("[{0}]: Waiting for exit", process.Id);
process.WaitForExit();
Logger.Info("[{0}]: Process terminated successfully", process.Id);
}
}
}

@ -41,7 +41,11 @@ namespace NzbDrone.Common
var service = ServiceController.GetServices()
.SingleOrDefault(s => String.Equals(s.ServiceName, name, StringComparison.InvariantCultureIgnoreCase));
return service != null && (service.Status == ServiceControllerStatus.Running || service.Status == ServiceControllerStatus.StartPending);
return service != null && (
service.Status != ServiceControllerStatus.Stopped ||
service.Status == ServiceControllerStatus.StopPending ||
service.Status == ServiceControllerStatus.Paused ||
service.Status == ServiceControllerStatus.PausePending);
}
public virtual void Install(string serviceName)

@ -33,12 +33,13 @@ namespace NzbDrone.Update.UpdateEngine
{
try
{
_logger.Info("NzbDrone Service is installed and running");
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
catch (InvalidOperationException e)
catch (Exception e)
{
_logger.WarnException("couldn't stop service", e);
_logger.ErrorException("couldn't stop service", e);
}
}

Loading…
Cancel
Save