Skip to content

Commit

Permalink
added configuration for start and stop timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado authored and phatboyg committed Feb 20, 2018
1 parent 84340ff commit d1a1897
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Topshelf.Extensions.Configuration.Tests/Configuration_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,52 @@ public void Should_create_a_service_that_runs_as_network_service()
Assert.AreEqual("", installHost.InstallSettings.Credentials.Password);
}

[Test]
public void Should_create_a_service_that_has_start_timeout()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "topshelf:StartTimeout", "123" },
})
.Build()
.GetSection("topshelf");

Host host = HostFactory.New(x =>
{
x.Service<MyService>();
x.ApplyConfiguration(configuration);
x.ApplyCommandLine("install");
});

Assert.IsInstanceOf<InstallHost>(host);
var installHost = (InstallHost)host;
Assert.AreEqual(TimeSpan.FromSeconds(123), installHost.InstallSettings.StartTimeOut);
}

[Test]
public void Should_create_a_service_that_has_stopt_timeout()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "topshelf:StopTimeout", "123" },
})
.Build()
.GetSection("topshelf");

Host host = HostFactory.New(x =>
{
x.Service<MyService>();
x.ApplyConfiguration(configuration);
x.ApplyCommandLine("install");
});

Assert.IsInstanceOf<InstallHost>(host);
var installHost = (InstallHost)host;
Assert.AreEqual(TimeSpan.FromSeconds(123), installHost.InstallSettings.StopTimeOut);
}

[Test]
public void Should_create_a_service_with_recovery_options()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public static IEnumerable<Option> Parse(this IConfigurationSection configuration
{
options.Add(new InstanceOption(entry.Value));
}
else if (string.Equals(entry.Key, "StartTimeout", StringComparison.OrdinalIgnoreCase))
{
options.Add(new StartTimeoutOption(configuration.GetSection("StartTimeout").Get<int>()));
}
else if (string.Equals(entry.Key, "StopTimeout", StringComparison.OrdinalIgnoreCase))
{
options.Add(new StopTimeoutOption(configuration.GetSection("StopTimeout").Get<int>()));
}
#if !NETCORE
else if (string.Equals(entry.Key, "StartMode", StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2007-2014 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace Topshelf.Options
{
using Topshelf.HostConfigurators;

/// <summary>
/// Represents an option to set a service start timeout (in seconds).
/// </summary>
/// <seealso cref="Option" />
internal class StartTimeoutOption
: Option
{
/// <summary>
/// The start timeout (in seconds).
/// </summary>
private readonly int startTimeout;

/// <summary>
/// Initializes a new instance of the <see cref="StartTimeoutOption"/> class.
/// </summary>
/// <param name="startTimeout">The start timeout (in seconds).</param>
public StartTimeoutOption(int startTimeout)
{
this.startTimeout = startTimeout;
}

/// <summary>
/// Applies the option to the specified host configurator.
/// </summary>
/// <param name="configurator">The host configurator.</param>
public void ApplyTo(HostConfigurator configurator)
{
configurator.SetStartTimeout(System.TimeSpan.FromSeconds(this.startTimeout));
}
}
}
47 changes: 47 additions & 0 deletions src/Topshelf.Extensions.Configuration/Options/StopTimeoutOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2007-2014 Chris Patterson, Dru Sellers, Travis Smith, et. al.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
namespace Topshelf.Options
{
using Topshelf.HostConfigurators;

/// <summary>
/// Represents an option to set a service stop timeout (in seconds).
/// </summary>
/// <seealso cref="Option" />
internal class StopTimeoutOption
: Option
{
/// <summary>
/// The stop timeout (in seconds).
/// </summary>
private readonly int stopTimeoout;

/// <summary>
/// Initializes a new instance of the <see cref="StopTimeoutOption"/> class.
/// </summary>
/// <param name="stopTimeoout">The stop timeout (in seconds).</param>
public StopTimeoutOption(int stopTimeoout)
{
this.stopTimeoout = stopTimeoout;
}

/// <summary>
/// Applies the option to the specified host configurator.
/// </summary>
/// <param name="configurator">The host configurator.</param>
public void ApplyTo(HostConfigurator configurator)
{
configurator.SetStopTimeout(System.TimeSpan.FromSeconds(this.stopTimeoout));
}
}
}

0 comments on commit d1a1897

Please sign in to comment.