Trigger queue cleaner sequentially (#14)

* added option to run queue cleaner after content blocker

* updated readme to clearly state what the jobs do
This commit is contained in:
Marius Nechifor
2024-11-25 21:33:06 +02:00
committed by GitHub
parent 599242aa2a
commit a0c8ff72fb
8 changed files with 118 additions and 44 deletions

View File

@@ -5,6 +5,8 @@ public sealed record QueueCleanerConfig : IJobConfig
public const string SectionName = "QueueCleaner";
public required bool Enabled { get; init; }
public required bool RunSequentially { get; init; }
public void Validate()
{

View File

@@ -37,32 +37,33 @@ public static class QuartzDI
TriggersConfig triggersConfig
)
{
q.AddJob<QueueCleaner, QueueCleanerConfig>(
configuration,
QueueCleanerConfig.SectionName,
triggersConfig.QueueCleaner
);
ContentBlockerConfig? contentBlockerConfig = configuration
.GetRequiredSection(ContentBlockerConfig.SectionName)
.Get<ContentBlockerConfig>();
q.AddJob<ContentBlocker, ContentBlockerConfig>(
configuration,
ContentBlockerConfig.SectionName,
triggersConfig.ContentBlocker
);
q.AddJob<ContentBlocker>(contentBlockerConfig, triggersConfig.ContentBlocker);
QueueCleanerConfig? queueCleanerConfig = configuration
.GetRequiredSection(QueueCleanerConfig.SectionName)
.Get<QueueCleanerConfig>();
if (contentBlockerConfig?.Enabled is true && queueCleanerConfig is { Enabled: true, RunSequentially: true })
{
q.AddJob<QueueCleaner>(queueCleanerConfig, string.Empty);
q.AddJobListener(new JobChainingListener(nameof(QueueCleaner)));
}
else
{
q.AddJob<QueueCleaner>(queueCleanerConfig, triggersConfig.QueueCleaner);
}
}
private static void AddJob<T, TConfig>(
private static void AddJob<T>(
this IServiceCollectionQuartzConfigurator q,
IConfiguration configuration,
string configSectionName,
IJobConfig? config,
string trigger
)
where T: GenericHandler
where TConfig : IJobConfig
) where T: GenericHandler
{
IJobConfig? config = configuration
.GetRequiredSection(configSectionName)
.Get<TConfig>();
string typeName = typeof(T).Name;
if (config is null)
@@ -75,11 +76,25 @@ public static class QuartzDI
return;
}
bool hasTrigger = trigger.Length > 0;
q.AddJob<GenericJob<T>>(opts =>
{
opts.WithIdentity(typeName);
if (!hasTrigger)
{
// jobs with no triggers need to be stored durably
opts.StoreDurably();
}
});
// skip empty triggers
if (!hasTrigger)
{
return;
}
q.AddTrigger(opts =>
{
opts.ForJob(typeName)

View File

@@ -12,7 +12,7 @@
"ContentBlocker": "0/10 * * * * ?"
},
"ContentBlocker": {
"Enabled": false,
"Enabled": true,
"Blacklist": {
"Enabled": false,
"Path": "https://raw.githubusercontent.com/flmorg/cleanuperr/refs/heads/main/blacklist"
@@ -23,7 +23,8 @@
}
},
"QueueCleaner": {
"Enabled": true
"Enabled": true,
"RunSequentially": true
},
"qBittorrent": {
"Enabled": true,

View File

@@ -23,7 +23,8 @@
}
},
"QueueCleaner": {
"Enabled": true
"Enabled": true,
"RunSequentially": true
},
"qBittorrent": {
"Enabled": true,

View File

@@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="QBittorrent.Client" Version="1.9.24285.1" />
<PackageReference Include="Quartz" Version="3.13.1" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,35 @@
using Quartz;
namespace Infrastructure.Verticals.Jobs;
public class JobChainingListener : IJobListener
{
private readonly string _nextJobName;
public JobChainingListener(string nextJobName)
{
_nextJobName = nextJobName;
}
public string Name => nameof(JobChainingListener);
public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken) => Task.CompletedTask;
public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken) => Task.CompletedTask;
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException? jobException, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(_nextJobName))
{
return;
}
IScheduler scheduler = context.Scheduler;
JobKey nextJobKey = new(_nextJobName);
if (await scheduler.CheckExists(nextJobKey, cancellationToken))
{
await scheduler.TriggerJob(nextJobKey, cancellationToken);
}
}
}

View File

@@ -177,6 +177,7 @@ services:
- TRIGGERS__CONTENTBLOCKER=0/30 * * * * ?
- QUEUECLEANER__ENABLED=true
- QUEUECLEANER__RUNSEQUENTIALLY=true
- CONTENTBLOCKER__ENABLED=true
- CONTENTBLOCKER__BLACKLIST__ENABLED=true