OFDL: 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 d5bedb67b0
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(3000).GetAwaiter().GetResult();
Environment.Exit(exitCode);
}
}

View File

@ -9,6 +9,7 @@ using OF_DL.Models.Entities.Users;
using OF_DL.Services; using OF_DL.Services;
using Serilog; using Serilog;
using Spectre.Console; using Spectre.Console;
using OF_DL.Helpers;
namespace OF_DL; namespace OF_DL;
@ -59,7 +60,7 @@ public class Program(IServiceProvider serviceProvider)
AnsiConsole.MarkupLine("[red]Press any key to exit.[/]"); AnsiConsole.MarkupLine("[red]Press any key to exit.[/]");
Log.Error("auth invalid after attempt to get auth from browser"); Log.Error("auth invalid after attempt to get auth from browser");
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
await authService.SaveToFileAsync(); await authService.SaveToFileAsync();
@ -85,11 +86,12 @@ public class Program(IServiceProvider serviceProvider)
ServiceCollection services = new(); ServiceCollection services = new();
services.AddSingleton<ILoggingService, LoggingService>(); services.AddSingleton<ILoggingService, LoggingService>();
services.AddSingleton<IConfigService, ConfigService>(); services.AddSingleton<IConfigService, ConfigService>();
services.AddSingleton(new ExitHelper(new SpectreDownloadEventHandler()));
ServiceProvider tempServiceProvider = services.BuildServiceProvider(); ServiceProvider tempServiceProvider = services.BuildServiceProvider();
ILoggingService loggingService = tempServiceProvider.GetRequiredService<ILoggingService>(); ILoggingService loggingService = tempServiceProvider.GetRequiredService<ILoggingService>();
IConfigService configService = tempServiceProvider.GetRequiredService<IConfigService>(); IConfigService configService = tempServiceProvider.GetRequiredService<IConfigService>();
ExitHelper exitHelper = tempServiceProvider.GetRequiredService<ExitHelper>();
if (!await configService.LoadConfigurationAsync(args)) if (!await configService.LoadConfigurationAsync(args))
{ {
@ -100,7 +102,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(3); exitHelper.ExitWithCode(3);
} }
AnsiConsole.Markup("[green]config.conf located successfully!\n[/]"); AnsiConsole.Markup("[green]config.conf located successfully!\n[/]");
@ -109,6 +111,7 @@ public class Program(IServiceProvider serviceProvider)
services = []; services = [];
services.AddSingleton(loggingService); services.AddSingleton(loggingService);
services.AddSingleton(configService); services.AddSingleton(configService);
services.AddSingleton(exitHelper);
services.AddSingleton<IAuthService, AuthService>(); services.AddSingleton<IAuthService, AuthService>();
services.AddSingleton<IApiService, ApiService>(); services.AddSingleton<IApiService, ApiService>();
services.AddSingleton<IDbService, DbService>(); services.AddSingleton<IDbService, DbService>();
@ -152,7 +155,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(1); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(1);
} }
if (!startupResult.FfmpegFound) if (!startupResult.FfmpegFound)
@ -169,7 +172,7 @@ public class Program(IServiceProvider serviceProvider)
"[red]Cannot locate FFmpeg; please modify config.conf with the correct path.[/]"); "[red]Cannot locate FFmpeg; please modify config.conf with the correct path.[/]");
} }
Environment.Exit(4); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(4);
} }
// Auth flow // Auth flow
@ -220,7 +223,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
} }
@ -249,7 +252,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(5); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(5);
} }
} }
@ -746,7 +749,7 @@ public class Program(IServiceProvider serviceProvider)
AnsiConsole.MarkupLine( AnsiConsole.MarkupLine(
"[red]You may also want to try using the browser extension which is documented here:[/]\n"); "[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[/]"); AnsiConsole.MarkupLine("[link]https://docs.ofdl.tools/config/auth/#legacy-methods[/]");
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
if (!configService.CurrentConfig.DisableBrowserAuth) 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( 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"); "\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(); Console.ReadKey();
} }
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
private static void DisplayVersionResult(VersionCheckResult result) 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) if (result.RulesJsonExists)
{ {
@ -899,7 +902,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey(); Console.ReadKey();
} }
Environment.Exit(2); serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
} }
} }
} }