Docker linux 容器中的 BackgroundService class 运行 未正常关闭
BackgroundService class running in Docker linux container does not shutdown gracefully
我创建了一个继承自 Microsoft.Extensions.Hosting.BackgroundService 的辅助服务,然后我通过 [=29= 将其部署到 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;
}
}
}
}
要收听 Ctrl+C、SIGINT 和 SIGTERM,您需要添加 Console Lifetime support, either through UseConsoleLifetime() or 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();
Cancel
和 ProcessExit
事件的内部 ConsoleLifetime class adds event listeners :
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
Console.CancelKeyPress += OnCancelKeyPress;
Ctrl+C will only forwart the Stop request to the host :
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 STOP 命令。
可能有一种方法可以让它最终起作用,但我还没有找到它。
为我增加关机超时工作。感谢 Panagiotis 分享 MS 文档。
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.Configure<HostOptions>(option =>
{
option.ShutdownTimeout = System.TimeSpan.FromSeconds(20);
});
});
我创建了一个继承自 Microsoft.Extensions.Hosting.BackgroundService 的辅助服务,然后我通过 [=29= 将其部署到 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;
}
}
}
}
要收听 Ctrl+C、SIGINT 和 SIGTERM,您需要添加 Console Lifetime support, either through UseConsoleLifetime() or 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();
Cancel
和 ProcessExit
事件的内部 ConsoleLifetime class adds event listeners :
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
Console.CancelKeyPress += OnCancelKeyPress;
Ctrl+C will only forwart the Stop request to the host :
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 STOP 命令。
可能有一种方法可以让它最终起作用,但我还没有找到它。
为我增加关机超时工作。感谢 Panagiotis 分享 MS 文档。
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.Configure<HostOptions>(option =>
{
option.ShutdownTimeout = System.TimeSpan.FromSeconds(20);
});
});