From 76146c4197d1afa6ba1d7b4f235d20d3cd4a75d6 Mon Sep 17 00:00:00 2001 From: Damian Hickey Date: Sun, 21 May 2023 11:04:46 +0200 Subject: [PATCH] top level programs --- .../NonTerminatingProcess.csproj | 2 +- src/NonTerminatingProcess/Program.cs | 148 ++++++++---------- src/SelfTerminatingProcess/Program.cs | 10 +- .../SelfTerminatingProcess.csproj | 2 +- 4 files changed, 64 insertions(+), 98 deletions(-) diff --git a/src/NonTerminatingProcess/NonTerminatingProcess.csproj b/src/NonTerminatingProcess/NonTerminatingProcess.csproj index 53882dd..8f4eef9 100644 --- a/src/NonTerminatingProcess/NonTerminatingProcess.csproj +++ b/src/NonTerminatingProcess/NonTerminatingProcess.csproj @@ -3,7 +3,7 @@ Exe net6.0 - latest + preview ..\LittleForker.Tests\NonTerminatingProcess\ false diff --git a/src/NonTerminatingProcess/Program.cs b/src/NonTerminatingProcess/Program.cs index 1686476..fbf7aa8 100644 --- a/src/NonTerminatingProcess/Program.cs +++ b/src/NonTerminatingProcess/Program.cs @@ -7,105 +7,79 @@ using Microsoft.Extensions.Logging.Abstractions; using Serilog; -namespace NonTerminatingProcess +var logger = new LoggerConfiguration() + .WriteTo.Console() + .CreateLogger(); +var shutdown = new CancellationTokenSource(TimeSpan.FromSeconds(100)); +var configRoot = new ConfigurationBuilder() + .AddCommandLine(args) + .AddEnvironmentVariables() + .Build(); + +// Running program with --debug=true will attach a debugger. +// Used to assist with debugging LittleForker. +if (configRoot.GetValue("debug", false)) { - internal sealed class Program - { - // Yeah this process is supposed to be "non-terminating" - // but we don't want tons of orphaned instances running - // because of tests so it terminates after a long - // enough time (100 seconds) - private readonly CancellationTokenSource _shutdown = new CancellationTokenSource(TimeSpan.FromSeconds(100)); - private readonly IConfigurationRoot _configRoot; - private readonly bool _ignoreShutdownSignal; - private readonly bool _exitWithNonZero; + Debugger.Launch(); +} - static Program() - { - Log.Logger = new LoggerConfiguration() - .WriteTo.Console() - .CreateLogger(); - } +var ignoreShutdownSignal = configRoot.GetValue("ignore-shutdown-signal", false); +if (ignoreShutdownSignal) +{ + Log.Logger.Information("Will ignore Shutdown Signal"); +} - private Program(string[] args) - { - _configRoot = new ConfigurationBuilder() - .AddCommandLine(args) - .AddEnvironmentVariables() - .Build(); +var exitWithNonZero = configRoot.GetValue("exit-with-non-zero", false); +if (exitWithNonZero) +{ + Log.Logger.Information("Will exit with non-zero exit code"); +} - // Running program with --debug=true will attach a debugger. - // Used to assist with debugging LittleForker. - if (_configRoot.GetValue("debug", false)) - { - Debugger.Launch(); - } - - _ignoreShutdownSignal = _configRoot.GetValue("ignore-shutdown-signal", false); - if (_ignoreShutdownSignal) - { - Log.Logger.Information("Will ignore Shutdown Signal"); - } +var pid = Process.GetCurrentProcess().Id; +Log.Logger.Information($"Long running process started. PID={pid}"); - _exitWithNonZero = _configRoot.GetValue("exit-with-non-zero", false); - if (_exitWithNonZero) - { - Log.Logger.Information("Will exit with non-zero exit code"); - } - } +var parentPid = configRoot.GetValue("ParentProcessId"); - private async Task Run() +using (parentPid.HasValue + ? new ProcessExitedHelper(parentPid.Value, _ => ParentExited(parentPid.Value), new NullLoggerFactory()) + : NoopDisposable.Instance) +{ + using (await CooperativeShutdown.Listen(ExitRequested, new NullLoggerFactory())) + { + // Poll the shutdown token in a tight loop + while (!shutdown.IsCancellationRequested || ignoreShutdownSignal) { - var pid = Process.GetCurrentProcess().Id; - Log.Logger.Information($"Long running process started. PID={pid}"); - - var parentPid = _configRoot.GetValue("ParentProcessId"); - - using (parentPid.HasValue - ? new ProcessExitedHelper(parentPid.Value, _ => ParentExited(parentPid.Value), new NullLoggerFactory()) - : NoopDisposable.Instance) - { - using (await CooperativeShutdown.Listen(ExitRequested, new NullLoggerFactory())) - { - // Poll the shutdown token in a tight loop - while(!_shutdown.IsCancellationRequested || _ignoreShutdownSignal) - { - await Task.Delay(100); - } - Log.Information("Exiting."); - } - } - - return _exitWithNonZero ? -1 : 0; + await Task.Delay(100); } + Log.Information("Exiting."); + } +} - static Task Main(string[] args) => new Program(args).Run(); +return exitWithNonZero ? -1 : 0; - private void ExitRequested() - { - Log.Logger.Information("Cooperative shutdown requested."); +void ExitRequested() +{ + Log.Logger.Information("Cooperative shutdown requested."); - if (_ignoreShutdownSignal) - { - Log.Logger.Information("Shut down signal ignored."); - return; - } + if (ignoreShutdownSignal) + { + Log.Logger.Information("Shut down signal ignored."); + return; + } - _shutdown.Cancel(); - } + shutdown.Cancel(); +} - private void ParentExited(int processId) - { - Log.Logger.Information($"Parent process {processId} exited."); - _shutdown.Cancel(); - } +void ParentExited(int processId) +{ + Log.Logger.Information($"Parent process {processId} exited."); + shutdown.Cancel(); +} - private class NoopDisposable : IDisposable - { - public void Dispose() - {} +class NoopDisposable : IDisposable +{ + public void Dispose() + { } - internal static readonly IDisposable Instance = new NoopDisposable(); - } - } -} + internal static readonly IDisposable Instance = new NoopDisposable(); +} \ No newline at end of file diff --git a/src/SelfTerminatingProcess/Program.cs b/src/SelfTerminatingProcess/Program.cs index 88cd600..9228d66 100644 --- a/src/SelfTerminatingProcess/Program.cs +++ b/src/SelfTerminatingProcess/Program.cs @@ -1,11 +1,3 @@ using System; -namespace SelfTerminatingProcess; - -public class Program -{ - static void Main(string[] args) - { - Console.WriteLine("Done."); - } -} \ No newline at end of file +Console.WriteLine("Done."); diff --git a/src/SelfTerminatingProcess/SelfTerminatingProcess.csproj b/src/SelfTerminatingProcess/SelfTerminatingProcess.csproj index bf72eac..bb0e294 100644 --- a/src/SelfTerminatingProcess/SelfTerminatingProcess.csproj +++ b/src/SelfTerminatingProcess/SelfTerminatingProcess.csproj @@ -3,7 +3,7 @@ Exe net6.0 - latest + preview false ..\LittleForker.Tests\SelfTerminatingProcess\