Skip to content

Commit

Permalink
top level programs
Browse files Browse the repository at this point in the history
  • Loading branch information
damianh committed Jun 13, 2024
1 parent 6fade23 commit 76146c4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/NonTerminatingProcess/NonTerminatingProcess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<OutputPath>..\LittleForker.Tests\NonTerminatingProcess\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
Expand Down
148 changes: 61 additions & 87 deletions src/NonTerminatingProcess/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>("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<bool>("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<bool>("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<bool>("exit-with-non-zero", false);
if (_exitWithNonZero)
{
Log.Logger.Information("Will exit with non-zero exit code");
}
}
var parentPid = configRoot.GetValue<int?>("ParentProcessId");

private async Task<int> 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<int?>("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<int> 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();
}
10 changes: 1 addition & 9 deletions src/SelfTerminatingProcess/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
using System;

namespace SelfTerminatingProcess;

public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Done.");
}
}
Console.WriteLine("Done.");
2 changes: 1 addition & 1 deletion src/SelfTerminatingProcess/SelfTerminatingProcess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutputPath>..\LittleForker.Tests\SelfTerminatingProcess\</OutputPath>
</PropertyGroup>
Expand Down

0 comments on commit 76146c4

Please sign in to comment.