Files
cleanuperr/code/Executable/DependencyInjection/QuartzDI.cs
T
Marius Nechifor 3e0913b437 Fix empty torrents (#11)
* fixed unwanted deletion of torrents in downloading metadata state

* refactored jobs code

* updated arr test data

* updated gitignore

* updated test configuration and removed dispensable files
2024-11-24 01:01:20 +02:00

99 lines
2.9 KiB
C#

using Common.Configuration;
using Common.Configuration.ContentBlocker;
using Common.Configuration.QueueCleaner;
using Executable.Jobs;
using Infrastructure.Verticals.ContentBlocker;
using Infrastructure.Verticals.QueueCleaner;
using Quartz;
namespace Executable.DependencyInjection;
public static class QuartzDI
{
public static IServiceCollection AddQuartzServices(this IServiceCollection services, IConfiguration configuration) =>
services
.AddQuartz(q =>
{
TriggersConfig? config = configuration
.GetRequiredSection(TriggersConfig.SectionName)
.Get<TriggersConfig>();
if (config is null)
{
throw new NullReferenceException("triggers configuration is null");
}
q.AddQueueCleanerJob(configuration, config.QueueCleaner);
q.AddContentBlockerJob(configuration, config.ContentBlocker);
})
.AddQuartzHostedService(opt =>
{
opt.WaitForJobsToComplete = true;
});
private static void AddQueueCleanerJob(
this IServiceCollectionQuartzConfigurator q,
IConfiguration configuration,
string trigger
)
{
QueueCleanerConfig? config = configuration
.GetRequiredSection(QueueCleanerConfig.SectionName)
.Get<QueueCleanerConfig>();
if (config is null)
{
throw new NullReferenceException($"{nameof(QueueCleaner)} configuration is null");
}
if (!config.Enabled)
{
return;
}
q.AddJob<GenericJob<QueueCleaner>>(opts =>
{
opts.WithIdentity(nameof(QueueCleaner));
});
q.AddTrigger(opts =>
{
opts.ForJob(nameof(QueueCleaner))
.WithIdentity($"{nameof(QueueCleaner)}-trigger")
.WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing());
});
}
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));
});
q.AddTrigger(opts =>
{
opts.ForJob(nameof(ContentBlocker))
.WithIdentity($"{nameof(ContentBlocker)}-trigger")
.WithCronSchedule(trigger, x =>x.WithMisfireHandlingInstructionDoNothing());
});
}
}