From 11f7c7ed0286a3cd8dce86e531446da7d6e3cbf7 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 11 Aug 2024 13:52:39 -0500 Subject: [PATCH] refactor: Use Autofac composite support for global setup tasks Utilize built-in composite support in Autofac for `IGlobalSetupTask` implementations. --- src/Recyclarr.Cli/CompositionRoot.cs | 2 +- .../Console/Helpers/CommandSetupInterceptor.cs | 12 +++++++----- ...upTaskExecutor.cs => CompositeGlobalSetupTask.cs} | 5 ++++- 3 files changed, 12 insertions(+), 7 deletions(-) rename src/Recyclarr.Cli/Console/Setup/{GlobalSetupTaskExecutor.cs => CompositeGlobalSetupTask.cs} (59%) diff --git a/src/Recyclarr.Cli/CompositionRoot.cs b/src/Recyclarr.Cli/CompositionRoot.cs index 4a335414..e6f34300 100644 --- a/src/Recyclarr.Cli/CompositionRoot.cs +++ b/src/Recyclarr.Cli/CompositionRoot.cs @@ -99,7 +99,7 @@ public static class CompositionRoot { builder.RegisterType().As(); - builder.RegisterType(); + builder.RegisterComposite(); builder.RegisterTypes( typeof(ProgramInformationDisplayTask), typeof(JanitorCleanupTask)) diff --git a/src/Recyclarr.Cli/Console/Helpers/CommandSetupInterceptor.cs b/src/Recyclarr.Cli/Console/Helpers/CommandSetupInterceptor.cs index 11e1873e..7d021186 100644 --- a/src/Recyclarr.Cli/Console/Helpers/CommandSetupInterceptor.cs +++ b/src/Recyclarr.Cli/Console/Helpers/CommandSetupInterceptor.cs @@ -12,21 +12,22 @@ internal sealed class CommandSetupInterceptor : ICommandInterceptor, IDisposable private readonly ConsoleAppCancellationTokenSource _ct = new(); private readonly LoggingLevelSwitch _loggingLevelSwitch; private readonly IAppDataSetup _appDataSetup; - private readonly Lazy _taskExecutor; + private readonly Lazy _globalTaskSetup; public CommandSetupInterceptor( Lazy log, LoggingLevelSwitch loggingLevelSwitch, IAppDataSetup appDataSetup, - Lazy taskExecutor) + Lazy globalTaskSetup) { _loggingLevelSwitch = loggingLevelSwitch; _appDataSetup = appDataSetup; - _taskExecutor = taskExecutor; + _globalTaskSetup = globalTaskSetup; _ct.CancelPressed.Subscribe(_ => log.Value.Information("Exiting due to signal interrupt")); } + // Executed on CLI startup public void Intercept(CommandContext context, CommandSettings settings) { switch (settings) @@ -36,12 +37,13 @@ internal sealed class CommandSetupInterceptor : ICommandInterceptor, IDisposable break; } - _taskExecutor.Value.OnStart(); + _globalTaskSetup.Value.OnStart(); } + // Executed on CLI exit public void InterceptResult(CommandContext context, CommandSettings settings, ref int result) { - _taskExecutor.Value.OnFinish(); + _globalTaskSetup.Value.OnFinish(); } private void HandleBaseCommand(BaseCommandSettings cmd) diff --git a/src/Recyclarr.Cli/Console/Setup/GlobalSetupTaskExecutor.cs b/src/Recyclarr.Cli/Console/Setup/CompositeGlobalSetupTask.cs similarity index 59% rename from src/Recyclarr.Cli/Console/Setup/GlobalSetupTaskExecutor.cs rename to src/Recyclarr.Cli/Console/Setup/CompositeGlobalSetupTask.cs index 3d60422d..b91f3f05 100644 --- a/src/Recyclarr.Cli/Console/Setup/GlobalSetupTaskExecutor.cs +++ b/src/Recyclarr.Cli/Console/Setup/CompositeGlobalSetupTask.cs @@ -1,6 +1,9 @@ +using JetBrains.Annotations; + namespace Recyclarr.Cli.Console.Setup; -public class GlobalSetupTaskExecutor(IOrderedEnumerable tasks) +[UsedImplicitly] +public class CompositeGlobalSetupTask(IOrderedEnumerable tasks) : IGlobalSetupTask { public void OnStart() {