首页 > 解决方案 > 在 Docker linux 容器中运行的 BackgroundService 类不会正常关闭

问题描述

我创建了一个从 Microsoft.Extensions.Hosting.BackgroundService 继承的工作服务,然后我通过 Visual Studio 调试器将其部署到我的 Windows 机器上的 docker linux 容器。我在cancellationtoken.IsCancellationRequested 为真时发生的代码上设置了一个断点。然后我向容器发出“docker stop --time = 30”,断点永远不会命中,30秒后调试器强制停止。

我还尝试覆盖 StopAsync 方法并在其中放置一个断点,并且也不会被调用。我正在运行 .net core 3,最新版本的 docker 桌面。我已经确认 StartAsync 被调用。

这是我的程序文件。

 public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }


        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                        .AddEnvironmentVariables().Build();
                })
                .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>();    });

    }

如果有人知道我错过了什么,或者尊重停止的非网络服务器服务的工作示例,我将非常感激。

添加我的工人的样子:

using System;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace MyWorker
{
    public class Worker : BackgroundService
    {    
        public Worker(ILogger<Worker> logger, IConfiguration configs)
        {

        }

        public override Task StopAsync(CancellationToken cancellationToken)
        {
           return base.StopAsync(cancellationToken);   
        }

        public override Task StartAsync(CancellationToken cancellationToken)
        { 
            return base.StartAsync(cancellationToken);
        }


        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            try
            {
                await DoWork(cancellationToken); //While loop that checks token in here
            }

            catch (Exception ex)
            {

                throw;
            }
        }
    }
}

标签: c#docker.net-core-3.0

解决方案


要收听 Ctrl+C、SIGINT 和 SIGTERM,您需要通过UseConsoleLifetime()RunConsoleAsync添加控制台生命周期支持,例如:

public static async Task Main(string[] args)
{
    CreateHostBuilder(args).Build().RunConsoleAsync();
}

或者

public static void Main(string[] args)
{
    CreateHostBuilder(args).UseConsoleLifeTime().Build().Run();
}

如果关闭时间超过ShutdownTimeout中设置的超时时间,则会记录警告。

这个怎么运作

了解控制台生命周期的工作原理有助于解决延迟问题。

如果您检查源代码,RunConsoleAsync只不过是调用UseConsoleLifeTime().Build().RunAsync();

内部ConsoleLifetime类为和事件添加事件侦听器:CancelProcessExit

AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
Console.CancelKeyPress += OnCancelKeyPress;

Ctrl+C只会将停止请求转发给主机

private void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
    e.Cancel = true;
    ApplicationLifetime.StopApplication();
}

ProcessExit另一方面,如果关闭时间超过HostOptions.ShutdownTimeout中指定的超时时间,则处理程序将发出警告:

private void OnProcessExit(object sender, EventArgs e)
{
    ApplicationLifetime.StopApplication();
    if(!_shutdownBlock.WaitOne(HostOptions.ShutdownTimeout))
    {
        Logger.LogInformation("Waiting for the host to be disposed. Ensure all 'IHost' instances are wrapped in 'using' blocks.");
    }
    _shutdownBlock.WaitOne();
    // On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
    // Suppress that since we shut down gracefully. https://github.com/aspnet/AspNetCore/issues/6526
    System.Environment.ExitCode = 0;
}

推荐阅读