Compare commits

..

2 Commits

Author SHA1 Message Date
1184346296 wm fx 2023-07-24 17:06:12 +04:00
8dab38dbeb fix 2023-06-03 02:15:16 +04:00

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,31 @@ 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
});
}
}
// кто-то мог уже перехватить заказ, игнорируем ошибку
catch (InvalidOperationException ex)
@ -94,16 +91,13 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic
{
_logger.LogError(ex, "Error while do work");
throw;
}
}
// отдыхаем
Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100));
}
});
}
/// <summary>
/// Ищем заказ, которые уже в работе (вдруг исполнителя прервали)
/// </summary>
/// <param name="implementer"></param>
/// <returns></returns>
private async Task RunOrderInWork(ImplementerViewModel implementer)
{
if (_orderLogic == null || implementer == null)
@ -144,6 +138,5 @@ namespace AbstractSoftwareInstallationBusinessLogic.BusinessLogic
throw;
}
}
}
}