From f0d28c0802d385695575cdd41bea1961b0e0acc1 Mon Sep 17 00:00:00 2001 From: Mikhail Kuznetsov Date: Thu, 1 Jun 2023 16:08:25 +1000 Subject: [PATCH] Services+Worker --- PARR.AIHIT/AIHIT/ISyncher.cs | 7 ++++ PARR.AIHIT/AIHIT/Syncher.cs | 13 +++++-- PARR.AIHIT/AIHIT/SyncherInstaller.cs | 11 ++---- .../20230519051342_AllTbls.Designer.cs | 8 ++-- .../20230601014714_UpdTblHosts.Designer.cs | 8 ++-- .../Migrations/DataContextModelSnapshot.cs | 8 ++-- .../Implementations/JobCategoryService.cs | 24 ++++++++++++ .../Implementations/JobJournalService.cs | 24 ++++++++++++ .../Implementations/JobModeService.cs | 24 ++++++++++++ .../Services/Implementations/JobService.cs | 30 +++++++++++++++ .../Implementations/JobStatusService.cs | 24 ++++++++++++ .../Interfaces/IJobCategoryService.cs | 9 +++++ .../Services/Interfaces/IJobJournalService.cs | 12 ++++++ .../Services/Interfaces/IJobModeService.cs | 9 +++++ PARR.DAL/Services/Interfaces/IJobService.cs | 9 +++++ .../Services/Interfaces/IJobStatusService.cs | 9 +++++ PARR.Worker/PARR.Worker/PARR.Worker.csproj | 1 + PARR.Worker/PARR.Worker/Program.cs | 2 + PARR.Worker/PARR.Worker/Syncher.cs | 38 ------------------- PARR.Worker/PARR.Worker/Worker.cs | 24 +++++++----- PARR.Worker/PARR.Worker/appsettings.json | 3 ++ 21 files changed, 226 insertions(+), 71 deletions(-) create mode 100644 PARR.AIHIT/AIHIT/ISyncher.cs create mode 100644 PARR.DAL/Services/Implementations/JobCategoryService.cs create mode 100644 PARR.DAL/Services/Implementations/JobJournalService.cs create mode 100644 PARR.DAL/Services/Implementations/JobModeService.cs create mode 100644 PARR.DAL/Services/Implementations/JobService.cs create mode 100644 PARR.DAL/Services/Implementations/JobStatusService.cs create mode 100644 PARR.DAL/Services/Interfaces/IJobCategoryService.cs create mode 100644 PARR.DAL/Services/Interfaces/IJobJournalService.cs create mode 100644 PARR.DAL/Services/Interfaces/IJobModeService.cs create mode 100644 PARR.DAL/Services/Interfaces/IJobService.cs create mode 100644 PARR.DAL/Services/Interfaces/IJobStatusService.cs delete mode 100644 PARR.Worker/PARR.Worker/Syncher.cs diff --git a/PARR.AIHIT/AIHIT/ISyncher.cs b/PARR.AIHIT/AIHIT/ISyncher.cs new file mode 100644 index 00000000..77496eb7 --- /dev/null +++ b/PARR.AIHIT/AIHIT/ISyncher.cs @@ -0,0 +1,7 @@ +namespace PARR.AIHIT +{ + public interface ISyncher + { + Task InvokeAsync(); + } +} diff --git a/PARR.AIHIT/AIHIT/Syncher.cs b/PARR.AIHIT/AIHIT/Syncher.cs index 450b5297..865ba557 100644 --- a/PARR.AIHIT/AIHIT/Syncher.cs +++ b/PARR.AIHIT/AIHIT/Syncher.cs @@ -1,18 +1,22 @@ using MailKit.Search; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using PARR.AIHIT.Settings; +using PARR.DAL.Services.Interfaces; using PARR.Mail.Services; namespace PARR.AIHIT { - internal class Syncher + internal class Syncher : ISyncher { private readonly ILogger logger; private readonly IMailService mailService; private readonly SyncherSettings syncherSettings; + private readonly IHostService hostService; - public Syncher(ILogger logger, IMailService mailService, SyncherSettings syncherSettings) + public Syncher(ILogger logger, IMailService mailService, SyncherSettings syncherSettings, IHostService hostService) { + this.hostService = hostService; this.logger = logger; this.mailService = mailService; this.syncherSettings = syncherSettings; @@ -20,6 +24,7 @@ namespace PARR.AIHIT public async Task InvokeAsync() { + var hosts = await hostService.Get().ToListAsync(); var messages = await mailService.CheckMailAsync(SearchQuery.NotSeen); if (!messages.Any()) { @@ -29,9 +34,9 @@ namespace PARR.AIHIT // var filtered = messages.Where(m => includeAddresses.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList(); var filtered = messages.Where(m => syncherSettings.IncludeAddressesArray.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList(); - foreach ( var message in filtered) + foreach (var message in filtered) { - + // } } } diff --git a/PARR.AIHIT/AIHIT/SyncherInstaller.cs b/PARR.AIHIT/AIHIT/SyncherInstaller.cs index 290da3d2..92886e04 100644 --- a/PARR.AIHIT/AIHIT/SyncherInstaller.cs +++ b/PARR.AIHIT/AIHIT/SyncherInstaller.cs @@ -1,23 +1,18 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using PARR.AIHIT.Settings; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PARR.AIHIT { - public class SyncherInstaller + public static class SyncherInstaller { - public void InstallerServices(IServiceCollection services, IConfiguration configuration) + public static void InstallerSyncerServices(this IServiceCollection services, IConfiguration configuration) { var syncherSettings = new SyncherSettings(); configuration.GetSection(nameof(SyncherSettings)).Bind(syncherSettings); services.AddSingleton(syncherSettings); - services.AddTransient(); + services.AddTransient(); } } } diff --git a/PARR.DAL/Migrations/20230519051342_AllTbls.Designer.cs b/PARR.DAL/Migrations/20230519051342_AllTbls.Designer.cs index bc5e2ae5..d102223a 100644 --- a/PARR.DAL/Migrations/20230519051342_AllTbls.Designer.cs +++ b/PARR.DAL/Migrations/20230519051342_AllTbls.Designer.cs @@ -157,7 +157,7 @@ namespace PARR.DAL.Migrations b.ToTable("JobJournals"); }); - modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + modelBuilder.Entity("PARR.DAL.Models.JobModeService", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -234,7 +234,7 @@ namespace PARR.DAL.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("PARR.DAL.Models.JobMode", "JobMode") + b.HasOne("PARR.DAL.Models.JobModeService", "JobModeService") .WithMany("Jobs") .HasForeignKey("JobModeId") .OnDelete(DeleteBehavior.Cascade) @@ -248,7 +248,7 @@ namespace PARR.DAL.Migrations b.Navigation("JobCategorye"); - b.Navigation("JobMode"); + b.Navigation("JobModeService"); b.Navigation("JobType"); }); @@ -295,7 +295,7 @@ namespace PARR.DAL.Migrations b.Navigation("Jobs"); }); - modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + modelBuilder.Entity("PARR.DAL.Models.JobModeService", b => { b.Navigation("Jobs"); }); diff --git a/PARR.DAL/Migrations/20230601014714_UpdTblHosts.Designer.cs b/PARR.DAL/Migrations/20230601014714_UpdTblHosts.Designer.cs index 920eb8bd..99c91458 100644 --- a/PARR.DAL/Migrations/20230601014714_UpdTblHosts.Designer.cs +++ b/PARR.DAL/Migrations/20230601014714_UpdTblHosts.Designer.cs @@ -178,7 +178,7 @@ namespace PARR.DAL.Migrations b.ToTable("JobJournals"); }); - modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + modelBuilder.Entity("PARR.DAL.Models.JobModeService", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -255,7 +255,7 @@ namespace PARR.DAL.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("PARR.DAL.Models.JobMode", "JobMode") + b.HasOne("PARR.DAL.Models.JobModeService", "JobModeService") .WithMany("Jobs") .HasForeignKey("JobModeId") .OnDelete(DeleteBehavior.Cascade) @@ -269,7 +269,7 @@ namespace PARR.DAL.Migrations b.Navigation("JobCategorye"); - b.Navigation("JobMode"); + b.Navigation("JobModeService"); b.Navigation("JobType"); }); @@ -316,7 +316,7 @@ namespace PARR.DAL.Migrations b.Navigation("Jobs"); }); - modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + modelBuilder.Entity("PARR.DAL.Models.JobModeService", b => { b.Navigation("Jobs"); }); diff --git a/PARR.DAL/Migrations/DataContextModelSnapshot.cs b/PARR.DAL/Migrations/DataContextModelSnapshot.cs index 497c793b..8fd1949b 100644 --- a/PARR.DAL/Migrations/DataContextModelSnapshot.cs +++ b/PARR.DAL/Migrations/DataContextModelSnapshot.cs @@ -175,7 +175,7 @@ namespace PARR.DAL.Migrations b.ToTable("JobJournals"); }); - modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + modelBuilder.Entity("PARR.DAL.Models.JobModeService", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -252,7 +252,7 @@ namespace PARR.DAL.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("PARR.DAL.Models.JobMode", "JobMode") + b.HasOne("PARR.DAL.Models.JobModeService", "JobModeService") .WithMany("Jobs") .HasForeignKey("JobModeId") .OnDelete(DeleteBehavior.Cascade) @@ -266,7 +266,7 @@ namespace PARR.DAL.Migrations b.Navigation("JobCategorye"); - b.Navigation("JobMode"); + b.Navigation("JobModeService"); b.Navigation("JobType"); }); @@ -313,7 +313,7 @@ namespace PARR.DAL.Migrations b.Navigation("Jobs"); }); - modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + modelBuilder.Entity("PARR.DAL.Models.JobModeService", b => { b.Navigation("Jobs"); }); diff --git a/PARR.DAL/Services/Implementations/JobCategoryService.cs b/PARR.DAL/Services/Implementations/JobCategoryService.cs new file mode 100644 index 00000000..437eb087 --- /dev/null +++ b/PARR.DAL/Services/Implementations/JobCategoryService.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.Context; +using PARR.DAL.Models; +using PARR.DAL.Services.Abstracts; +using PARR.DAL.Services.Interfaces; + +namespace PARR.DAL.Services.Implementations +{ + internal class JobCategoryService : BaseService, IJobCategoryService + { + private readonly DataContext dataContext; + private readonly ILogger logger; + + public JobCategoryService(DataContext dataContext, ILogger logger) : base(logger) + { + this.dataContext = dataContext; + this.logger = logger; + } + protected override DbSet EntitySet => dataContext.JobCategories; + + protected override DataContext EntitiContext => dataContext; + } +} diff --git a/PARR.DAL/Services/Implementations/JobJournalService.cs b/PARR.DAL/Services/Implementations/JobJournalService.cs new file mode 100644 index 00000000..8519208d --- /dev/null +++ b/PARR.DAL/Services/Implementations/JobJournalService.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.Context; +using PARR.DAL.Models; +using PARR.DAL.Services.Abstracts; +using PARR.DAL.Services.Interfaces; + +namespace PARR.DAL.Services.Implementations +{ + internal class JobJournalService : BaseService, IJobJournalService + { + private readonly DataContext dataContext; + private readonly ILogger logger; + + public JobJournalService(DataContext dataContext, ILogger logger) : base(logger) + { + this.dataContext = dataContext; + this.logger = logger; + } + protected override DbSet EntitySet => dataContext.JobJournals; + + protected override DataContext EntitiContext => dataContext; + } +} diff --git a/PARR.DAL/Services/Implementations/JobModeService.cs b/PARR.DAL/Services/Implementations/JobModeService.cs new file mode 100644 index 00000000..58c06b6e --- /dev/null +++ b/PARR.DAL/Services/Implementations/JobModeService.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.Context; +using PARR.DAL.Models; +using PARR.DAL.Services.Abstracts; +using PARR.DAL.Services.Interfaces; + +namespace PARR.DAL.Services.Implementations +{ + internal class JobModeService : BaseService, IJobModeService + { + private readonly DataContext dataContext; + private readonly ILogger logger; + + public JobModeService(DataContext dataContext, ILogger logger) : base(logger) + { + this.dataContext = dataContext; + this.logger = logger; + } + protected override DbSet EntitySet => dataContext.JobModes; + + protected override DataContext EntitiContext => dataContext; + } +} diff --git a/PARR.DAL/Services/Implementations/JobService.cs b/PARR.DAL/Services/Implementations/JobService.cs new file mode 100644 index 00000000..f8126cd3 --- /dev/null +++ b/PARR.DAL/Services/Implementations/JobService.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.Context; +using PARR.DAL.Models; +using PARR.DAL.Services.Abstracts; +using PARR.DAL.Services.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PARR.DAL.Services.Implementations +{ + internal class JobService : BaseService, IJobService + { + private readonly DataContext dataContext; + private readonly ILogger logger; + + public JobService(DataContext dataContext,ILogger logger):base(logger) + { + this.dataContext = dataContext; + this.logger = logger; + } + protected override DbSet EntitySet => dataContext.Jobs; + + protected override DataContext EntitiContext => dataContext; + + } +} diff --git a/PARR.DAL/Services/Implementations/JobStatusService.cs b/PARR.DAL/Services/Implementations/JobStatusService.cs new file mode 100644 index 00000000..1e6b8020 --- /dev/null +++ b/PARR.DAL/Services/Implementations/JobStatusService.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.Context; +using PARR.DAL.Models; +using PARR.DAL.Services.Abstracts; +using PARR.DAL.Services.Interfaces; + +namespace PARR.DAL.Services.Implementations +{ + internal class JobStatusService : BaseService, IJobStatusService + { + private readonly DataContext dataContext; + private readonly ILogger logger; + + public JobStatusService(DataContext dataContext, ILogger logger) : base(logger) + { + this.dataContext = dataContext; + this.logger = logger; + } + protected override DbSet EntitySet => dataContext.JobStatuses; + + protected override DataContext EntitiContext => dataContext; + } +} diff --git a/PARR.DAL/Services/Interfaces/IJobCategoryService.cs b/PARR.DAL/Services/Interfaces/IJobCategoryService.cs new file mode 100644 index 00000000..cf1defbe --- /dev/null +++ b/PARR.DAL/Services/Interfaces/IJobCategoryService.cs @@ -0,0 +1,9 @@ +using PARR.DAL.Models; +using PARR.DAL.Services.Interfaces.Base; + +namespace PARR.DAL.Services.Interfaces +{ + internal interface IJobCategoryService : IBaseService + { + } +} diff --git a/PARR.DAL/Services/Interfaces/IJobJournalService.cs b/PARR.DAL/Services/Interfaces/IJobJournalService.cs new file mode 100644 index 00000000..523b38bb --- /dev/null +++ b/PARR.DAL/Services/Interfaces/IJobJournalService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PARR.DAL.Services.Interfaces +{ + internal interface IJobJournalService + { + } +} diff --git a/PARR.DAL/Services/Interfaces/IJobModeService.cs b/PARR.DAL/Services/Interfaces/IJobModeService.cs new file mode 100644 index 00000000..312230fc --- /dev/null +++ b/PARR.DAL/Services/Interfaces/IJobModeService.cs @@ -0,0 +1,9 @@ +using PARR.DAL.Models; +using PARR.DAL.Services.Interfaces.Base; + +namespace PARR.DAL.Services.Interfaces +{ + internal interface IJobModeService : IBaseService + { + } +} diff --git a/PARR.DAL/Services/Interfaces/IJobService.cs b/PARR.DAL/Services/Interfaces/IJobService.cs new file mode 100644 index 00000000..3343d2d8 --- /dev/null +++ b/PARR.DAL/Services/Interfaces/IJobService.cs @@ -0,0 +1,9 @@ +using PARR.DAL.Models; +using PARR.DAL.Services.Interfaces.Base; + +namespace PARR.DAL.Services.Interfaces +{ + public interface IJobService : IBaseService + { + } +} diff --git a/PARR.DAL/Services/Interfaces/IJobStatusService.cs b/PARR.DAL/Services/Interfaces/IJobStatusService.cs new file mode 100644 index 00000000..4fe1eded --- /dev/null +++ b/PARR.DAL/Services/Interfaces/IJobStatusService.cs @@ -0,0 +1,9 @@ +using PARR.DAL.Models; +using PARR.DAL.Services.Interfaces.Base; + +namespace PARR.DAL.Services.Interfaces +{ + internal interface IJobStatusService : IBaseService + { + } +} diff --git a/PARR.Worker/PARR.Worker/PARR.Worker.csproj b/PARR.Worker/PARR.Worker/PARR.Worker.csproj index 353e86c9..873749d8 100644 --- a/PARR.Worker/PARR.Worker/PARR.Worker.csproj +++ b/PARR.Worker/PARR.Worker/PARR.Worker.csproj @@ -17,6 +17,7 @@ + diff --git a/PARR.Worker/PARR.Worker/Program.cs b/PARR.Worker/PARR.Worker/Program.cs index 7cb64e85..895c53af 100644 --- a/PARR.Worker/PARR.Worker/Program.cs +++ b/PARR.Worker/PARR.Worker/Program.cs @@ -1,3 +1,4 @@ +using PARR.AIHIT; using PARR.DAL; using PARR.Mail; using PARR.Worker; @@ -8,6 +9,7 @@ IHost host = Host.CreateDefaultBuilder(args) { services.InstallMailServices(hostContext.Configuration); services.InstallDalServices(hostContext.Configuration); + services.InstallerSyncerServices(hostContext.Configuration); services.AddHostedService(); }) diff --git a/PARR.Worker/PARR.Worker/Syncher.cs b/PARR.Worker/PARR.Worker/Syncher.cs deleted file mode 100644 index da7e364f..00000000 --- a/PARR.Worker/PARR.Worker/Syncher.cs +++ /dev/null @@ -1,38 +0,0 @@ -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 logger; - private readonly IMailService mailService; - private readonly SyncherSettings aIHITSyncherSettings; - - public Syncher(ILogger 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(); - } - } -} diff --git a/PARR.Worker/PARR.Worker/Worker.cs b/PARR.Worker/PARR.Worker/Worker.cs index 358822ee..69b9c6ba 100644 --- a/PARR.Worker/PARR.Worker/Worker.cs +++ b/PARR.Worker/PARR.Worker/Worker.cs @@ -1,17 +1,16 @@ -using Microsoft.EntityFrameworkCore; -using PARR.DAL.Services.Interfaces; +using PARR.AIHIT; namespace PARR.Worker { public class Worker : BackgroundService { private readonly ILogger logger; - private readonly IHostService hostService; + private readonly IServiceProvider serviceProvider; - public Worker(ILogger logger, IHostService hostService) + public Worker(ILogger logger, IServiceProvider serviceProvider) { this.logger = logger; - this.hostService = hostService; + this.serviceProvider = serviceProvider; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) @@ -19,12 +18,19 @@ namespace PARR.Worker logger.LogInformation("--- Сервис запущен! ---"); while (!stoppingToken.IsCancellationRequested) { - var hosts = await hostService.Get().ToListAsync(); - foreach ( var host in hosts ) { logger.LogInformation($"{host.HostName}"); } - // syncher.InvokeAsync(); + using var scope = serviceProvider.CreateScope(); + var services = scope.ServiceProvider; + var syncer = services.GetService(); + + if (syncer != null) await syncer.InvokeAsync(); + + //var hosts = await hostService.Get().ToListAsync(); + //foreach ( var host in hosts ) { logger.LogInformation($"{host.HostName}"); } + + //await attachmentUploader.UploadAsync(); //logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); - await Task.Delay(60000, stoppingToken); + await Task.Delay(60 * 1000, stoppingToken); } } } diff --git a/PARR.Worker/PARR.Worker/appsettings.json b/PARR.Worker/PARR.Worker/appsettings.json index a2b4945d..607cdc52 100644 --- a/PARR.Worker/PARR.Worker/appsettings.json +++ b/PARR.Worker/PARR.Worker/appsettings.json @@ -1,4 +1,7 @@ { + "ConnectionStrings": { + "DefaultConnection": "Server=10.99.253.184;Database=parr;User Id=app_parr; Password=PosdfkhT&)%sdfligL&%5546;" + }, "Logging": { "LogLevel": { "Default": "Information",