SelfKeeper 1.1.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SelfKeeper --version 1.1.0
NuGet\Install-Package SelfKeeper -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SelfKeeper" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SelfKeeper --version 1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SelfKeeper, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SelfKeeper as a Cake Addin #addin nuget:?package=SelfKeeper&version=1.1.0 // Install SelfKeeper as a Cake Tool #tool nuget:?package=SelfKeeper&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SelfKeeper
1. Intro
A library that lets applications run as their own watchdog.
允许应用程序作为自己的看门狗运行的库。
- 支持部分信号传递到工作进程,可以在某些情况下执行优雅关闭;
- 原始的控制台输出颜色会被保留;
工作原理
通过命令行参数判断当前是否是工作进程,如果不是,则复制当前进程的启动设置,添加相关命令行参数后启动工作进程,并监控其运行状态。
关键词
- 工作进程(Worker Process): 运行程序实际逻辑的进程
- 主进程(Host Process): 启动并监控工作进程的进程
2. 如何使用
2.1 安装Nuget
包
dotnet add package SelfKeeper --prerelease
2.2 启用SelfKeeper
在程序入口处添加处理代码
KeepSelf.Handle(args);
或进行更多配置
KeepSelf.Handle(args, options =>
{
options.RemoveFlag(KeepSelfFeatureFlag.SkipWhenDebuggerAttached | KeepSelfFeatureFlag.DisableForceKillByHost); //配置功能
options.StartFailRetryDelay = TimeSpan.FromSeconds(1); //配置启动失败的重试延时
options.RestartDelay = TimeSpan.FromSeconds(1); //进程退出后的重启延时
options.Logger = null; //配置日志记录器
options.WorkerProcessOptionsCommandArgumentName = "--worker-process-options"; //自定义工作进程选项的参数名
options.NoKeepSelfCommandArgumentName = "--no-keep-self"; //自定义不启用 KeepSelf 的参数名
options.NoKeepSelfEnvironmentVariableName = "NoWatchDog"; //自定义不启用 KeepSelf 的环境变量名
options.ExcludeRestartExitCodes.Add(0); //添加工作进程执行成功的退出码,当工作进程退出码在该列表内时,不再进行重启
});
- 主进程会在
KeepSelf.Handle
阻塞,启动并监控工作进程; - 主进程在退出时会在
KeepSelf.Handle
内调用Environment.Exit
退出,不会执行后面的代码;
options flags
默认配置下开启了所有flag
ExitWhenHostExited
: 当主进程退出时,工作进程也退出SkipWhenDebuggerAttached
: 已附加调试器时不进行处理ForceGCBeforeRunKeepService
: 在运行保持服务前,触发强制GCForceGCAfterWorkerProcessExited
: 工作进程退出时,触发强制GCDisableForceKillByHost
: 禁用主进程强制关闭功能
2.3 环境相关操作
获取环境信息
SelfKeeperEnvironment.IsWorkerProcess
: 当前进程是否为工作进程SelfKeeperEnvironment.ParentProcessId
: 父进程ID,当前为工作进程时才会有具体的值SelfKeeperEnvironment.SessionId
: SessionId,当前为工作进程时才会有具体的值
请求工作进程强制终止当前工作进程
SelfKeeperEnvironment.RequestKillCurrentProcess();
请求主进程关闭当前进程(仅当当前进程为工作进程,且未开启 DisableForceKillByHost 时有效),主进程将会调用 Process.Kill(entireProcessTree: true)
结束当前进程
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.1.1 | 23,810 | 1/7/2024 |
1.1.0 | 28,094 | 2/4/2023 |
1.0.0 | 7,185 | 7/3/2022 |
1.0.0-beta-005 | 244 | 5/13/2022 |
1.0.0-beta-004 | 169 | 5/11/2022 |
1.0.0-beta-003 | 175 | 5/9/2022 |
1.0.0-beta-002 | 169 | 5/8/2022 |
1.0.0-beta-001 | 190 | 5/6/2022 |