Worker Service .NET8 how to gracefully shutdown as a docker container
I'm a junior developer just starting to work with Docker, and I have a question regarding graceful shutdown of the worker services.
I created a default worker service in Visual Studio, and my Worker class looks something like this:
After publishing the application as a Docker container, I noticed that when I run
docker stop
, it immediately aborts my async task inside await myAsyncWork();
or await mySecondAsyncWork();
.
Is there a way to ensure that when I run docker stop
, the application finishes the current async task and finishes the current iteration of while (!stoppingToken.IsCancellationRequested)
and then gracefully shuts down? And is there any best practise I should follow?4 Replies
Sounds like you're looking for IApplicationLifetime
Let me try to find a resource
Andrew Lock | .NET Escapades
Introducing IHostLifetime and untangling the Generic Host startup i...
In this post I introduce the new IHostLifetime interface and look at the interactions involved in the ASP.NET Core generic host startup and shutdown processes
Ah, right, it was renamed to IHostLifetime
I think the best ideal solution would be to design the app in a way which is not dependant on clean shutdown, which can't be guaranteed if there's some higher forces like power outage
Unknown User•3d ago
Message Not Public
Sign In & Join Server To View