使用事件监听意外停止
This commit is contained in:
parent
03fc545c6c
commit
a0fafb074d
23
FastGithub.Dns.DnscryptProxy/ControllState.cs
Normal file
23
FastGithub.Dns.DnscryptProxy/ControllState.cs
Normal file
@ -0,0 +1,23 @@
|
||||
namespace FastGithub.Dns.DnscryptProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// 服务控制状态
|
||||
/// </summary>
|
||||
enum ControllState
|
||||
{
|
||||
/// <summary>
|
||||
/// 未控制
|
||||
/// </summary>
|
||||
None,
|
||||
|
||||
/// <summary>
|
||||
/// 控制启动
|
||||
/// </summary>
|
||||
Started,
|
||||
|
||||
/// <summary>
|
||||
/// 控制停止
|
||||
/// </summary>
|
||||
Stopped,
|
||||
}
|
||||
}
|
||||
@ -38,6 +38,18 @@ namespace FastGithub.Dns.DnscryptProxy
|
||||
{
|
||||
await this.dnscryptProxyService.StartAsync(cancellationToken);
|
||||
this.logger.LogInformation($"{this.dnscryptProxyService}启动成功");
|
||||
|
||||
// 监听意外退出
|
||||
var service = this.dnscryptProxyService.Process;
|
||||
if (service == null)
|
||||
{
|
||||
this.OnProcessExit(null, new EventArgs());
|
||||
}
|
||||
else
|
||||
{
|
||||
service.EnableRaisingEvents = true;
|
||||
service.Exited += this.OnProcessExit;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -45,21 +57,18 @@ namespace FastGithub.Dns.DnscryptProxy
|
||||
}
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 后台监控
|
||||
///// </summary>
|
||||
///// <param name="stoppingToken"></param>
|
||||
///// <returns></returns>
|
||||
//protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
//{
|
||||
// await Task.Yield();
|
||||
|
||||
// this.dnscryptProxyService.Process?.WaitForExit();
|
||||
// if (this.isStopped == false)
|
||||
// {
|
||||
// this.logger.LogCritical($"{this.dnscryptProxyService}已停止运行,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。");
|
||||
// }
|
||||
//}
|
||||
/// <summary>
|
||||
/// 进程退出时
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnProcessExit(object? sender, EventArgs e)
|
||||
{
|
||||
if (this.dnscryptProxyService.ControllState != ControllState.Stopped)
|
||||
{
|
||||
this.logger.LogCritical($"{this.dnscryptProxyService}已停止运行,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 停止dnscrypt-proxy
|
||||
|
||||
@ -19,6 +19,11 @@ namespace FastGithub.Dns.DnscryptProxy
|
||||
/// </summary>
|
||||
public Process? Process { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取服务控制状态
|
||||
/// </summary>
|
||||
public ControllState ControllState { get; private set; } = ControllState.None;
|
||||
|
||||
/// <summary>
|
||||
/// DnscryptProxy服务
|
||||
/// </summary>
|
||||
@ -35,6 +40,8 @@ namespace FastGithub.Dns.DnscryptProxy
|
||||
/// <returns></returns>
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
this.ControllState = ControllState.Started;
|
||||
|
||||
var tomlPath = $"{name}.toml";
|
||||
await TomlUtil.SetListensAsync(tomlPath, this.fastGithubConfig.PureDns, cancellationToken);
|
||||
|
||||
@ -60,6 +67,8 @@ namespace FastGithub.Dns.DnscryptProxy
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
this.ControllState = ControllState.Stopped;
|
||||
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
StartDnscryptProxy("-service stop")?.WaitForExit();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user