This commit is contained in:
devil_1nc 2023-06-03 02:15:16 +04:00
parent c0c09b59ab
commit 8dab38dbeb

View File

@ -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<WorkModeling> 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
}
}
/// <summary>
/// Иммитация работы исполнителя
/// </summary>
/// <param name="implementer"></param>
/// <param name="orders"></param>
private async Task WorkerWorkAsync(ImplementerViewModel implementer, List<OrderViewModel> 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;
}
}
}
});
}
/// <summary>
/// Ищем заказ, которые уже в работе (вдруг исполнителя прервали)
/// </summary>
/// <param name="implementer"></param>
/// <returns></returns>
private async Task RunOrderInWork(ImplementerViewModel implementer)
{
if (_orderLogic == null || implementer == null)
@ -145,5 +139,6 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic
}
}
}
}