починила, сдача

This commit is contained in:
malimova 2024-05-23 15:09:31 +04:00
parent d3d8d0e340
commit 83e8f6eff2
3 changed files with 167 additions and 155 deletions

View File

@ -17,27 +17,11 @@ namespace ConfectioneryBusinessLogic
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage; private readonly IOrderStorage _orderStorage;
static readonly object _locker = new object();
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage) public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{ {
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
}
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
} }
public List<OrderViewModel>? ReadList(OrderSearchModel? model) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{ {
@ -67,9 +51,12 @@ namespace ConfectioneryBusinessLogic
} }
public bool TakeOrderInWork(OrderBindingModel model) public bool TakeOrderInWork(OrderBindingModel model)
{
lock (_locker)
{ {
return ChangeStatus(model, OrderStatus.Выполняется); return ChangeStatus(model, OrderStatus.Выполняется);
} }
}
public bool FinishOrder(OrderBindingModel model) public bool FinishOrder(OrderBindingModel model)
{ {
@ -93,7 +80,7 @@ namespace ConfectioneryBusinessLogic
} }
if (model.Count <= 0) if (model.Count <= 0)
{ {
throw new ArgumentException("Колличество кондитерских изделий в заказе не может быть меньше 1", nameof(model.Count)); throw new ArgumentException("Колличество суши в заказе не может быть меньше 1", nameof(model.Count));
} }
if (model.Sum <= 0) if (model.Sum <= 0)
{ {
@ -103,7 +90,7 @@ namespace ConfectioneryBusinessLogic
{ {
throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}"); throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}");
} }
_logger.LogInformation("Pastry. PastryId:{PastryId}.Count:{Count}.Sum:{Sum}Id:{Id}", _logger.LogInformation("Sushi. SushiId:{SushiId}.Count:{Count}.Sum:{Sum}Id:{Id}",
model.PastryId, model.Count, model.Sum, model.Id); model.PastryId, model.Count, model.Sum, model.Id);
} }
@ -116,7 +103,7 @@ namespace ConfectioneryBusinessLogic
}); });
if (element == null) if (element == null)
{ {
throw new ArgumentNullException(nameof(element)); throw new InvalidOperationException(nameof(element));
} }
model.DateCreate = element.DateCreate; model.DateCreate = element.DateCreate;
model.PastryId = element.PastryId; model.PastryId = element.PastryId;
@ -144,7 +131,26 @@ namespace ConfectioneryBusinessLogic
return true; return true;
} }
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus); _logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus);
throw new ArgumentException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}"); throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
}
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
} }
} }
} }

View File

@ -20,9 +20,9 @@ namespace ConfectioneryBusinessLogic.BusinessLogics
private IOrderLogic? _orderLogic; private IOrderLogic? _orderLogic;
public WorkModeling(ILogger<WorkModeling> logger) public WorkModeling(ILogger<WorkModeling> Logger)
{ {
_logger = logger; _logger = Logger;
_rnd = new Random(1000); _rnd = new Random(1000);
} }
@ -48,87 +48,93 @@ namespace ConfectioneryBusinessLogic.BusinessLogics
} }
} }
private async Task WorkerWorkAsync(ImplementerViewModel implementer, List<OrderViewModel> orders) private async Task WorkerWorkAsync(ImplementerViewModel Implementer, List<OrderViewModel> Orders)
{ {
if (_orderLogic == null || implementer == null) if (_orderLogic == null || Implementer == null)
{ {
return; return;
} }
await RunOrderInWork(implementer);
await RunOrderInWork(Implementer);
await Task.Run(() => await Task.Run(() =>
{ {
foreach (var order in orders) foreach (var Order in Orders)
{ {
try try
{ {
_logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id); _logger.LogDebug("WorkerWorkAsync. Worker {Id} try get order {Order}", Implementer.Id, Order.Id);
// пытаемся назначить заказ на исполнителя bool AcquireResult = _orderLogic.TakeOrderInWork(new OrderBindingModel
_orderLogic.TakeOrderInWork(new OrderBindingModel
{ {
Id = order.Id, Id = Order.Id,
ImplementerId = implementer.Id ImplementerId = Implementer.Id
}); });
// делаем работу
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count); if (!AcquireResult)
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); {
_logger.LogDebug("WorkerWorkAsync. Worker {Id} tried to get order {Order} but it's already acquired by other worker", Implementer.Id, Order.Id);
continue;
}
Thread.Sleep(Implementer.WorkExperience * _rnd.Next(100, 1000) * Order.Count);
_logger.LogDebug("WorkerWorkAsync. Worker {Id} finish order {Order}", Implementer.Id, Order.Id);
_orderLogic.FinishOrder(new OrderBindingModel _orderLogic.FinishOrder(new OrderBindingModel
{ {
Id = order.Id Id = Order.Id
}); });
Thread.Sleep(Implementer.Qualification * _rnd.Next(10, 100));
} }
// кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
_logger.LogWarning(ex, "Error try get work"); _logger.LogWarning(ex, "Error try get work");
} }
// заканчиваем выполнение имитации в случае иной ошибки
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error while do work"); _logger.LogError(ex, "Error while do work");
throw;
} }
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
} }
}); });
} }
private async Task RunOrderInWork(ImplementerViewModel implementer) private async Task RunOrderInWork(ImplementerViewModel Implementer)
{
if (_orderLogic == null || implementer == null)
{ {
if (_orderLogic == null || Implementer == null)
return; return;
}
try try
{ {
var runOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel var RunOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel
{ {
ImplementerId = implementer.Id, ImplementerId = Implementer.Id,
Status = OrderStatus.Выполняется Status = OrderStatus.Выполняется
})); }));
if (runOrder == null)
if (RunOrder == null)
{ {
return; return;
} }
_logger.LogDebug("DoWork. Worker {Id} back to order {Order}", implementer.Id, runOrder.Id); _logger.LogDebug("RunOrderInWork. Worker {Id} back to order {Order}", Implementer.Id, RunOrder.Id);
// доделываем работу Thread.Sleep(Implementer.WorkExperience * _rnd.Next(100, 300) * RunOrder.Count);
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) * runOrder.Count);
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id); _logger.LogDebug("RunOrderInWork. Worker {Id} finish order {Order}", Implementer.Id, RunOrder.Id);
_orderLogic.FinishOrder(new OrderBindingModel _orderLogic.FinishOrder(new OrderBindingModel
{ {
Id = runOrder.Id Id = RunOrder.Id
}); });
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); Thread.Sleep(Implementer.Qualification * _rnd.Next(10, 100));
} }
// заказа может не быть, просто игнорируем ошибку
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
_logger.LogWarning(ex, "Error try get work"); _logger.LogWarning(ex, "Error try get work");
} }
// а может возникнуть иная ошибка, тогда просто заканчиваем выполнение имитации
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error while do work"); _logger.LogError(ex, "Error while do work");