using JobEngineBLL.Interfaces; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace JobEngineSL.Services; // Loads all active Quartz jobs from MJOBDEFINE on application startup. // Using IHostedService.StartAsync keeps startup fully async — no sync-over-async deadlock risk. internal sealed class JobEngineStartupService : IHostedService { private readonly IQuartzSyncService _syncService; private readonly ILogger _logger; public JobEngineStartupService(IQuartzSyncService syncService, ILogger logger) { _syncService = syncService; _logger = logger; } public async Task StartAsync(CancellationToken cancellationToken) { try { await _syncService.LoadAllJobsAsync(cancellationToken).ConfigureAwait(false); } catch (Exception ex) when (ex is not OperationCanceledException) { // Log but do not crash the host — scheduler starts empty; jobs will be loaded on next sync _logger.LogError(ex, "JobEngine: failed to load Quartz jobs at startup — running with empty scheduler"); } } public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }