- FIX scheduler
- Ottimizzazione funzione invio mail scheduler
This commit is contained in:
@ -44,4 +44,4 @@ var host = Host.CreateDefaultBuilder(args)
|
|||||||
|
|
||||||
using var scope = host.Services.CreateScope();
|
using var scope = host.Services.CreateScope();
|
||||||
var processor = scope.ServiceProvider.GetRequiredService<MailProcessor>();
|
var processor = scope.ServiceProvider.GetRequiredService<MailProcessor>();
|
||||||
processor.Process();
|
await processor.Process();
|
||||||
@ -6,4 +6,5 @@ namespace StandManager.Service.Interfaces;
|
|||||||
public interface IMailQueueService : ITService<MailQueue>
|
public interface IMailQueueService : ITService<MailQueue>
|
||||||
{
|
{
|
||||||
Task<MailQueue> Salva(MailQueue entity);
|
Task<MailQueue> Salva(MailQueue entity);
|
||||||
|
Task<List<MailQueue>> Salva(List<MailQueue> entities);
|
||||||
}
|
}
|
||||||
@ -23,4 +23,16 @@ public class MailQueueService : TService<MailQueue>, IMailQueueService
|
|||||||
|
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<MailQueue>> Salva(List<MailQueue> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -24,55 +24,58 @@ public class RegistrazioneMailProcessor : IMailProcessor
|
|||||||
|
|
||||||
foreach (var chunk in mailQueueList.Chunk(size))
|
foreach (var chunk in mailQueueList.Chunk(size))
|
||||||
{
|
{
|
||||||
foreach (var mailQueue in chunk)
|
var emailTasks = chunk.Select(async mailQueue =>
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var email = new Email()
|
var email = new Email()
|
||||||
{
|
{
|
||||||
From = _config.From,
|
From = _config.From,
|
||||||
Body = string.IsNullOrEmpty(mailQueue.Args) ? mailQueue.Body : string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? [""]),
|
Body = string.IsNullOrEmpty(mailQueue.Args)
|
||||||
|
? mailQueue.Body
|
||||||
|
: string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? Array.Empty<string>()),
|
||||||
Cc = null,
|
Cc = null,
|
||||||
Subject = mailQueue.Subject,
|
Subject = mailQueue.Subject,
|
||||||
To = new(){"g.vitari@oaservice.it"} // mailQueue.ToList.Split(";").ToList()
|
To = mailQueue.ToList.Split(";").ToList() // new() { "g.vitari@oaservice.it" }
|
||||||
};
|
};
|
||||||
|
|
||||||
var messageJson = JsonSerializer.Serialize(email);
|
var messageJson = JsonSerializer.Serialize(email);
|
||||||
|
|
||||||
// multipart
|
// multipart
|
||||||
using var form = new MultipartFormDataContent();
|
using var form = new MultipartFormDataContent();
|
||||||
form.Add(
|
form.Add(new StringContent(messageJson, Encoding.UTF8, "application/json"), "messageJson");
|
||||||
new StringContent(messageJson, Encoding.UTF8, "application/json"),
|
|
||||||
"messageJson"
|
|
||||||
);
|
|
||||||
|
|
||||||
//Attachments
|
//Attachments
|
||||||
/*var filePath = @"C:\temp\test.pdf";
|
/* if (File.Exists(@"C:\temp\test.pdf")) {
|
||||||
var fileBytes = await File.ReadAllBytesAsync(filePath);
|
var fileBytes = await File.ReadAllBytesAsync(@"C:\temp\test.pdf");
|
||||||
|
|
||||||
var fileContent = new ByteArrayContent(fileBytes);
|
var fileContent = new ByteArrayContent(fileBytes);
|
||||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
|
||||||
|
form.Add(fileContent, "attachments", "test.pdf");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
form.Add(
|
|
||||||
fileContent,
|
|
||||||
"attachments",
|
|
||||||
Path.GetFileName(filePath)
|
|
||||||
);*/
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var response = await httpClient.PostAsync(
|
var response = await httpClient.PostAsync(
|
||||||
_config.ServerAddress,
|
_config.ServerAddress,
|
||||||
form
|
form
|
||||||
);
|
);
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
mailQueue.Sent = true;
|
mailQueue.Sent = true;
|
||||||
await _managerService.MailQueueService.Salva(mailQueue);
|
mailQueue.Error = null;
|
||||||
|
return mailQueue;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
mailQueue.Sent = false;
|
mailQueue.Sent = false;
|
||||||
mailQueue.Error = e.Message;
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user