Skip to content

Commit

Permalink
Added log rotation and additional features
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Nov 27, 2024
1 parent 40d5bbc commit 1761077
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 148 deletions.
15 changes: 15 additions & 0 deletions src/PodeMonitor/PodeLogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

namespace PodeMonitor
{
/// <summary>
/// Enum representing the various log levels for PodeMonitorLogger.
/// </summary>
public enum PodeLogLevel
{
DEBUG, // Detailed information for debugging purposes
INFO, // General operational information
WARN, // Warning messages for potential issues
ERROR, // Error messages for failures
CRITICAL // Critical errors indicating severe failures
}
}
62 changes: 30 additions & 32 deletions src/PodeMonitor/PodeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public class PodeMonitor
public bool DisableTermination { get => _disableTermination; }




/// <summary>
/// Initializes a new instance of the <see cref="PodeMonitor"/> class with the specified configuration options.
/// </summary>
Expand All @@ -73,7 +71,7 @@ public PodeMonitor(PodeMonitorWorkerOptions options)

// Generate a unique pipe name
_pipeName = PipeNameGenerator.GeneratePipeName();
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Initialized PodeMonitor with pipe name: {_pipeName}");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Initialized PodeMonitor with pipe name: {_pipeName}");
// Define the state file path only for Linux/macOS
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
Expand All @@ -93,7 +91,7 @@ public PodeMonitor(PodeMonitorWorkerOptions options)
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId,
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId,
$"Failed to create state directory at {stateDirectory}: {ex.Message}");
throw;
}
Expand All @@ -102,7 +100,7 @@ public PodeMonitor(PodeMonitorWorkerOptions options)
_stateFilePath = Path.Combine(stateDirectory, $"{Environment.ProcessId}.state");


PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Initialized PodeMonitor with pipe name: {_pipeName} and state file: {_stateFilePath}");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Initialized PodeMonitor with pipe name: {_pipeName} and state file: {_stateFilePath}");
}
}

Expand All @@ -117,7 +115,7 @@ public void StartPowerShellProcess()
{
if ((DateTime.Now - _lastLogTime).TotalMinutes >= 5)
{
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process is Alive.");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process is Alive.");
_lastLogTime = DateTime.Now;
}
return;
Expand All @@ -144,15 +142,15 @@ public void StartPowerShellProcess()
{
if (!string.IsNullOrEmpty(args.Data))
{
PodeMonitorLogger.Log(LogLevel.INFO, "Pode", _powerShellProcess.Id, args.Data);
PodeMonitorLogger.Log(PodeLogLevel.INFO, "Pode", _powerShellProcess.Id, args.Data);
ParseServiceState(args.Data);
}
};

// Subscribe to the error stream for logging errors
_powerShellProcess.ErrorDataReceived += (sender, args) =>
{
PodeMonitorLogger.Log(LogLevel.ERROR, "Pode", _powerShellProcess.Id, args.Data);
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "Pode", _powerShellProcess.Id, args.Data);
};

// Start the process and begin reading the output/error streams
Expand All @@ -161,12 +159,12 @@ public void StartPowerShellProcess()
_powerShellProcess.BeginErrorReadLine();

_lastLogTime = DateTime.Now;
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process started successfully.");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process started successfully.");
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Failed to start Pode process: {ex.Message}");
PodeMonitorLogger.Log(LogLevel.DEBUG, ex);
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Failed to start Pode process: {ex.Message}");
PodeMonitorLogger.Log(PodeLogLevel.DEBUG, ex);
}
}
}
Expand All @@ -180,7 +178,7 @@ public void StopPowerShellProcess()
{
if (_powerShellProcess == null || (_powerShellProcess.HasExited && Process.GetProcessById(_powerShellProcess.Id) == null))
{
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process is not running.");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process is not running.");
return;
}

Expand All @@ -189,24 +187,24 @@ public void StopPowerShellProcess()
if (InitializePipeClientWithRetry())
{
SendPipeMessage("shutdown");
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Waiting for {_shutdownWaitTimeMs} milliseconds for Pode process to exit...");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Waiting for {_shutdownWaitTimeMs} milliseconds for Pode process to exit...");
WaitForProcessExit(_shutdownWaitTimeMs);

if (_powerShellProcess != null && !_powerShellProcess.HasExited )
if (_powerShellProcess != null && !_powerShellProcess.HasExited)
{
PodeMonitorLogger.Log(LogLevel.WARN, "PodeMonitor", Environment.ProcessId, $"Pode process has exited:{_powerShellProcess.HasExited} Id:{_powerShellProcess.Id}");
PodeMonitorLogger.Log(PodeLogLevel.WARN, "PodeMonitor", Environment.ProcessId, $"Pode process has exited:{_powerShellProcess.HasExited} Id:{_powerShellProcess.Id}");

PodeMonitorLogger.Log(LogLevel.WARN, "PodeMonitor", Environment.ProcessId, "Pode process did not terminate gracefully. Killing process.");
PodeMonitorLogger.Log(PodeLogLevel.WARN, "PodeMonitor", Environment.ProcessId, "Pode process did not terminate gracefully. Killing process.");
_powerShellProcess.Kill();
}

PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process stopped successfully.");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, "Pode process stopped successfully.");
}
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Error stopping Pode process: {ex.Message}");
PodeMonitorLogger.Log(LogLevel.DEBUG, ex);
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Error stopping Pode process: {ex.Message}");
PodeMonitorLogger.Log(PodeLogLevel.DEBUG, ex);
}
finally
{
Expand Down Expand Up @@ -245,13 +243,13 @@ private void ExecutePipeCommand(string command)
if (InitializePipeClientWithRetry())
{
SendPipeMessage(command);
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"{command.ToUpper()} command sent to Pode process.");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"{command.ToUpper()} command sent to Pode process.");
}
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Error executing {command} command: {ex.Message}");
PodeMonitorLogger.Log(LogLevel.DEBUG, ex);
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Error executing {command} command: {ex.Message}");
PodeMonitorLogger.Log(PodeLogLevel.DEBUG, ex);
}
finally
{
Expand Down Expand Up @@ -287,7 +285,7 @@ private void ParseServiceState(string output)
UpdateServiceState(ServiceState.Stopping);
break;
default:
PodeMonitorLogger.Log(LogLevel.WARN, "PodeMonitor", Environment.ProcessId, $"Unknown service state: {state}");
PodeMonitorLogger.Log(PodeLogLevel.WARN, "PodeMonitor", Environment.ProcessId, $"Unknown service state: {state}");
UpdateServiceState(ServiceState.Unknown);
break;
}
Expand All @@ -301,7 +299,7 @@ private void ParseServiceState(string output)
private void UpdateServiceState(ServiceState state)
{
_state = state;
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Service state updated to: {state}");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Service state updated to: {state}");
// Write the state to the state file only on Linux/macOS
if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
Expand Down Expand Up @@ -340,15 +338,15 @@ private bool InitializePipeClientWithRetry(int maxRetries = 3)

if (!_pipeClient.IsConnected)
{
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Connecting to pipe server (Attempt {attempts + 1})...");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Connecting to pipe server (Attempt {attempts + 1})...");
_pipeClient.Connect(10000); // Timeout of 10 seconds
}

return _pipeClient.IsConnected;
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Pipe connection attempt {attempts + 1} failed: {ex.Message}");
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Pipe connection attempt {attempts + 1} failed: {ex.Message}");
}

attempts++;
Expand All @@ -371,8 +369,8 @@ private void SendPipeMessage(string message)
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Error sending message to pipe: {ex.Message}");
PodeMonitorLogger.Log(LogLevel.DEBUG, ex);
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Error sending message to pipe: {ex.Message}");
PodeMonitorLogger.Log(PodeLogLevel.DEBUG, ex);
}
}

Expand Down Expand Up @@ -402,11 +400,11 @@ private void WriteServiceStateToFile(ServiceState state)
try
{
File.WriteAllText(_stateFilePath, state.ToString().ToLowerInvariant());
PodeMonitorLogger.Log(LogLevel.DEBUG, "PodeMonitor", Environment.ProcessId, $"Service state written to file: {_stateFilePath}");
PodeMonitorLogger.Log(PodeLogLevel.DEBUG, "PodeMonitor", Environment.ProcessId, $"Service state written to file: {_stateFilePath}");
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Failed to write service state to file: {ex.Message}");
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Failed to write service state to file: {ex.Message}");
}
}
}
Expand All @@ -423,12 +421,12 @@ private void DeleteServiceStateFile()
if (File.Exists(_stateFilePath))
{
File.Delete(_stateFilePath);
PodeMonitorLogger.Log(LogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Service state file deleted: {_stateFilePath}");
PodeMonitorLogger.Log(PodeLogLevel.INFO, "PodeMonitor", Environment.ProcessId, $"Service state file deleted: {_stateFilePath}");
}
}
catch (Exception ex)
{
PodeMonitorLogger.Log(LogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Failed to delete service state file: {ex.Message}");
PodeMonitorLogger.Log(PodeLogLevel.ERROR, "PodeMonitor", Environment.ProcessId, $"Failed to delete service state file: {ex.Message}");
}
}
}
Expand Down
Loading

0 comments on commit 1761077

Please sign in to comment.