-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from microsoft/fix/bot-service-timeout-issues
Improve how the bot service reports its starting status to windows
- Loading branch information
Showing
6 changed files
with
136 additions
and
42 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/BotService/Infrastructure/WindowsService/HostService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
using System.Runtime.InteropServices; | ||
using BotService.Infrastructure.Extensions; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.WindowsServices; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace BotService.Infrastructure.WindowsService | ||
{ | ||
public class HostService : WebHostService | ||
{ | ||
private readonly IWebHost _webHost; | ||
private readonly ILogger _logger; | ||
|
||
public HostService(IWebHost host) | ||
: base(host) | ||
{ | ||
_webHost = host; | ||
_logger = host.Services.GetRequiredService<ILogger<HostService>>(); | ||
} | ||
|
||
protected override void OnStarting(string[] args) | ||
{ | ||
_logger.LogInformation("Starting the bot service"); | ||
|
||
// At this point, all dependencies have been registered and the configuration was retrieved | ||
SetServiceAsStartPending(2); | ||
|
||
_webHost.SetupDatabase(); | ||
SetServiceAsStartPending(3); | ||
|
||
base.OnStarting(args); | ||
} | ||
|
||
protected override void OnStarted() | ||
{ | ||
// At this point, the ASP.NET host should be running and receiving requests | ||
SetServiceAsStartPending(4); | ||
|
||
_webHost.RegisterBotService(); | ||
SetServiceAsRunning(); | ||
|
||
_logger.LogInformation("The bot service completed the start up process."); | ||
|
||
base.OnStarted(); | ||
} | ||
|
||
protected override void OnStopping() | ||
{ | ||
_logger.LogInformation("Stopping the bot service."); | ||
_webHost.UnregisterBotService(); | ||
|
||
base.OnStopping(); | ||
} | ||
|
||
protected override void OnStopped() | ||
{ | ||
_logger.LogInformation("The bot service was stopped."); | ||
|
||
base.OnStopped(); | ||
} | ||
|
||
[DllImport("advapi32.dll", SetLastError = true)] | ||
private static extern bool SetServiceStatus(System.IntPtr handle, ref WindowsServiceStatus serviceStatus); | ||
|
||
private void SetServiceAsStartPending(int? progress = null) | ||
{ | ||
var serviceStatus = default(WindowsServiceStatus); | ||
serviceStatus.dwCurrentState = WindowsServiceState.SERVICE_START_PENDING; | ||
serviceStatus.dwWaitHint = 100000; // 100 seconds | ||
|
||
if (progress.HasValue) | ||
{ | ||
// The number itself doesn't have any meaning, but it needs to be increased in each call to this method | ||
// to inform Windows that the service is making progress and didn't hang-up. | ||
serviceStatus.dwCheckPoint = progress.Value; | ||
} | ||
|
||
SetServiceStatus(ServiceHandle, ref serviceStatus); | ||
} | ||
|
||
private void SetServiceAsRunning() | ||
{ | ||
var serviceStatus = default(WindowsServiceStatus); | ||
serviceStatus.dwCurrentState = WindowsServiceState.SERVICE_RUNNING; | ||
SetServiceStatus(ServiceHandle, ref serviceStatus); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/BotService/Infrastructure/WindowsService/WindowsServiceState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
namespace BotService.Infrastructure.Extensions | ||
{ | ||
public enum WindowsServiceState | ||
{ | ||
SERVICE_STOPPED = 0x00000001, | ||
SERVICE_START_PENDING = 0x00000002, | ||
SERVICE_STOP_PENDING = 0x00000003, | ||
SERVICE_RUNNING = 0x00000004, | ||
SERVICE_CONTINUE_PENDING = 0x00000005, | ||
SERVICE_PAUSE_PENDING = 0x00000006, | ||
SERVICE_PAUSED = 0x00000007, | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/BotService/Infrastructure/WindowsService/WindowsServiceStatus.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
using System.Runtime.InteropServices; | ||
|
||
#pragma warning disable SA1307 // We cannot change the name of the struct fields to match our naming conventions | ||
namespace BotService.Infrastructure.Extensions | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
public struct WindowsServiceStatus | ||
{ | ||
public int dwServiceType; | ||
public WindowsServiceState dwCurrentState; | ||
public int dwControlsAccepted; | ||
public int dwWin32ExitCode; | ||
public int dwServiceSpecificExitCode; | ||
public int dwCheckPoint; | ||
public int dwWaitHint; | ||
} | ||
} | ||
#pragma warning restore SA1307 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters