public MassEmailController(AppDbContext context, ILogger <MassEmailController> logger, IConfiguration Configuration, IMassEmailRepository mailrepo, IMassEmailService massmailservice) { _context = context; _logger = logger; _configuration = Configuration; _mailrepo = mailrepo; _massmailservice = massmailservice; int.TryParse(_configuration["SQL:PageRecords"], out pageRecords); }
public string Schedule => "*/10 * * * *"; //every 10 minutes #endif public async Task ExecuteAsync(CancellationToken cancellationToken) { IsRunning = true; try { using (var serviceScope = _serviceProvider.CreateScope()) { AppDbContext context = serviceScope.ServiceProvider.GetRequiredService <AppDbContext>(); if (context == null) { _logger.LogError("Can't obtain DB context"); return; } var companies = await context.Companies.ToListAsync(); IMassEmailService queue_mail = serviceScope.ServiceProvider.GetRequiredService <IMassEmailService>(); var send_queue = await queue_mail.SendEmailFromQueueAsync(); if (!send_queue) { _logger.LogError("Mass mail sending outstanding queue error"); } foreach (var comp in companies) { if (!context.IsHttpContext()) { context.SetCompanyID(comp.Id); } foreach (var em in await context.MassEmail.WhereCompany(comp.Id).AsNoTracking().ToListAsync()) { // Thread.Sleep(1000000); MassMailWrapper wrap = new MassMailWrapper(em); _logger.LogInformation($"Processing mass email {em.Name} next send=>{wrap.NextRunTime}"); if (wrap.InvalidSchedule) { _logger.LogWarning("Mass mail {0} has wrong schedule definition.", em.Id); continue; } if (!wrap.ShouldSend) { continue; } IMassEmailService meservice = serviceScope.ServiceProvider.GetRequiredService <IMassEmailService>(); wrap.Increment(); _logger.LogWarning($"Scheduling next send for {em.Name} =>{wrap.NextRunTime}"); //if (await meservice.SendMassEmailAsync(comp.Id, em, wrap.NextRunTime)) //{ //} var success = await meservice.SendMassEmailAsync(comp.Id, em, wrap.NextRunTime); // if(!success) // _logger.LogError("SendMassEmailAsync failed"); } } } } catch (Exception ex) { _logger.LogError(ex, "EMailSenderTask error"); } finally { IsRunning = false; } }