added startup job trigger (#12)
This commit is contained in:
@@ -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";
|
||||||
|
|
||||||
|
|||||||
@@ -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()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user