Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Verify monitored process exit state before adding & emitting event #673

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions ProcessMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using NLog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand All @@ -20,6 +21,7 @@ internal class ProcessMonitor
{
private readonly Dictionary<string, MonitoredProcess> monitoredProcesses;
private readonly Timer monitorProcessTimer;
private readonly static Logger logger = LogManager.GetCurrentClassLogger();

static ProcessMonitor()
{
Expand Down Expand Up @@ -75,6 +77,7 @@ private void MonitorProcessTimer_Elapsed(object sender, ElapsedEventArgs e)
{
monitoredProcess.ProcessExited();
ProcessExited?.Invoke(monitoredProcess);
logger.Info($"Monitored process {monitoredProcess.ProcessName} (PID: {(monitoredProcess.Process?.Id.ToString() ?? "null")}) exited.");
}
}
else
Expand All @@ -91,11 +94,14 @@ private void MonitorProcessTimer_Elapsed(object sender, ElapsedEventArgs e)
foreach (var monitoredProcess in processesNeedingUpdate)
{
var process = processes.FirstOrDefault(p => string.Equals(p.ProcessName, monitoredProcess.ProcessName, StringComparison.OrdinalIgnoreCase));
if (process == null)

// We are also checking to see if the process is exiting before adding it, otherwise we'll keep adding it and then removing it constantly in an endless loop.
if (process == null || WinApi.HasProcessExited(process.Id))
continue;

monitoredProcess.ProcessStarted(process);
ProcessStarted?.Invoke(monitoredProcess);
logger.Info($"Monitored process {monitoredProcess.ProcessName} (PID: {process.Id}) started.");
}
}

Expand Down Expand Up @@ -129,6 +135,7 @@ public void AddProcess(Process process)
return;

monitoredProcesses.Add(processName, new MonitoredProcess(process));
logger.Debug($"Added process {processName} to process monitor.");
}

/// <summary>
Expand All @@ -142,6 +149,7 @@ public void AddProcess(string processName)
return;

monitoredProcesses.Add(processName, new MonitoredProcess(processName));
logger.Debug($"Added process {processName} to process monitor.");
}

/// <summary>
Expand All @@ -152,6 +160,7 @@ public void RemoveProcess(string processName)
{
processName = processName.ToLower();
monitoredProcesses.Remove(processName);
logger.Debug($"Removed process {processName} from process monitor.");
}
}

Expand All @@ -175,7 +184,6 @@ public MonitoredProcess(string processName)
public Process Process { get; private set; }
public string ProcessName { get; private set; }
public bool IsRunning { get; private set; }
public DateTime LastExitTime { get; private set; }

public bool HasName(string processName)
{
Expand All @@ -187,16 +195,11 @@ public void ProcessExited()
IsRunning = false;
Process?.Dispose();
Process = null;
LastExitTime = DateTime.Now;
}

public void ProcessStarted(Process process)
{
// check last exit time to prevent status flapping
if (LastExitTime != DateTime.MinValue && DateTime.Now - LastExitTime < TimeSpan.FromSeconds(5))
return;

Process = process;
Process = process;
ProcessName = process.ProcessName.ToLower();
IsRunning = true;
}
Expand Down