added startup job trigger (#12)

This commit is contained in:
Marius Nechifor
2024-11-24 01:47:01 +02:00
committed by GitHub
parent 3e0913b437
commit 599242aa2a
4 changed files with 52 additions and 42 deletions
@@ -1,6 +1,6 @@
namespace Common.Configuration.ContentBlocker; namespace Common.Configuration.ContentBlocker;
public sealed record ContentBlockerConfig : IConfig public sealed record ContentBlockerConfig : IJobConfig
{ {
public const string SectionName = "ContentBlocker"; public const string SectionName = "ContentBlocker";
+6
View File
@@ -0,0 +1,6 @@
namespace Common.Configuration;
public interface IJobConfig : IConfig
{
bool Enabled { get; init; }
}
@@ -1,8 +1,12 @@
namespace Common.Configuration.QueueCleaner; namespace Common.Configuration.QueueCleaner;
public sealed record QueueCleanerConfig public sealed record QueueCleanerConfig : IJobConfig
{ {
public const string SectionName = "QueueCleaner"; public const string SectionName = "QueueCleaner";
public required bool Enabled { get; init; } public required bool Enabled { get; init; }
public void Validate()
{
}
} }
+42 -42
View File
@@ -3,6 +3,7 @@ using Common.Configuration.ContentBlocker;
using Common.Configuration.QueueCleaner; using Common.Configuration.QueueCleaner;
using Executable.Jobs; using Executable.Jobs;
using Infrastructure.Verticals.ContentBlocker; using Infrastructure.Verticals.ContentBlocker;
using Infrastructure.Verticals.Jobs;
using Infrastructure.Verticals.QueueCleaner; using Infrastructure.Verticals.QueueCleaner;
using Quartz; using Quartz;
@@ -23,27 +24,50 @@ public static class QuartzDI
throw new NullReferenceException("triggers configuration is null"); throw new NullReferenceException("triggers configuration is null");
} }
q.AddQueueCleanerJob(configuration, config.QueueCleaner); q.AddJobs(configuration, config);
q.AddContentBlockerJob(configuration, config.ContentBlocker);
}) })
.AddQuartzHostedService(opt => .AddQuartzHostedService(opt =>
{ {
opt.WaitForJobsToComplete = true; opt.WaitForJobsToComplete = true;
}); });
private static void AddQueueCleanerJob( private static void AddJobs(
this IServiceCollectionQuartzConfigurator q, this IServiceCollectionQuartzConfigurator q,
IConfiguration configuration, IConfiguration configuration,
string trigger TriggersConfig triggersConfig
) )
{ {
QueueCleanerConfig? config = configuration q.AddJob<QueueCleaner, QueueCleanerConfig>(
.GetRequiredSection(QueueCleanerConfig.SectionName) configuration,
.Get<QueueCleanerConfig>(); QueueCleanerConfig.SectionName,
triggersConfig.QueueCleaner
);
q.AddJob<ContentBlocker, ContentBlockerConfig>(
configuration,
ContentBlockerConfig.SectionName,
triggersConfig.ContentBlocker
);
}
private static void AddJob<T, TConfig>(
this IServiceCollectionQuartzConfigurator q,
IConfiguration configuration,
string configSectionName,
string trigger
)
where T: GenericHandler
where TConfig : IJobConfig
{
IJobConfig? config = configuration
.GetRequiredSection(configSectionName)
.Get<TConfig>();
string typeName = typeof(T).Name;
if (config is null) if (config is null)
{ {
throw new NullReferenceException($"{nameof(QueueCleaner)} configuration is null"); throw new NullReferenceException($"{typeName} configuration is null");
} }
if (!config.Enabled) if (!config.Enabled)
@@ -51,49 +75,25 @@ public static class QuartzDI
return; return;
} }
q.AddJob<GenericJob<QueueCleaner>>(opts => q.AddJob<GenericJob<T>>(opts =>
{ {
opts.WithIdentity(nameof(QueueCleaner)); opts.WithIdentity(typeName);
}); });
q.AddTrigger(opts => q.AddTrigger(opts =>
{ {
opts.ForJob(nameof(QueueCleaner)) opts.ForJob(typeName)
.WithIdentity($"{nameof(QueueCleaner)}-trigger") .WithIdentity($"{typeName}-trigger")
.WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing()); .WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing())
}); .StartNow();
}
private static void AddContentBlockerJob(
this IServiceCollectionQuartzConfigurator q,
IConfiguration configuration,
string trigger
)
{
ContentBlockerConfig? config = configuration
.GetRequiredSection(ContentBlockerConfig.SectionName)
.Get<ContentBlockerConfig>();
if (config is null)
{
throw new NullReferenceException($"{nameof(ContentBlocker)} configuration is null");
}
if (!config.Enabled)
{
return;
}
q.AddJob<GenericJob<ContentBlocker>>(opts =>
{
opts.WithIdentity(nameof(ContentBlocker));
}); });
// Startup trigger
q.AddTrigger(opts => q.AddTrigger(opts =>
{ {
opts.ForJob(nameof(ContentBlocker)) opts.ForJob(typeName)
.WithIdentity($"{nameof(ContentBlocker)}-trigger") .WithIdentity($"{typeName}-startup-trigger")
.WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing()); .StartNow();
}); });
} }
} }