簡體   English   中英

在 Docker linux 容器中運行的 BackgroundService class 未正常關閉

[英]BackgroundService class running in Docker linux container does not shutdown gracefully

我創建了一個從 Microsoft.Extensions.Hosting.BackgroundService 繼承的工作服務,然后我通過 Visual Studio 調試器機器將其部署到 docker linux 容器。 我在cancellationtoken.IsCancellationRequested 為真時發生的代碼上設置了一個斷點。 然后我向容器發出“docker stop --time=30”,斷點永遠不會命中,30 秒后調試器強制停止。

我還嘗試覆蓋 StopAsync 方法並在其中放置一個斷點,並且也不會被調用。 我正在運行 .net 核心 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;
            }
        }
    }
}

要收聽 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 class 為CancelProcessExit事件添加事件偵聽器:

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

Ctrl+C 只會將停止請求轉發給主機

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

另一方面,如果關閉時間超過HostOptions.ShutdownTimeout中指定的超時時間, ProcessExit處理程序將發出警告:

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;
}

4天后我終於弄明白了,問題不在於代碼或docker,問題在於Visual Studio調試器。 如果您通過在 VS2019 中按播放來運行代碼,它將不遵守 SIGTERM。 如果您通過手動 docker 運行命令運行,您將看到您的代碼響應 DOCKER 停止命令。

最終可能有辦法讓它工作,但我還沒有找到。

為我增加關機超時工作。 感謝 Panagiotis 分享 MS Docs。

Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
    services.Configure<HostOptions>(option =>
    {
        option.ShutdownTimeout = System.TimeSpan.FromSeconds(20);
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM