Fixed multi instance detection and improved logging

This commit is contained in:
Casper Sparre 2026-02-28 13:18:51 +01:00
parent c43cdc2ef1
commit ac0aa8c645
3 changed files with 58 additions and 13 deletions

View File

@ -0,0 +1,43 @@
using System.Diagnostics;
namespace OF_DL.Extensions;
internal static class LoggingExtensions
{
extension(Log)
{
public static ILogger ForProcessContext(Process process)
=> Log.Logger.ForProcessContext(process);
public static ILogger ForProcessContext(string propertyName, Process process)
=> Log.Logger.ForProcessContext(propertyName, process);
public static ILogger ForProcessContext(Process[] processes)
=> Log.Logger.ForProcessContext(processes);
}
extension(ILogger logger)
{
public ILogger ForProcessContext(Process process)
=> logger.ForProcessContext($"Process_{process.Id}", process);
public ILogger ForProcessContext(string propertyName, Process process)
=> logger.ForContext(propertyName, process.GenerateContext(), destructureObjects: true);
public ILogger ForProcessContext(Process[] processes)
{
ILogger l = logger;
foreach (Process p in processes)
l = l.ForProcessContext(p);
return l;
}
}
extension(Process p)
{
public object GenerateContext()
=> new { p.Id, p.ProcessName };
}
}

View File

@ -1,5 +1,4 @@
using System.Diagnostics; using System.Diagnostics;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red)); AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red));
@ -10,7 +9,8 @@ static async Task RunAsync(string[] args)
ServiceCollection services = await ConfigureServices(args); ServiceCollection services = await ConfigureServices(args);
ServiceProvider serviceProvider = services.BuildServiceProvider(); ServiceProvider serviceProvider = services.BuildServiceProvider();
ExitIfOtherProcess(serviceProvider); if (IsAlreadyRunning(serviceProvider))
return;
Worker worker = serviceProvider.GetRequiredService<Worker>(); Worker worker = serviceProvider.GetRequiredService<Worker>();
await worker.RunAsync(); await worker.RunAsync();
@ -162,23 +162,24 @@ static CajetanConfig ParseCommandlineArgs(string[] args, Config currentConfig)
} }
} }
static void ExitIfOtherProcess(ServiceProvider serviceProvider) static bool IsAlreadyRunning(ServiceProvider serviceProvider)
{ {
Assembly? entryAssembly = Assembly.GetEntryAssembly();
AssemblyName? entryAssemblyName = entryAssembly?.GetName();
if (entryAssemblyName?.Name is null)
return;
Process thisProcess = Process.GetCurrentProcess(); Process thisProcess = Process.GetCurrentProcess();
Process[] otherProcesses = [.. Process.GetProcessesByName(entryAssemblyName.Name).Where(p => p.Id != thisProcess.Id)]; Process[] otherProcesses = [.. Process.GetProcessesByName(thisProcess.ProcessName).Where(p => p.Id != thisProcess.Id)];
if (otherProcesses.Length <= 0) if (otherProcesses.Length <= 0)
return; {
Log.ForContext("AllProcesses", Process.GetProcesses().OrderBy(p => p.Id).Select(p => $"{p.Id}: {p.ProcessName}"))
.Information("All other processes found..");
return false;
}
AnsiConsole.Markup($"[green]Other OF DL process detected, exiting..\n[/]"); AnsiConsole.Markup($"[green]Other OF DL process detected, exiting..\n[/]");
Log.Warning("Other OF DL process detected, exiting..");
Log.ForProcessContext("ThisProcess", thisProcess)
.ForProcessContext(otherProcesses)
.Warning("Other OF DL process detected, exiting..");
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(0); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(0);
return true;
} }

View File

@ -3,6 +3,7 @@ global using OF_DL;
global using OF_DL.CLI; global using OF_DL.CLI;
global using OF_DL.Enumerations; global using OF_DL.Enumerations;
global using OF_DL.Exceptions; global using OF_DL.Exceptions;
global using OF_DL.Extensions;
global using OF_DL.Helpers; global using OF_DL.Helpers;
global using OF_DL.Models; global using OF_DL.Models;
global using OF_DL.Models.Config; global using OF_DL.Models.Config;