From 599242aa2a8684d39a0f137bc34963eb98f1018c Mon Sep 17 00:00:00 2001 From: Marius Nechifor Date: Sun, 24 Nov 2024 01:47:01 +0200 Subject: [PATCH] added startup job trigger (#12) --- .../ContentBlocker/ContentBlockerConfig.cs | 2 +- code/Common/Configuration/IJobConfig.cs | 6 ++ .../QueueCleaner/QueueCleanerConfig.cs | 6 +- .../DependencyInjection/QuartzDI.cs | 80 +++++++++---------- 4 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 code/Common/Configuration/IJobConfig.cs diff --git a/code/Common/Configuration/ContentBlocker/ContentBlockerConfig.cs b/code/Common/Configuration/ContentBlocker/ContentBlockerConfig.cs index b20c4527..1e50a5b1 100644 --- a/code/Common/Configuration/ContentBlocker/ContentBlockerConfig.cs +++ b/code/Common/Configuration/ContentBlocker/ContentBlockerConfig.cs @@ -1,6 +1,6 @@ namespace Common.Configuration.ContentBlocker; -public sealed record ContentBlockerConfig : IConfig +public sealed record ContentBlockerConfig : IJobConfig { public const string SectionName = "ContentBlocker"; diff --git a/code/Common/Configuration/IJobConfig.cs b/code/Common/Configuration/IJobConfig.cs new file mode 100644 index 00000000..1ef7a8f1 --- /dev/null +++ b/code/Common/Configuration/IJobConfig.cs @@ -0,0 +1,6 @@ +namespace Common.Configuration; + +public interface IJobConfig : IConfig +{ + bool Enabled { get; init; } +} \ No newline at end of file diff --git a/code/Common/Configuration/QueueCleaner/QueueCleanerConfig.cs b/code/Common/Configuration/QueueCleaner/QueueCleanerConfig.cs index 7b3e9584..13de9848 100644 --- a/code/Common/Configuration/QueueCleaner/QueueCleanerConfig.cs +++ b/code/Common/Configuration/QueueCleaner/QueueCleanerConfig.cs @@ -1,8 +1,12 @@ namespace Common.Configuration.QueueCleaner; -public sealed record QueueCleanerConfig +public sealed record QueueCleanerConfig : IJobConfig { public const string SectionName = "QueueCleaner"; public required bool Enabled { get; init; } + + public void Validate() + { + } } \ No newline at end of file diff --git a/code/Executable/DependencyInjection/QuartzDI.cs b/code/Executable/DependencyInjection/QuartzDI.cs index c054b0a6..32a7a3a1 100644 --- a/code/Executable/DependencyInjection/QuartzDI.cs +++ b/code/Executable/DependencyInjection/QuartzDI.cs @@ -3,6 +3,7 @@ using Common.Configuration.ContentBlocker; using Common.Configuration.QueueCleaner; using Executable.Jobs; using Infrastructure.Verticals.ContentBlocker; +using Infrastructure.Verticals.Jobs; using Infrastructure.Verticals.QueueCleaner; using Quartz; @@ -23,60 +24,50 @@ public static class QuartzDI throw new NullReferenceException("triggers configuration is null"); } - q.AddQueueCleanerJob(configuration, config.QueueCleaner); - q.AddContentBlockerJob(configuration, config.ContentBlocker); + q.AddJobs(configuration, config); }) .AddQuartzHostedService(opt => { opt.WaitForJobsToComplete = true; }); - private static void AddQueueCleanerJob( + private static void AddJobs( this IServiceCollectionQuartzConfigurator q, IConfiguration configuration, - string trigger + TriggersConfig triggersConfig ) { - QueueCleanerConfig? config = configuration - .GetRequiredSection(QueueCleanerConfig.SectionName) - .Get(); - - if (config is null) - { - throw new NullReferenceException($"{nameof(QueueCleaner)} configuration is null"); - } - - if (!config.Enabled) - { - return; - } + q.AddJob( + configuration, + QueueCleanerConfig.SectionName, + triggersConfig.QueueCleaner + ); - q.AddJob>(opts => - { - opts.WithIdentity(nameof(QueueCleaner)); - }); - - q.AddTrigger(opts => - { - opts.ForJob(nameof(QueueCleaner)) - .WithIdentity($"{nameof(QueueCleaner)}-trigger") - .WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing()); - }); + q.AddJob( + configuration, + ContentBlockerConfig.SectionName, + triggersConfig.ContentBlocker + ); } - - private static void AddContentBlockerJob( + + private static void AddJob( this IServiceCollectionQuartzConfigurator q, IConfiguration configuration, + string configSectionName, string trigger ) + where T: GenericHandler + where TConfig : IJobConfig { - ContentBlockerConfig? config = configuration - .GetRequiredSection(ContentBlockerConfig.SectionName) - .Get(); - + IJobConfig? config = configuration + .GetRequiredSection(configSectionName) + .Get(); + + string typeName = typeof(T).Name; + if (config is null) { - throw new NullReferenceException($"{nameof(ContentBlocker)} configuration is null"); + throw new NullReferenceException($"{typeName} configuration is null"); } if (!config.Enabled) @@ -84,16 +75,25 @@ public static class QuartzDI return; } - q.AddJob>(opts => + q.AddJob>(opts => { - opts.WithIdentity(nameof(ContentBlocker)); + opts.WithIdentity(typeName); }); q.AddTrigger(opts => { - opts.ForJob(nameof(ContentBlocker)) - .WithIdentity($"{nameof(ContentBlocker)}-trigger") - .WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing()); + opts.ForJob(typeName) + .WithIdentity($"{typeName}-trigger") + .WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing()) + .StartNow(); + }); + + // Startup trigger + q.AddTrigger(opts => + { + opts.ForJob(typeName) + .WithIdentity($"{typeName}-startup-trigger") + .StartNow(); }); } } \ No newline at end of file