feat(dal, templateDistributor): шаблон для templateDistributor
This commit is contained in:
@@ -13,10 +13,9 @@ namespace PARR.BLL.Services.Implementations
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
|
||||
private DateOnly GetEndPeriodDate(DateOnly startPeriod, DistributionPeriodTypeEnum periodType, string distributionPeriod)
|
||||
{
|
||||
//TODO: подходит для NextRun
|
||||
|
||||
switch (periodType)
|
||||
{
|
||||
case (DistributionPeriodTypeEnum.Day):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.Constants;
|
||||
using PARR.DAL.Contracts;
|
||||
using PARR.DAL.DomainModels;
|
||||
using PARR.DAL.Extensions;
|
||||
@@ -322,5 +323,43 @@ namespace PARR.DAL.TransformServices
|
||||
|
||||
calcDay = calcDay.AddDays(daysOffset);
|
||||
}
|
||||
|
||||
|
||||
public DateTimeOffset GetNextDateForDistributionRun(DateTimeOffset lastRun, DistributionPeriodTypeEnum periodType, string distributionPeriod)
|
||||
{
|
||||
switch (periodType)
|
||||
{
|
||||
case (DistributionPeriodTypeEnum.Day):
|
||||
return lastRun.AddDays(ParseInt(distributionPeriod));
|
||||
|
||||
case (DistributionPeriodTypeEnum.Month):
|
||||
return lastRun.AddMonths(ParseInt(distributionPeriod));
|
||||
|
||||
case (DistributionPeriodTypeEnum.Year):
|
||||
return lastRun.AddYears(ParseInt(distributionPeriod));
|
||||
|
||||
//case (DistributionPeriodTypeEnum.TimeSpan):
|
||||
// var timeSpan = ParseTimeSpan(distributionPeriod);
|
||||
// return timeSpan.Days > 0 ? startPeriod.AddDays();
|
||||
}
|
||||
|
||||
|
||||
//return startPeriod.AddDays(duration.Days).AddDays(-1);
|
||||
return lastRun;
|
||||
}
|
||||
|
||||
|
||||
private int ParseInt(string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
return int.Parse(value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"{nameof(this.GetType)}, получение конца периода. Не смог распарсить string в int для значения {value}.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using PARR.DAL.DomainModels;
|
||||
using PARR.Constants;
|
||||
using PARR.DAL.DomainModels;
|
||||
|
||||
namespace PARR.DAL.TransformServices
|
||||
{
|
||||
@@ -38,5 +39,15 @@ namespace PARR.DAL.TransformServices
|
||||
/// <param name="lastRun"></param>
|
||||
/// <returns></returns>
|
||||
Task<List<DateTimeOffset>> GetNextScheduleAsync(Guid applicationInWorkId, DateTimeOffset lastRun);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получить следующую дату срабатывания в указанном периоде
|
||||
/// </summary>
|
||||
/// <param name="lastRun"></param>
|
||||
/// <param name="periodType"></param>
|
||||
/// <param name="distributionPeriod"></param>
|
||||
/// <returns></returns>
|
||||
DateTimeOffset GetNextDateForDistributionRun(DateTimeOffset lastRun, DistributionPeriodTypeEnum periodType, string distributionPeriod);
|
||||
}
|
||||
}
|
||||
|
||||
8
PARR.TemplateDistributor/IMqTemplateDistributor.cs
Normal file
8
PARR.TemplateDistributor/IMqTemplateDistributor.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace PARR.TemplateDistributor
|
||||
{
|
||||
public interface IMqTemplateDistributor
|
||||
{
|
||||
void Start();
|
||||
void Stop();
|
||||
}
|
||||
}
|
||||
6
PARR.TemplateDistributor/ITemplateDistributor.cs
Normal file
6
PARR.TemplateDistributor/ITemplateDistributor.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PARR.TemplateDistributor
|
||||
{
|
||||
public interface ITemplateDistributor
|
||||
{
|
||||
}
|
||||
}
|
||||
9
PARR.TemplateDistributor/MqTemplateDistributor.cs
Normal file
9
PARR.TemplateDistributor/MqTemplateDistributor.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace PARR.TemplateDistributor
|
||||
{
|
||||
internal class MqTemplateDistributor : IMqTemplateDistributor
|
||||
{
|
||||
public void Start() { }
|
||||
|
||||
public void Stop() { }
|
||||
}
|
||||
}
|
||||
39
PARR.TemplateDistributor/TemplateDistributor.cs
Normal file
39
PARR.TemplateDistributor/TemplateDistributor.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.DAL.Models;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.TemplateDistributor
|
||||
{
|
||||
internal class TemplateDistributor : ITemplateDistributor
|
||||
{
|
||||
private readonly IApplicationsInWorkService applicationsInWorkService;
|
||||
|
||||
public TemplateDistributor(IApplicationsInWorkService applicationsInWorkService)
|
||||
{
|
||||
this.applicationsInWorkService = applicationsInWorkService;
|
||||
}
|
||||
|
||||
|
||||
//TODO: AppInWorkId - перераспределение, очередь
|
||||
//public async Task
|
||||
// распределить?
|
||||
// не распределить, NextRun = appInWork.ReferenceDate
|
||||
|
||||
|
||||
public async Task<List<Template>> DistributeTemplateForPeriodAsync(List<Template> templates, Guid applicationInWorkId)
|
||||
{
|
||||
//TODO: ЗАГУЛШКА переделать!
|
||||
|
||||
var appInWork = await applicationsInWorkService.Get().FirstOrDefaultAsync(t => t.Id == applicationInWorkId);
|
||||
if (appInWork == null)
|
||||
{
|
||||
throw new Exception("ERROROORORO!!!");
|
||||
}
|
||||
|
||||
|
||||
templates.ForEach(t => t.NextRun = appInWork.ReferenceDate);
|
||||
|
||||
return templates;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,8 @@ namespace PARR.TemplateDistributor
|
||||
configuration.GetSection(nameof(MqSettings)).Bind(mqSettings);
|
||||
services.AddSingleton(mqSettings);
|
||||
|
||||
//services.AddTransient<ITemplateSyncer, TemplateMQSyncer>();
|
||||
services.AddTransient<ITemplateDistributor, TemplateDistributor>();
|
||||
services.AddTransient<IMqTemplateDistributor, MqTemplateDistributor>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
using PARR.TemplateDistributor;
|
||||
|
||||
namespace PARR.TemplateDistributorWorker
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
private readonly IMqTemplateDistributor mqTemplateDistributor;
|
||||
|
||||
public Worker(ILogger<Worker> logger)
|
||||
public Worker(ILogger<Worker> logger, IMqTemplateDistributor mqTemplateDistributor)
|
||||
{
|
||||
_logger = logger;
|
||||
this.mqTemplateDistributor = mqTemplateDistributor;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
//while (!stoppingToken.IsCancellationRequested)
|
||||
//{
|
||||
// _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
// await Task.Delay(1000, stoppingToken);
|
||||
//}
|
||||
mqTemplateDistributor.Start();
|
||||
}
|
||||
|
||||
public override Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
mqTemplateDistributor.Stop();
|
||||
|
||||
return base.StopAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user