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