Syncer АИХ ИТ

This commit is contained in:
Mikhail Kuznetsov
2023-06-01 12:30:38 +10:00
parent e34037267c
commit 118cd280df
27 changed files with 953 additions and 8 deletions

View File

@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-PARR.Worker-87bfa085-3c62-4b97-b25f-bdece8fc03df</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PARR.DAL\PARR.DAL.csproj" />
<ProjectReference Include="..\..\PARR.Mail\PARR.Mail\PARR.Mail.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,22 @@
using PARR.DAL;
using PARR.Mail;
using PARR.Worker;
using Serilog;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.InstallMailServices(hostContext.Configuration);
services.InstallDalServices(hostContext.Configuration);
services.AddHostedService<Worker>();
})
.UseSerilog((hostContext, services, config) =>
{
config
.WriteTo.Console()
.ReadFrom.Configuration(hostContext.Configuration);
})
.Build();
host.Run();

View File

@@ -0,0 +1,11 @@
{
"profiles": {
"PARR.Worker": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,10 @@
namespace PARR.Worker.Settings
{
internal class SyncherSettings
{
public int OccursEvery { get; set; }
public string? IncludeAddresses { get; set; }
public List<string> IncludeAddressesArray => IncludeAddresses == null ? new List<string>() : IncludeAddresses.Split(",").ToList();
}
}

View File

@@ -0,0 +1,38 @@
using MailKit.Search;
using PARR.Mail.Services;
using PARR.Worker.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PARR.Worker
{
internal class Syncher
{
private readonly ILogger<Worker> logger;
private readonly IMailService mailService;
private readonly SyncherSettings aIHITSyncherSettings;
public Syncher(ILogger<Worker> logger, IMailService mailService, SyncherSettings aIHITSyncherSettings)
{
this.logger = logger;
this.mailService = mailService;
this.aIHITSyncherSettings = aIHITSyncherSettings;
}
public async Task InvokeAsync()
{
var messages = await mailService.CheckMailAsync(SearchQuery.NotSeen);
if (!messages.Any())
{
return;
}
//var includeAddresses = aIHITSyncherSettings.IncludeAddresses.Any() ? aIHITSyncherSettings.IncludeAddresses.Split(",") : null;
// var filtered = messages.Where(m => includeAddresses.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList();
var filtered = messages.Where(m => aIHITSyncherSettings.IncludeAddressesArray.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList();
}
}
}

View File

@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using PARR.DAL.Services.Interfaces;
namespace PARR.Worker
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> logger;
private readonly IHostService hostService;
public Worker(ILogger<Worker> logger, IHostService hostService)
{
this.logger = logger;
this.hostService = hostService;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("--- Сервис запущен! ---");
while (!stoppingToken.IsCancellationRequested)
{
var hosts = await hostService.Get().ToListAsync();
foreach ( var host in hosts ) { logger.LogInformation($"{host.HostName}"); }
// syncher.InvokeAsync();
//await attachmentUploader.UploadAsync();
//logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
await Task.Delay(60000, stoppingToken);
}
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@@ -0,0 +1,38 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Debug"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log/log-.txt",
"rollingInterval": "Day"
}
}
]
},
"MailSettings": {
"Host": "uc.dvgd.rzd",
"Password": "Robin123",
"Port": 143,
"SSL": false,
"UserName": "IVC_Robot"
},
"AIHITSyncherSettings": {
"OccursEvery": 60000,
"IncludeAddresses": "aihit@gvc.rzd"
}
}