using IRaCIS.Core.Application.MassTransit.Consumer;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;

namespace IRaCIS.Core.API.HostService
{
    public class RecurringJobConfigurationService :
     BackgroundService
    {
        readonly IServiceScopeFactory _scopeFactory;

        public RecurringJobConfigurationService(IServiceScopeFactory scopeFactory)
        {
            _scopeFactory = scopeFactory;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await using var scope = _scopeFactory.CreateAsyncScope();

            var endpoint = scope.ServiceProvider.GetRequiredService<IPublishEndpoint>();

            await endpoint.AddOrUpdateRecurringJob(nameof(MasstransitTestConsumer), new MasstransiTestCommand(), x => x.Every(minutes: 1),
                stoppingToken);
        }
    }
}