diff --git a/StandManager.MailProcessor/Program.cs b/StandManager.MailProcessor/Program.cs index deddf56..6a668cd 100644 --- a/StandManager.MailProcessor/Program.cs +++ b/StandManager.MailProcessor/Program.cs @@ -44,4 +44,4 @@ var host = Host.CreateDefaultBuilder(args) using var scope = host.Services.CreateScope(); var processor = scope.ServiceProvider.GetRequiredService(); -processor.Process(); \ No newline at end of file +await processor.Process(); \ No newline at end of file diff --git a/StandManager.Service/Interfaces/IMailQueueService.cs b/StandManager.Service/Interfaces/IMailQueueService.cs index 88e0673..a3df9fd 100644 --- a/StandManager.Service/Interfaces/IMailQueueService.cs +++ b/StandManager.Service/Interfaces/IMailQueueService.cs @@ -6,4 +6,5 @@ namespace StandManager.Service.Interfaces; public interface IMailQueueService : ITService { Task Salva(MailQueue entity); + Task> Salva(List entities); } \ No newline at end of file diff --git a/StandManager.Service/MailQueueService.cs b/StandManager.Service/MailQueueService.cs index fc4a6e7..b0a3516 100644 --- a/StandManager.Service/MailQueueService.cs +++ b/StandManager.Service/MailQueueService.cs @@ -23,4 +23,16 @@ public class MailQueueService : TService, IMailQueueService return entity; } + + public async Task> Salva(List entities) + { + foreach(var entity in entities) + { + if (entity.Id == Guid.Empty) await _unitOfWork.MailQueueRepository.Put(entity); + else _unitOfWork.MailQueueRepository.Update(entity); + } + + await _unitOfWork.Salva(); + return entities; + } } \ No newline at end of file diff --git a/StandManager.Service/RegistrazioneMailProcessor.cs b/StandManager.Service/RegistrazioneMailProcessor.cs index 6390807..44cc67c 100644 --- a/StandManager.Service/RegistrazioneMailProcessor.cs +++ b/StandManager.Service/RegistrazioneMailProcessor.cs @@ -24,55 +24,58 @@ public class RegistrazioneMailProcessor : IMailProcessor foreach (var chunk in mailQueueList.Chunk(size)) { - foreach (var mailQueue in chunk) + var emailTasks = chunk.Select(async mailQueue => { - var email = new Email() - { - From = _config.From, - Body = string.IsNullOrEmpty(mailQueue.Args) ? mailQueue.Body : string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? [""]), - Cc = null, - Subject = mailQueue.Subject, - To = new(){"g.vitari@oaservice.it"} // mailQueue.ToList.Split(";").ToList() - }; - var messageJson = JsonSerializer.Serialize(email); - - // multipart - using var form = new MultipartFormDataContent(); - form.Add( - new StringContent(messageJson, Encoding.UTF8, "application/json"), - "messageJson" - ); - - //Attachments - /*var filePath = @"C:\temp\test.pdf"; - var fileBytes = await File.ReadAllBytesAsync(filePath); - - var fileContent = new ByteArrayContent(fileBytes); - fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf"); - - form.Add( - fileContent, - "attachments", - Path.GetFileName(filePath) - );*/ - try { + var email = new Email() + { + From = _config.From, + Body = string.IsNullOrEmpty(mailQueue.Args) + ? mailQueue.Body + : string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? Array.Empty()), + Cc = null, + Subject = mailQueue.Subject, + To = mailQueue.ToList.Split(";").ToList() // new() { "g.vitari@oaservice.it" } + }; + + var messageJson = JsonSerializer.Serialize(email); + + // multipart + using var form = new MultipartFormDataContent(); + form.Add(new StringContent(messageJson, Encoding.UTF8, "application/json"), "messageJson"); + + //Attachments + /* if (File.Exists(@"C:\temp\test.pdf")) { + var fileBytes = await File.ReadAllBytesAsync(@"C:\temp\test.pdf"); + var fileContent = new ByteArrayContent(fileBytes); + fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf"); + form.Add(fileContent, "attachments", "test.pdf"); + } + */ + var response = await httpClient.PostAsync( _config.ServerAddress, form ); response.EnsureSuccessStatusCode(); mailQueue.Sent = true; - await _managerService.MailQueueService.Salva(mailQueue); + mailQueue.Error = null; + return mailQueue; } catch (Exception e) { mailQueue.Sent = false; mailQueue.Error = e.Message; - await _managerService.MailQueueService.Salva(mailQueue); + return mailQueue; } - } + }); + + // Prendo i risultati dell'invio + var results = await Task.WhenAll(emailTasks); + + // Salvo in blocco le entità + await _managerService.MailQueueService.Salva(results.ToList()); } } } \ No newline at end of file