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 ba0347f86f
commit 3c8231d329
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 Serilog;
using Spectre.Console;
using OF_DL.Helpers;
namespace OF_DL;
@ -57,7 +58,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();
@ -83,11 +84,12 @@ public class Program(IServiceProvider serviceProvider)
ServiceCollection services = new();
services.AddSingleton<ILoggingService, LoggingService>();
services.AddSingleton<IConfigService, ConfigService>();
services.AddSingleton(new ExitHelper(new SpectreDownloadEventHandler()));
ServiceProvider tempServiceProvider = services.BuildServiceProvider();
ILoggingService loggingService = tempServiceProvider.GetRequiredService<ILoggingService>();
IConfigService configService = tempServiceProvider.GetRequiredService<IConfigService>();
ExitHelper exitHelper = tempServiceProvider.GetRequiredService<ExitHelper>();
if (!await configService.LoadConfigurationAsync(args))
{
@ -98,7 +100,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(3);
exitHelper.ExitWithCode(3);
}
AnsiConsole.Markup("[green]config.conf located successfully!\n[/]");
@ -107,6 +109,7 @@ public class Program(IServiceProvider serviceProvider)
services = [];
services.AddSingleton(loggingService);
services.AddSingleton(configService);
services.AddSingleton(exitHelper);
services.AddSingleton<IAuthService, AuthService>();
services.AddSingleton<IApiService, ApiService>();
services.AddSingleton<IDbService, DbService>();
@ -150,7 +153,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(1);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(1);
}
if (!startupResult.FfmpegFound)
@ -167,7 +170,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);
}
if (!startupResult.FfprobeFound)
@ -235,7 +238,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
}
@ -264,7 +267,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(5);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(5);
}
}
@ -761,7 +764,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)
@ -790,7 +793,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");
@ -804,7 +807,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
private static void DisplayVersionResult(VersionCheckResult result)
@ -897,7 +900,7 @@ public class Program(IServiceProvider serviceProvider)
}
}
private static void DisplayRulesJsonResult(StartupResult result, IConfigService configService)
private void DisplayRulesJsonResult(StartupResult result, IConfigService configService)
{
if (result.RulesJsonExists)
{
@ -917,7 +920,7 @@ public class Program(IServiceProvider serviceProvider)
Console.ReadKey();
}
Environment.Exit(2);
serviceProvider.GetRequiredService<ExitHelper>().ExitWithCode(2);
}
}
}