Skip to content

Commit

Permalink
Renamed to MoryxHost instead of Service.
Browse files Browse the repository at this point in the history
Add Platform check, so that it does not crash on other platforms.
Add StateChanged event to enable other servicees to plug into the moryx lifecycle via the host.
  • Loading branch information
andreniggemann committed Sep 3, 2024
1 parent 7d64c35 commit ff72cbd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static void AddMoryxModules(this IServiceCollection serviceCollection)
/// </summary>
public static void AddMoryxService(this IServiceCollection serviceCollection)
{
serviceCollection.AddHostedService<MoryxService>();
serviceCollection.AddHostedService<MoryxHost>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
using Castle.Core.Logging;
using Microsoft.Extensions.Hosting;
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
internal class MoryxHost : BackgroundService
{
private readonly IModuleManager moduleManager;
private readonly IHost lifeTime;
private readonly ILogger<MoryxService> logger;
private readonly ILogger<MoryxHost> 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")]
Expand Down Expand Up @@ -54,18 +51,39 @@ private bool ConsoleCtrlCheck(CtrlTypes ctrlType)
return true;
}

public MoryxService(IModuleManager moduleManager, IHost lifetTime, ILogger<MoryxService> logger)
public MoryxHost(IModuleManager moduleManager, IHost lifetTime, ILogger<MoryxHost> logger)
{
this.moduleManager = moduleManager;
this.lifeTime = lifetTime;
this.logger = logger;
}
public enum MoryxHostState {
NotStarted,
Starting,
Started,
Stopping,
Stopped
}

public MoryxHostState State {get;private set;}
public event EventHandler<MoryxHostState> StateChanged;


public override async Task StartAsync(CancellationToken cancellationToken)
{
SetConsoleCtrlHandler(ConsoleCtrlCheck, true);
State = MoryxHostState.Starting;
StateChanged?.Invoke(this, State);
// Only register on windows, because the behavior is os specific
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
SetConsoleCtrlHandler(ConsoleCtrlCheck, true);
}
moduleManager.StartModules();

await base.StartAsync(cancellationToken);

State = MoryxHostState.Started;
StateChanged?.Invoke(this, State);
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Expand All @@ -74,8 +92,12 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)

public override async Task StopAsync(CancellationToken cancellationToken)
{
State = MoryxHostState.Stopping;
StateChanged?.Invoke(this, State);
moduleManager.StopModules();
await base.StopAsync(cancellationToken);
State = MoryxHostState.Stopped;
StateChanged?.Invoke(this, State);
}
}
}

0 comments on commit ff72cbd

Please sign in to comment.