Fixed an upgrade/service bug where it would try to stop an already stopped service.

pull/2/head
kay.one 13 years ago
parent 9dbc97223f
commit e35a4bf8ac

@ -67,7 +67,7 @@
<Compile Include="DiskProviderTests.cs" /> <Compile Include="DiskProviderTests.cs" />
<Compile Include="EnviromentProviderTest.cs" /> <Compile Include="EnviromentProviderTest.cs" />
<Compile Include="ProcessProviderTests.cs" /> <Compile Include="ProcessProviderTests.cs" />
<Compile Include="ServiceControllerTests.cs" /> <Compile Include="ServiceProviderTests.cs" />
<Compile Include="WebClientTests.cs" /> <Compile Include="WebClientTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -8,10 +8,10 @@ using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test namespace NzbDrone.Common.Test
{ {
[TestFixture] [TestFixture]
public class ServiceControllerTests:TestBase public class ServiceProviderTests : TestBase
{ {
private const string ALWAYS_INSTALLED_SERVICE = "SCardSvr"; //Smart Card private const string ALWAYS_INSTALLED_SERVICE = "SCardSvr"; //Smart Card
private const string TEMP_SERVICE_NAME = "NzbDrone_Nunit"; //Smart Card private const string TEMP_SERVICE_NAME = "NzbDrone_Nunit";
private ServiceProvider serviceProvider; private ServiceProvider serviceProvider;
@ -91,5 +91,21 @@ namespace NzbDrone.Common.Test
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().Be(ServiceControllerStatus.Stopped); .Should().Be(ServiceControllerStatus.Stopped);
} }
[Test]
public void Should_log_warn_if_on_stop_if_service_is_already_stopped()
{
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().NotBe(ServiceControllerStatus.Running);
//Act
serviceProvider.Stop(ALWAYS_INSTALLED_SERVICE);
//Assert
serviceProvider.GetService(ALWAYS_INSTALLED_SERVICE).Status
.Should().Be(ServiceControllerStatus.Stopped);
ExceptionVerification.ExcpectedWarns(1);
}
} }
} }

@ -100,6 +100,8 @@ namespace NzbDrone.Common
Logger.Info("Service is currently {0}", service.Status); Logger.Info("Service is currently {0}", service.Status);
if (service.Status != ServiceControllerStatus.Stopped)
{
service.Stop(); service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60)); service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
@ -113,6 +115,11 @@ namespace NzbDrone.Common
Logger.Error("Service stop request has timed out. {0}", service.Status); Logger.Error("Service stop request has timed out. {0}", service.Status);
} }
} }
else
{
Logger.Warn("Service {0} is already in stopped state.", service.ServiceName);
}
}
public virtual void Start(string serviceName) public virtual void Start(string serviceName)
{ {

@ -51,9 +51,10 @@ namespace NzbDrone.Update.Test
} }
[Test] [Test]
public void should_stop_nzbdrone_service_if_installed() public void should_stop_nzbdrone_service_if_installed_and_running()
{ {
WithInstalledService(); WithInstalledService();
WithServiceRunning(true);
//Act //Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER); Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
@ -62,6 +63,19 @@ namespace NzbDrone.Update.Test
Mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once()); Mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Once());
} }
[Test]
public void should_not_stop_nzbdrone_service_if_installed_but_not_running()
{
WithInstalledService();
WithServiceRunning(false);
//Act
Mocker.Resolve<UpdateProvider>().Start(TARGET_FOLDER);
//Assert
Mocker.GetMock<ServiceProvider>().Verify(c => c.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME), Times.Never());
}
[Test] [Test]
public void should_not_stop_nzbdrone_service_if_service_isnt_installed() public void should_not_stop_nzbdrone_service_if_service_isnt_installed()
{ {

@ -49,12 +49,11 @@ namespace NzbDrone.Update.Providers
bool isService = false; bool isService = false;
logger.Info("Stopping all running services"); logger.Info("Stopping all running services");
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{ if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)
if (_serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME)) && _serviceProvider.IsServiceRunning(ServiceProvider.NZBDRONE_SERVICE_NAME))
{ {
isService = true; isService = true;
}
_serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME); _serviceProvider.Stop(ServiceProvider.NZBDRONE_SERVICE_NAME);
} }

@ -33,6 +33,7 @@ namespace NzbDrone
{ {
Logger.Info("Application mode: {0}", applicationMode); Logger.Info("Application mode: {0}", applicationMode);
//TODO:move this outside, it should be one of application modes (ApplicationMode.Service?)
if (!_enviromentProvider.IsUserInteractive) if (!_enviromentProvider.IsUserInteractive)
{ {
_serviceProvider.Run(_applicationServer); _serviceProvider.Run(_applicationServer);

Loading…
Cancel
Save