Mango.Nop.Plugins/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs

52 lines
2.0 KiB
C#

using AyCode.Utils.Extensions;
using Mango.Nop.Services;
using Microsoft.AspNetCore.SignalR;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Data;
using Nop.Plugin.Misc.AuctionPlugin.Hubs;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Services.Customers;
using Nop.Services.Logging;
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public class AuctionBackgroundService : MgBackgroundServiceBase
{
private readonly ILogger _logger;
private readonly IHubContext<AuctionHub> _auctionHubContext;
private readonly ILockService _lockService;
private readonly Customer _auctionSystemCustomer;
public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext<AuctionHub> auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository<Customer> customerService) : base(logger, service)
{
_logger = logger;
_auctionHubContext = auctionHubContext;
_lockService = lockService;
_auctionSystemCustomer = customerService.Table.FirstOrDefault(x => x.Email == "builtin@background_task_auction.com");
if (_auctionSystemCustomer == null)
_logger.Error($"AuctionBackgroundService.AuctionBackgroundService(); _auctionSystemCustomer == null;", null, null);
}
protected override async Task OnExecuteAsync()
{
await Task.Delay(15000); //Az elejére kell tenni! ha exception lenne, akkor ne kezdje el darálni... - J.
if (_auctionSystemCustomer == null) return;
//await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Before lock; ", null, _auctionSystemCustomer);
using (await _lockService.SemaphoreSlim.UseWaitAsync())
{
//var auctionDto = auctionService.GetProductToAuctionDtosByAuctionId()
//await auctionHubContext.Clients.All.SendAsync("send", new MessageWrapper());
await _auctionHubContext.Clients.All.SendAsync("OnDateTimeReceive", DateTime.Now.ToString("G"));
}
}
}