using PARR.Core.Services.Workload.Interfaces;
using PARR.WorkloadBuilderWorker.Settings;

namespace PARR.WorkloadBuilderWorker
{
    public class RabbitWorker : BackgroundService
    {
        private readonly ILogger<RabbitWorker> logger;
        private readonly IWorkloadCacheBuilderService workloadCacheBuilderService;
        private readonly WorkerSettings workerSettings;

        public RabbitWorker(ILogger<RabbitWorker> logger, IWorkloadCacheBuilderService workloadCacheBuilderService, WorkerSettings WorkerSettings)
        {
            this.logger = logger;
            this.workloadCacheBuilderService = workloadCacheBuilderService;
            this.workerSettings = WorkerSettings;
        }


        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            logger.LogInformation("Запуск {workerName}", nameof(RabbitWorker));
            await workloadCacheBuilderService.ProcessMessagesAsync(workerSettings.MqSettings);
        }

        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            await workloadCacheBuilderService.StopProcessingAsync();
            await base.StopAsync(cancellationToken);
        }
    }
}
