diff --git a/SoftwareInstallation/AbstractSoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs b/SoftwareInstallation/AbstractSoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs index cffd8d8..507ab76 100644 --- a/SoftwareInstallation/AbstractSoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs +++ b/SoftwareInstallation/AbstractSoftwareInstallationBusinessLogic/BusinessLogic/WorkModeling.cs @@ -15,7 +15,9 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic public class WorkModeling : IWorkProcess { private readonly ILogger _logger; + private readonly Random _rnd; + private IOrderLogic? _orderLogic; public WorkModeling(ILogger logger) @@ -33,10 +35,7 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic _logger.LogWarning("DoWork. Implementers is null"); return; } - 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"); @@ -49,11 +48,6 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic } } - /// - /// Иммитация работы исполнителя - /// - /// - /// private async Task WorkerWorkAsync(ImplementerViewModel implementer, List orders) { if (_orderLogic == null || implementer == null) @@ -61,28 +55,33 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic return; } await RunOrderInWork(implementer); + await Task.Run(() => { foreach (var order in orders) { 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 + if (_orderLogic.TakeOrderInWork(new OrderBindingModel { Id = order.Id, ImplementerId = implementer.Id - }); - // делаем работу - Thread.Sleep(implementer.WorkExperience * _rnd.Next(1000, 10000) * order.Count); - _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); - _orderLogic.FinishOrder(new OrderBindingModel + })) { - Id = order.Id, - }); - // отдыхаем - Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + // делаем работу + Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 1000) * order.Count); + _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, order.Id); + _orderLogic.FinishOrder(new OrderBindingModel + { + Id = order.Id, + ImplementerId = implementer.Id + }); + // отдыхаем + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + } + } // кто-то мог уже перехватить заказ, игнорируем ошибку catch (InvalidOperationException ex) @@ -94,16 +93,11 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic { _logger.LogError(ex, "Error while do work"); throw; - } + } } }); } - /// - /// Ищем заказ, которые уже в работе (вдруг исполнителя прервали) - /// - /// - /// private async Task RunOrderInWork(ImplementerViewModel implementer) { if (_orderLogic == null || implementer == null) @@ -145,5 +139,6 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic } } + } }