-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MoryxService to handle closing the console window.
Add extension method to KernelServiceCollectionExtensions to use the MoryxService. Add dependency to Microsoft.Extensions.Hosting to make background services available.
- Loading branch information
1 parent
684698a
commit 9b34863
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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,81 @@ | ||
using Castle.Core.Logging; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Moryx.Runtime.Modules; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Moryx.Runtime.Kernel | ||
{ | ||
internal class MoryxService : BackgroundService | ||
{ | ||
private readonly IModuleManager moduleManager; | ||
private readonly IHost lifeTime; | ||
private readonly ILogger<MoryxService> logger; | ||
|
||
// Console shutdown handling according to https://stackoverflow.com/questions/21751545/how-to-make-a-console-app-exit-gracefully-when-it-is-closed | ||
[DllImport("Kernel32")] | ||
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add); | ||
|
||
// A delegate type to be used as the handler routine | ||
// for SetConsoleCtrlHandler. | ||
public delegate bool HandlerRoutine(CtrlTypes CtrlType); | ||
|
||
// An enumerated type for the control messages | ||
// sent to the handler routine. | ||
public enum CtrlTypes | ||
{ | ||
CTRL_C_EVENT = 0, | ||
CTRL_BREAK_EVENT, | ||
CTRL_CLOSE_EVENT, | ||
CTRL_LOGOFF_EVENT = 5, | ||
CTRL_SHUTDOWN_EVENT | ||
} | ||
|
||
/// <summary> | ||
/// Callback that is called when a console event is raised. | ||
/// </summary> | ||
/// <param name="ctrlType"></param> | ||
/// <returns></returns> | ||
private bool ConsoleCtrlCheck(CtrlTypes ctrlType) | ||
{ | ||
// Stop the host and await shutdown. | ||
lifeTime.StopAsync(TimeSpan.FromSeconds(100)).ContinueWith(task => | ||
{ | ||
if (task.IsFaulted) | ||
{ | ||
logger.LogError(task.Exception, "Received failure on shutdown."); | ||
} | ||
}).Wait(); | ||
return true; | ||
} | ||
|
||
public MoryxService(IModuleManager moduleManager, IHost lifetTime, ILogger<MoryxService> logger) | ||
{ | ||
this.moduleManager = moduleManager; | ||
this.lifeTime = lifetTime; | ||
this.logger = logger; | ||
} | ||
|
||
public override async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
SetConsoleCtrlHandler(ConsoleCtrlCheck, true); | ||
moduleManager.StartModules(); | ||
await base.StartAsync(cancellationToken); | ||
} | ||
protected override Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public override async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
moduleManager.StopModules(); | ||
await base.StopAsync(cancellationToken); | ||
} | ||
} | ||
} |