I need to return Exit Code -1 from Main if retries failed so (Kubernetes) handle it, I try throwing an exception (Task.FromException
) from ExecuteAsync
but the method signature will change and it is not allowed to change from Task
to Task<T>
. How can I raised a Environment.Exit(-1)
to the main thread? or cancellation to main thread so I can return an exit code -1 from there
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
int tries = 0;
while (!stoppingToken.IsCancellationRequested && tries <= _tryOuts)
{
try
{
...
tries++;
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
...
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
break;
}
else
{
_logger.LogError(response.Message, DateTimeOffset.Now);
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
}
_logger.LogDebug("Worker next iteration in milliseconds {0}", _timeBetweenTryOuts);
await Task.Delay(_timeBetweenTryOuts, stoppingToken);
}
_hostApplicationLifetime.StopApplication();
}
Interesting topic. I created PoC but in Asp .NET Core 2.1(I don't have now version 3), and I'm able to return other exit code using:
Environment.ExitCode
Documentation: https://learn.microsoft.com/en-us/dotnet/api/system.environment.exitcode?view=netcore-3.0
So, try do something like:
Environment.ExitCode = -1;
_hostApplicationLifetime.StopApplication();