Worker
This commit is contained in:
@@ -57,6 +57,7 @@ namespace PARR.AIHIT
|
||||
{
|
||||
await GetAppTypesAsync();
|
||||
|
||||
|
||||
var messages = await mailService.CheckMailAsync(SearchQuery.NotSeen);
|
||||
if (!messages.Any())
|
||||
{
|
||||
@@ -65,6 +66,7 @@ namespace PARR.AIHIT
|
||||
var filtered = messages.Where(m => syncherSettings.IncludeAddressesArray.Any(i => m.Envelope.From.Mailboxes.FirstOrDefault()?.Address == i)).ToList();
|
||||
var lastMessage = filtered.OrderByDescending(f => f.Date).First();
|
||||
var attachments = await mailService.GetAttachmentsAsync(lastMessage);
|
||||
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
if (attachment is MessagePart)
|
||||
|
||||
@@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace PARR.DAL.Models
|
||||
{
|
||||
[Table("ApplicationsinHosts")]
|
||||
[Table("ApplicationsInHosts")]
|
||||
public class ApplicationInHost : IBase
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
@@ -16,8 +16,4 @@
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Contracts\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace PARR.Mail.Services
|
||||
public interface IMailService
|
||||
{
|
||||
Task<IList<IMessageSummary>> CheckMailAsync(SearchQuery query);
|
||||
Task<MimeEntity> GetBodyPartAsync(IMessageSummary messageSummary);
|
||||
Task<MimeEntity?> GetBodyPartAsync(IMessageSummary messageSummary);
|
||||
Task<List<MimeEntity>> GetAttachmentsAsync(IMessageSummary messageSummary);
|
||||
Task<string> GetBodyTextAsync(IMessageSummary messageSummary);
|
||||
Task SetAsSeenAsync(IMessageSummary messageSummary);
|
||||
|
||||
@@ -35,42 +35,75 @@ namespace PARR.Mail.Services
|
||||
return items;
|
||||
}
|
||||
|
||||
public async Task<MimeEntity> GetBodyPartAsync(IMessageSummary messageSummary)
|
||||
public async Task<MimeEntity?> GetBodyPartAsync(IMessageSummary messageSummary)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var client = await GetClient();
|
||||
|
||||
return await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Ошибка получения тела письма");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<List<MimeEntity>> GetAttachmentsAsync(IMessageSummary messageSummary)
|
||||
{
|
||||
using var client = await GetClient();
|
||||
|
||||
List<MimeEntity> result = new List<MimeEntity>();
|
||||
|
||||
try
|
||||
{
|
||||
using var client = await GetClient();
|
||||
foreach (var attachment in messageSummary.Attachments)
|
||||
{
|
||||
result.Add(await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, attachment));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Ошибка получения тела письма");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<string> GetBodyTextAsync(IMessageSummary messageSummary)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
using var client = await GetClient();
|
||||
|
||||
return ((TextPart)await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody)).Text;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Ошибка содержания письма");
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public async Task SetAsSeenAsync(IMessageSummary messageSummary)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var client = await GetClient();
|
||||
|
||||
await client.Inbox.AddFlagsAsync(messageSummary.UniqueId, MessageFlags.Seen, false);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, "Не удалось сделать письмо прочитанным");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<ImapClient> GetClient()
|
||||
{
|
||||
var client = new ImapClient();
|
||||
try
|
||||
{
|
||||
await client.ConnectAsync(mailSettings.Host, mailSettings.Port, SecureSocketOptions.None);
|
||||
await client.AuthenticateAsync(mailSettings.UserName, mailSettings.Password);
|
||||
await client.Inbox.OpenAsync(FolderAccess.ReadWrite);
|
||||
@@ -78,7 +111,10 @@ namespace PARR.Mail.Services
|
||||
// игнорить сертификат
|
||||
client.CheckCertificateRevocation = false;
|
||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||
|
||||
}
|
||||
catch(Exception e) {
|
||||
logger.LogError(e, "Не удалось создать почтовый клиент");
|
||||
}
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
{
|
||||
public class WorkerSettings
|
||||
{
|
||||
public int OccursEvery { get; set; }
|
||||
public TimeSpan OccursEvery { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace PARR.Worker
|
||||
logger.LogInformation("--- Сервис запущен! ---");
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
logger.LogInformation("--- --- --- --- Начало синхронизации --- --- --- ---");
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var services = scope.ServiceProvider;
|
||||
var syncer = services.GetService<ISyncher>();
|
||||
@@ -36,7 +37,12 @@ namespace PARR.Worker
|
||||
|
||||
//await attachmentUploader.UploadAsync();
|
||||
//logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
await Task.Delay(workerSettings.OccursEvery, stoppingToken);
|
||||
logger.LogInformation("=== === === === Конец синхронизации === === === ===");
|
||||
|
||||
|
||||
var mills = (int?) workerSettings?.OccursEvery.TotalMilliseconds ?? 60*1000;
|
||||
await Task.Delay(mills, stoppingToken);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
"IncludeAddresses": "aihit@gvc.rzd"
|
||||
},
|
||||
"WorkerSettings": {
|
||||
"OccursEvery": 60000
|
||||
"OccursEvery": "00:10:00.0"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user