Skip to content

Commit

Permalink
Update Guard
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Mar 4, 2021
1 parent b6e4e5e commit 74e1635
Showing 1 changed file with 61 additions and 30 deletions.
91 changes: 61 additions & 30 deletions Netch/Controllers/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public abstract class Guard
private FileStream? _logFileStream;

private StreamWriter? _logStreamWriter;
private bool _redirectToFile = true;

/// <summary>
/// 日志文件(重定向输出文件)
Expand Down Expand Up @@ -50,6 +51,12 @@ public abstract class Guard
/// </summary>
protected bool RedirectStd { get; set; } = true;

protected bool RedirectToFile
{
get => RedirectStd && _redirectToFile;
set => _redirectToFile = value;
}

/// <summary>
/// 进程实例
/// </summary>
Expand Down Expand Up @@ -123,40 +130,31 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority
State = State.Starting;
// 初始化程序
InitInstance(argument);
Instance!.EnableRaisingEvents = true;
if (RedirectStd)
{
// 清理日志
_logFileStream = File.Open(LogPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
_logStreamWriter = new StreamWriter(_logFileStream);
}

Instance.Exited += OnExited;
if (RedirectToFile)
OpenLogFile();

// 启动程序
Instance.Start();
Instance!.Start();
if (priority != ProcessPriorityClass.Normal)
Instance.PriorityClass = priority;

if (RedirectStd)
{
Task.Run(() => ReadOutput(Instance.StandardOutput));
Task.Run(() => ReadOutput(Instance.StandardError));

if (!StartedKeywords.Any())
{
State = State.Started;
return;
}
}
else
{
return;
}

// 启动日志重定向
_flushFileStreamTimer.Elapsed += FlushFileStreamTimerEvent;
_flushFileStreamTimer.Enabled = true;
if (!StartedKeywords.Any())
{
State = State.Started;
return;
}

// 等待启动
for (var i = 0; i < 1000; i++)
{
Expand All @@ -178,33 +176,68 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority
throw new MessageException($"{Name} 控制器启动超时");
}

protected virtual void OnKeywordStarted()
#region FileStream

private void OpenLogFile()
{
_logFileStream = File.Open(LogPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
_logStreamWriter = new StreamWriter(_logFileStream);

_flushFileStreamTimer.Elapsed += FlushFileStreamTimerEvent;
_flushFileStreamTimer.Enabled = true;
}

protected virtual void OnKeywordTimeout()
private void WriteLog(string line)
{
if (!RedirectToFile)
return;

_logStreamWriter!.WriteLine(line);
}

private void CloseLogFile()
{
if (!RedirectToFile)
return;

_flushFileStreamTimer.Enabled = false;
_logStreamWriter!.Close();
_logFileStream!.Close();
_logStreamWriter = _logStreamWriter = null;
}

#endregion

#region virtual

protected virtual void OnReadNewLine(string line)
{
}

protected virtual void OnKeywordStarted()
{
}

protected virtual void OnKeywordStopped()
{
Utils.Utils.Open(LogPath);
throw new MessageException($"{Name} 控制器启动失败");
}

private void OnExited(object sender, EventArgs e)
protected virtual void OnKeywordTimeout()
{
State = State.Stopped;
}

protected virtual void ReadOutput(TextReader reader)
#endregion

protected void ReadOutput(TextReader reader)
{
string? line;
while ((line = reader.ReadLine()) != null)
{
_logStreamWriter!.WriteLine(line);
OnReadNewLine(line);
WriteLog(line);

// 检查启动
// State == State.Started if !StartedKeywords.Any()
if (State == State.Starting)
{
if (StartedKeywords.Any(s => line.Contains(s)))
Expand All @@ -214,10 +247,8 @@ protected virtual void ReadOutput(TextReader reader)
}
}

_flushFileStreamTimer.Enabled = false;
_logStreamWriter!.Close();
_logFileStream!.Close();
_logStreamWriter = _logStreamWriter = null;
State = State.Stopped;
CloseLogFile();
}

/// <summary>
Expand Down

0 comments on commit 74e1635

Please sign in to comment.