Skip to content

Commit

Permalink
Updated worker signature
Browse files Browse the repository at this point in the history
  • Loading branch information
jakenuts committed Aug 19, 2024
1 parent 1252dc1 commit ed182ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,15 @@ namespace Community.Extensions.Spectre.Cli.Hosting;
/// <summary>
/// A background service that runs the Spectre Console App
/// </summary>
public class SpectreConsoleWorker : BackgroundService
public class SpectreConsoleWorker(
ILogger<SpectreConsoleWorker> logger,
ICommandApp commandApp,
IHostApplicationLifetime hostLifetime) : BackgroundService
{
private readonly ICommandApp _commandApp;

private readonly IHostApplicationLifetime _hostLifetime;

private readonly ILogger<SpectreConsoleWorker> _logger;

private int _exitCode;

/// <summary>
/// The exit code returned by the Spectre Console App
/// </summary>
/// <param name="logger"></param>
/// <param name="commandApp"></param>
/// <param name="hostLifetime"></param>
public SpectreConsoleWorker(ILogger<SpectreConsoleWorker> logger, ICommandApp commandApp,
IHostApplicationLifetime hostLifetime)
{
_logger = logger;
_commandApp = commandApp;
_hostLifetime = hostLifetime;

logger.LogDebug("Constructed");
}
private int _exitCode;

/// <summary>
/// This method is called when the <see cref="T:Microsoft.Extensions.Hosting.IHostedService" /> starts. The
Expand All @@ -49,26 +34,26 @@ public SpectreConsoleWorker(ILogger<SpectreConsoleWorker> logger, ICommandApp co
/// </remarks>
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogDebug("🧵 Console Background Worker Started");
logger.LogDebug("🧵 Console Background Worker Started");

try
{
var args = Environment.GetCommandLineArgs().Skip(1).ToArray();

_logger.LogTrace("_commandApp.RunAsync(args)");
logger.LogTrace("_commandApp.RunAsync(args)");

_exitCode = await _commandApp.RunAsync(args);
_exitCode = await commandApp.RunAsync(args);
}
catch (Exception ex)
{
_logger.LogError(ex, "An unexpected error occurred");
logger.LogError(ex, "An unexpected error occurred");
_exitCode = 1;
}
finally
{
_logger.LogDebug("🛑 Console Background Worker Stopping");
logger.LogDebug("🛑 Console Background Worker Stopping");
Environment.ExitCode = _exitCode;
_hostLifetime.StopApplication();
hostLifetime.StopApplication();
}
}
}
5 changes: 1 addition & 4 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<Project>

<PropertyGroup>
</PropertyGroup>

<PropertyGroup Label="Settings">
<Deterministic>true</Deterministic>
<LangVersion>preview</LangVersion>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
<MinVerMinimumMajorMinor>1.0</MinVerMinimumMajorMinor>
<MinVerMinimumMajorMinor>1.1</MinVerMinimumMajorMinor>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down

0 comments on commit ed182ba

Please sign in to comment.