feat: extract chainer builder and add clean job
This commit is contained in:
parent
3791738c41
commit
3ed521350e
@ -10,6 +10,10 @@ namespace AutoScan;
|
|||||||
|
|
||||||
public class AutoScanApp
|
public class AutoScanApp
|
||||||
{
|
{
|
||||||
|
private const string GROUP_NAME = "ScanGroup";
|
||||||
|
private const string JOBKEY_DOWNLOADER = "downloader";
|
||||||
|
private const string JOBKEY_SCANNER = "scanner";
|
||||||
|
private const string JOBKEY_CLEANER = "cleaner";
|
||||||
private readonly AutoScanOptions _options;
|
private readonly AutoScanOptions _options;
|
||||||
private readonly ILogger<AutoScanApp> _logger;
|
private readonly ILogger<AutoScanApp> _logger;
|
||||||
private readonly IScheduler _scheduler;
|
private readonly IScheduler _scheduler;
|
||||||
@ -35,37 +39,43 @@ public class AutoScanApp
|
|||||||
_logger.LogInformation("DryRun: {RunDry} | Scan DryRun: {scanRunDry}", _options.RunDry, _options.Scanner?.RunDry);
|
_logger.LogInformation("DryRun: {RunDry} | Scan DryRun: {scanRunDry}", _options.RunDry, _options.Scanner?.RunDry);
|
||||||
_logger.LogInformation("AutoScanApp is running...");
|
_logger.LogInformation("AutoScanApp is running...");
|
||||||
|
|
||||||
var at = DateTime.Now.AddSeconds(10);
|
// var at = DateTime.Now.AddSeconds(10);
|
||||||
// var cron = $"{at.Second} {at.Minute} {at.Hour} * * ?";
|
// var cron = $"{at.Second} {at.Minute} {at.Hour} * * ?";
|
||||||
var cron = CronFromAt(_options.At);
|
var cron = CronFromAt(_options.At);
|
||||||
_logger.LogInformation("Waiting for next scan at {At} [{cron}].", at, cron);
|
_logger.LogInformation("Waiting for next scan at {At} [{cron}].", _options.At, cron);
|
||||||
|
|
||||||
await _scheduler.Start(cancellationToken);
|
await _scheduler.Start(cancellationToken);
|
||||||
_logger.LogDebug("Scheduler started successfully!");
|
_logger.LogDebug("Scheduler started successfully!");
|
||||||
|
|
||||||
const string groupName = "ScanGroup";
|
|
||||||
|
|
||||||
IJobDetail downloaderJob = JobBuilder.Create<DownloaderJob>()
|
IJobDetail downloaderJob = JobBuilder.Create<DownloaderJob>()
|
||||||
.WithIdentity("downloader", groupName)
|
.WithIdentity(JOBKEY_DOWNLOADER, GROUP_NAME)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
//Job with no trigger but tied to the chainer to be executed after the downloader job
|
//Job with no trigger but tied to the chainer to be executed after the downloader job
|
||||||
IJobDetail scannerJob = JobBuilder.Create<ScannerJob>()
|
IJobDetail scannerJob = JobBuilder.Create<ScannerJob>()
|
||||||
.WithIdentity("scanner", groupName)
|
.WithIdentity(JOBKEY_SCANNER, GROUP_NAME)
|
||||||
|
.StoreDurably()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
IJobDetail cleanJob = JobBuilder.Create<CleanJob>()
|
||||||
|
.WithIdentity(JOBKEY_CLEANER, GROUP_NAME)
|
||||||
.StoreDurably()
|
.StoreDurably()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
ITrigger trigger = TriggerBuilder.Create()
|
ITrigger trigger = TriggerBuilder.Create()
|
||||||
.WithIdentity("trigger1", groupName)
|
.WithIdentity("trigger1", GROUP_NAME)
|
||||||
.WithCronSchedule(cron)
|
.WithCronSchedule(cron)
|
||||||
.StartNow()
|
.StartNow()
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var chainer = _chainerListenerFactory.CreateChainerListener("Scan Chainer");
|
var chainer = _chainerListenerFactory.Start("Scan Chainer")
|
||||||
chainer.AddJobChainLink(downloaderJob.Key, scannerJob.Key);
|
.AddJob(downloaderJob)
|
||||||
chainer.OnJobChainFinished = async () => await OnScanCompleted?.Invoke(_options);
|
.AddJob(scannerJob)
|
||||||
|
.AddJob(cleanJob)
|
||||||
|
.OnFinish(async () => await OnScanCompleted?.Invoke(_options))
|
||||||
|
.Build();
|
||||||
|
|
||||||
_scheduler.ListenerManager.AddJobListener(chainer, GroupMatcher<JobKey>.GroupEquals(groupName));
|
_scheduler.ListenerManager.AddJobListener(chainer, GroupMatcher<JobKey>.GroupEquals(GROUP_NAME));
|
||||||
|
|
||||||
await _scheduler.ScheduleJob(downloaderJob, trigger, cancellationToken);
|
await _scheduler.ScheduleJob(downloaderJob, trigger, cancellationToken);
|
||||||
await _scheduler.AddJob(scannerJob, false, true, cancellationToken);
|
await _scheduler.AddJob(scannerJob, false, true, cancellationToken);
|
||||||
@ -73,6 +83,12 @@ public class AutoScanApp
|
|||||||
_logger.LogDebug("Scheduled job successfully!");
|
_logger.LogDebug("Scheduled job successfully!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task StartNewScan()
|
||||||
|
{
|
||||||
|
var jobKey = new JobKey(JOBKEY_DOWNLOADER, "ScanGroup");
|
||||||
|
await _scheduler.TriggerJob(jobKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private string CronFromAt(string at)
|
private string CronFromAt(string at)
|
||||||
{
|
{
|
||||||
|
43
src/CasaBot/AutoScan/Listener/ChainerListenerBuilder.cs
Normal file
43
src/CasaBot/AutoScan/Listener/ChainerListenerBuilder.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using Quartz;
|
||||||
|
|
||||||
|
namespace AutoScan.Listener;
|
||||||
|
|
||||||
|
public class ChainerListenerBuilder
|
||||||
|
{
|
||||||
|
private readonly ChainerListener _chainerListener;
|
||||||
|
|
||||||
|
private readonly List<IJobDetail> _chain = [];
|
||||||
|
|
||||||
|
private Func<Task> _finishCallback = () => Task.CompletedTask;
|
||||||
|
|
||||||
|
public ChainerListenerBuilder(ChainerListener chainerListener)
|
||||||
|
{
|
||||||
|
_chainerListener = chainerListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChainerListenerBuilder AddJob(IJobDetail job)
|
||||||
|
{
|
||||||
|
_chain.Add(job);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChainerListenerBuilder OnFinish(Func<Task> callback)
|
||||||
|
{
|
||||||
|
_finishCallback = callback;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChainerListener Build()
|
||||||
|
{
|
||||||
|
if(_chain.Count < 2)
|
||||||
|
{
|
||||||
|
throw new Exception("ChainerListenerBuilder requires at least 2 jobs to chain.");
|
||||||
|
}
|
||||||
|
for (var i = 0; i < _chain.Count -1; i++)
|
||||||
|
{
|
||||||
|
_chainerListener.AddJobChainLink(_chain[i].Key, _chain[i + 1].Key);
|
||||||
|
}
|
||||||
|
_chainerListener.OnJobChainFinished = _finishCallback;
|
||||||
|
return _chainerListener;
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ namespace AutoScan.Listener;
|
|||||||
public interface IChainerListenerFactory
|
public interface IChainerListenerFactory
|
||||||
{
|
{
|
||||||
ChainerListener CreateChainerListener(string name);
|
ChainerListener CreateChainerListener(string name);
|
||||||
|
ChainerListenerBuilder Start(string name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ChainerListenerFactory : IChainerListenerFactory
|
public class ChainerListenerFactory : IChainerListenerFactory
|
||||||
@ -22,4 +23,11 @@ public class ChainerListenerFactory : IChainerListenerFactory
|
|||||||
var logger = _loggerFactory.CreateLogger<ChainerListener>();
|
var logger = _loggerFactory.CreateLogger<ChainerListener>();
|
||||||
return new ChainerListener(name, logger);
|
return new ChainerListener(name, logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ChainerListenerBuilder Start(string name)
|
||||||
|
{
|
||||||
|
var logger = _loggerFactory.CreateLogger<ChainerListener>();
|
||||||
|
return new ChainerListenerBuilder(new ChainerListener(name, logger));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,4 +11,6 @@ public record AutoScanOptions
|
|||||||
public int MaxAmount { get; set; }
|
public int MaxAmount { get; set; }
|
||||||
public string? MediaFolder { get; set; }
|
public string? MediaFolder { get; set; }
|
||||||
public ScannerOptions? Scanner { get; set; }
|
public ScannerOptions? Scanner { get; set; }
|
||||||
|
public bool RemoveOriginalFiles { get; set; }
|
||||||
|
public bool RemoveDetectionFiles { get; set; }
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user