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