Add lock and Fixes

This commit is contained in:
parent 0faf124bef
commit 5db3d3961f
2 changed files with 36 additions and 37 deletions

View File

@ -211,7 +211,7 @@ namespace AircraftPlantBusinessLogic.BusinessLogics
model.Status = newStatus; model.Status = newStatus;
if (newStatus == OrderStatus.Выдан) if (model.Status == OrderStatus.Готов || element.Status == OrderStatus.Ожидание)
{ {
var plane = _planeStorage.GetElement(new PlaneSearchModel { Id = element.PlaneId }); var plane = _planeStorage.GetElement(new PlaneSearchModel { Id = element.PlaneId });
if (plane == null) if (plane == null)
@ -219,27 +219,22 @@ namespace AircraftPlantBusinessLogic.BusinessLogics
_logger.LogWarning("Status change error. Plane not found"); _logger.LogWarning("Status change error. Plane not found");
return false; return false;
} }
if (!CheckSupply(plane, element.Count)) if (!CheckSupply(plane, element.Count))
{ {
_logger.LogWarning("Status change error. Shop is overflowed");
model.Status = OrderStatus.Ожидание; model.Status = OrderStatus.Ожидание;
if (_orderStorage.Update(model) == null) _logger.LogWarning("Status change error. Shop is overflowed");
{ }
_logger.LogWarning("Change status operation failed"); else
} {
return false; model.DateImplement = DateTime.Now;
} }
}
if (model.Status == OrderStatus.Выдан)
{
model.DateImplement = DateTime.Now;
} }
else else
{ {
model.DateImplement = element.DateImplement; model.DateImplement = element.DateImplement;
} }
CheckModel(model, false); CheckModel(model, false);
if (_orderStorage.Update(model) == null) if (_orderStorage.Update(model) == null)
{ {

View File

@ -27,10 +27,13 @@ namespace AircraftPlantBusinessLogic.BusinessLogics
/// </summary> /// </summary>
private readonly Random _rnd; private readonly Random _rnd;
/// <summary> private readonly object orderLock = new object();
/// Бизнес-логика для заказов private readonly object waitingOrderLock = new object();
/// </summary>
private IOrderLogic? _orderLogic; /// <summary>
/// Бизнес-логика для заказов
/// </summary>
private IOrderLogic? _orderLogic;
/// <summary> /// <summary>
/// Конструктор /// Конструктор
@ -58,13 +61,8 @@ namespace AircraftPlantBusinessLogic.BusinessLogics
} }
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят }); var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
if (orders == null || orders.Count == 0)
{
_logger.LogWarning("DoWork. Orders is null or empty");
return;
}
_logger.LogDebug("DoWork for {Count} orders", orders.Count); _logger.LogDebug("DoWork for {Count} orders", orders?.Count);
foreach (var implementer in implementers) foreach (var implementer in implementers)
{ {
Task.Run(() => WorkerWorkAsync(implementer, orders)); Task.Run(() => WorkerWorkAsync(implementer, orders));
@ -95,17 +93,20 @@ namespace AircraftPlantBusinessLogic.BusinessLogics
try try
{ {
_logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id); _logger.LogDebug("DoWork. Worker {Id} try get order {Order}", implementer.Id, order.Id);
// пытаемся назначить заказ на исполнителя // пытаемся назначить заказ на исполнителя
_orderLogic.TakeOrderInWork(new OrderBindingModel lock (orderLock)
{ {
Id = order.Id, _orderLogic.TakeOrderInWork(new OrderBindingModel
ImplementerId = implementer.Id {
}); Id = order.Id,
ImplementerId = implementer.Id
});
}
// делаем работу // делаем работу
Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count); Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count);
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id);
_orderLogic.FinishOrder(new OrderBindingModel _orderLogic.DeliveryOrder(new OrderBindingModel
{ {
Id = order.Id Id = order.Id
}); });
@ -155,7 +156,7 @@ namespace AircraftPlantBusinessLogic.BusinessLogics
// доделываем работу // доделываем работу
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("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id);
_orderLogic.FinishOrder(new OrderBindingModel _orderLogic.DeliveryOrder(new OrderBindingModel
{ {
Id = runOrder.Id Id = runOrder.Id
}); });
@ -203,10 +204,13 @@ namespace AircraftPlantBusinessLogic.BusinessLogics
foreach (var order in orders) foreach (var order in orders)
{ {
_logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id);
_orderLogic.FinishOrder(new OrderBindingModel lock (waitingOrderLock)
{ {
Id = order.Id _orderLogic.DeliveryOrder(new OrderBindingModel
}); {
Id = order.Id
});
}
// отдыхаем // отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));