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-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-13 19:13:21 -03:00
|
|
|
|
2025-02-14 20:32:04 -03:00
|
|
|
public AutoScanApp(IOptions<AutoScanOptions> options, ILogger<AutoScanApp> logger, IScheduler scheduler)
|
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-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);
|
|
|
|
_logger.LogInformation("Waiting for next scan at {At} [{cron}].", at, cron);
|
|
|
|
|
|
|
|
await _scheduler.Start(cancellationToken);
|
|
|
|
_logger.LogInformation("Scheduler started successfully!");
|
|
|
|
|
|
|
|
// define the job and tie it to our HelloJob class
|
|
|
|
IJobDetail job = JobBuilder.Create<ScanJob>()
|
|
|
|
.WithIdentity("job1", "group1")
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
ITrigger trigger = TriggerBuilder.Create()
|
|
|
|
.WithIdentity("trigger1", "group1")
|
|
|
|
.WithCronSchedule(cron)
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
await _scheduler.ScheduleJob(job, trigger, cancellationToken);
|
|
|
|
_logger.LogInformation("Scheduled job successfully!");
|
2025-02-13 19:13:21 -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
|
|
|
}
|