Use helper for Environment.Exit to ensure logger is flushed.

This commit is contained in:
Casper Sparre 2026-02-17 22:44:57 +01:00
parent aee920a9f1
commit 41eea18d99
2 changed files with 37 additions and 12 deletions

View File

@ -0,0 +1,22 @@
using OF_DL.Services;
using Serilog;
namespace OF_DL.Helpers;
public class ExitHelper(IDownloadEventHandler eventHandler)
{
private readonly IDownloadEventHandler _eventHandler = eventHandler;
public void ExitWithCode(int exitCode)
{
Console.WriteLine();
_eventHandler?.OnMessage("Exiting run with Code '{exitCode}'..");
Log.CloseAndFlush();
Log.CloseAndFlush();
Task.Delay(5000).GetAwaiter().GetResult();
Environment.Exit(exitCode);
}
}

View File

@ -9,6 +9,7 @@ using OF_DL.Models.Entities.Users;
using OF_DL.Services;
using Serilog;
using Spectre.Console;
using OF_DL.Helpers;
namespace OF_DL;
@ -59,7 +60,7 @@ public class Program(IServiceProvider serviceProvider)
AnsiConsole.MarkupLine("[red]Press any key to exit.[/]");
Log.Error("auth invalid after attempt to get auth from browser");
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
await authService.SaveToFileAsync();
@ -85,11 +86,12 @@ public class Program(IServiceProvider serviceProvider)
ServiceCollection services = new();
services.AddSingleton<ILoggingService, LoggingService>();
services.AddSingleton<IConfigService, ConfigService>();
services.AddSingleton<ExitHelper>();
ServiceProvider tempServiceProvider = services.BuildServiceProvider();
ILoggingService loggingService = tempServiceProvider.GetRequiredService<ILoggingService>();
IConfigService configService = tempServiceProvider.GetRequiredService<IConfigService>();
ExitHelper tempExitHelper = tempServiceProvider.GetRequiredService<ExitHelper>();
if (!await configService.LoadConfigurationAsync(args))
{
@ -100,7 +102,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(3);
tempExitHelper.ExitWithCode(3);
}
AnsiConsole.Markup("[green]config.conf located successfully!\n[/]");
@ -117,6 +119,7 @@ public class Program(IServiceProvider serviceProvider)
services.AddSingleton<IStartupService, StartupService>();
services.AddSingleton<IDownloadOrchestrationService, DownloadOrchestrationService>();
services.AddSingleton<Program>();
services.AddSingleton<ExitHelper>();
return services;
}
@ -152,7 +155,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(1);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(1);
}
if (!startupResult.FfmpegFound)
@ -169,7 +172,7 @@ public class Program(IServiceProvider serviceProvider)
"[red]Cannot locate FFmpeg; please modify config.conf with the correct path.[/]");
}
Environment.Exit(4);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(4);
}
// Auth flow
@ -220,7 +223,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
}
@ -249,7 +252,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(5);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(5);
}
}
@ -746,7 +749,7 @@ public class Program(IServiceProvider serviceProvider)
AnsiConsole.MarkupLine(
"[red]You may also want to try using the browser extension which is documented here:[/]\n");
AnsiConsole.MarkupLine("[link]https://docs.ofdl.tools/config/auth/#legacy-methods[/]");
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
if (!configService.CurrentConfig.DisableBrowserAuth)
@ -775,7 +778,7 @@ public class Program(IServiceProvider serviceProvider)
}
}
private static void ShowAuthMissingError(bool nonInteractiveMode)
private void ShowAuthMissingError(bool nonInteractiveMode)
{
AnsiConsole.MarkupLine(
"\n[red]auth.json is missing. The file can be generated automatically when OF-DL is run in the standard, interactive mode.[/]\n");
@ -789,7 +792,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
private static void DisplayVersionResult(VersionCheckResult result)
@ -879,7 +882,7 @@ public class Program(IServiceProvider serviceProvider)
}
}
private static void DisplayRulesJsonResult(StartupResult result, IConfigService configService)
private void DisplayRulesJsonResult(StartupResult result, IConfigService configService)
{
if (result.RulesJsonExists)
{
@ -899,7 +902,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
}
}