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