2025-02-15 15:55:29 -03:00
|
|
|
using AutoScan.Jobs;
|
|
|
|
using AutoScan.Listener;
|
2025-02-13 19:13:21 -03:00
|
|
|
using AutoScan.Options;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-14 20:32:04 -03:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Quartz;
|
2025-02-15 15:55:29 -03:00
|
|
|
using Quartz.Impl.Matchers;
|
2025-02-13 19:13:21 -03:00
|
|
|
|
|
|
|
namespace AutoScan;
|
|
|
|
|
|
|
|
public class AutoScanApp
|
|
|
|
{
|
|
|
|
private readonly AutoScanOptions _options;
|
|
|
|
private readonly ILogger<AutoScanApp> _logger;
|
2025-02-14 20:32:04 -03:00
|
|
|
private readonly IScheduler _scheduler;
|
2025-02-15 15:55:29 -03:00
|
|
|
private readonly IChainerListenerFactory _chainerListenerFactory;
|
2025-02-13 19:13:21 -03:00
|
|
|
|
2025-02-15 15:55:29 -03:00
|
|
|
public AutoScanApp(IOptions<AutoScanOptions> options, ILogger<AutoScanApp> logger, IScheduler scheduler, IChainerListenerFactory chainerListenerFactory)
|
2025-02-13 19:13:21 -03:00
|
|
|
{
|
2025-02-14 20:32:04 -03:00
|
|
|
_options = options.Value;
|
2025-02-13 19:13:21 -03:00
|
|
|
_logger = logger;
|
2025-02-14 20:32:04 -03:00
|
|
|
_scheduler = scheduler;
|
2025-02-15 15:55:29 -03:00
|
|
|
_chainerListenerFactory = chainerListenerFactory;
|
2025-02-13 19:13:21 -03:00
|
|
|
}
|
|
|
|
|
2025-02-14 20:32:04 -03:00
|
|
|
public async Task Run(CancellationToken cancellationToken)
|
2025-02-13 19:13:21 -03:00
|
|
|
{
|
|
|
|
_logger.LogInformation("AutoScanApp is running...");
|
2025-02-14 20:32:04 -03:00
|
|
|
|
|
|
|
var at = DateTime.Now.AddMinutes(1).ToString("HH:mm");
|
|
|
|
var cron = CronFromAt(at);
|
2025-02-15 15:55:29 -03:00
|
|
|
//var cron = CronFromAt(_options.At);
|
2025-02-14 20:32:04 -03:00
|
|
|
_logger.LogInformation("Waiting for next scan at {At} [{cron}].", at, cron);
|
|
|
|
|
|
|
|
await _scheduler.Start(cancellationToken);
|
2025-02-15 15:55:29 -03:00
|
|
|
_logger.LogDebug("Scheduler started successfully!");
|
2025-02-14 20:32:04 -03:00
|
|
|
|
2025-02-15 16:05:11 -03:00
|
|
|
const string groupName = "ScanGroup";
|
2025-02-15 15:55:29 -03:00
|
|
|
|
|
|
|
IJobDetail downloaderJob = JobBuilder.Create<DownloaderJob>()
|
2025-02-15 16:05:11 -03:00
|
|
|
.WithIdentity("downloader", groupName)
|
2025-02-15 15:55:29 -03:00
|
|
|
.Build();
|
|
|
|
|
2025-02-15 16:05:11 -03:00
|
|
|
//Job with no trigger but tied to the chainer to be executed after the downloader job
|
2025-02-15 15:55:29 -03:00
|
|
|
IJobDetail scannerJob = JobBuilder.Create<ScannerJob>()
|
2025-02-15 16:05:11 -03:00
|
|
|
.WithIdentity("scanner", groupName)
|
|
|
|
.StoreDurably()
|
2025-02-14 20:32:04 -03:00
|
|
|
.Build();
|
|
|
|
|
|
|
|
ITrigger trigger = TriggerBuilder.Create()
|
2025-02-15 16:05:11 -03:00
|
|
|
.WithIdentity("trigger1", groupName)
|
2025-02-14 20:32:04 -03:00
|
|
|
.WithCronSchedule(cron)
|
2025-02-15 15:55:29 -03:00
|
|
|
.StartNow()
|
2025-02-14 20:32:04 -03:00
|
|
|
.Build();
|
|
|
|
|
2025-02-15 15:55:29 -03:00
|
|
|
var chainer = _chainerListenerFactory.CreateChainerListener("Scan Chainer");
|
|
|
|
chainer.AddJobChainLink(downloaderJob.Key, scannerJob.Key);
|
|
|
|
|
2025-02-15 16:05:11 -03:00
|
|
|
_scheduler.ListenerManager.AddJobListener(chainer, GroupMatcher<JobKey>.GroupEquals(groupName));
|
2025-02-15 15:55:29 -03:00
|
|
|
|
|
|
|
await _scheduler.ScheduleJob(downloaderJob, trigger, cancellationToken);
|
|
|
|
await _scheduler.AddJob(scannerJob, false, true, cancellationToken);
|
|
|
|
|
|
|
|
_logger.LogDebug("Scheduled job successfully!");
|
2025-02-13 19:13:21 -03:00
|
|
|
}
|
2025-02-14 20:32:04 -03:00
|
|
|
|
2025-02-15 15:55:29 -03:00
|
|
|
|
2025-02-14 20:32:04 -03:00
|
|
|
private string CronFromAt(string at)
|
|
|
|
{
|
|
|
|
var parts = at.Split(':');
|
|
|
|
return $"0 {parts[1]} {parts[0]} * * ?";
|
|
|
|
}
|
|
|
|
|
2025-02-13 19:13:21 -03:00
|
|
|
}
|