This commit is contained in:
Adam 2025-01-05 14:24:13 +01:00
commit e6cfe35cc2
3 changed files with 29 additions and 24 deletions

View File

@ -31,7 +31,7 @@ public class AuctionDbTable : MgDbTableBase<Auction>
var utcNow = DateTime.UtcNow; var utcNow = DateTime.UtcNow;
return GetAllAuctions() return GetAllAuctions()
.Where(x => x.AuctionType == AuctionType.AutomaticAll && x.StartDateUtc <= utcNow && (!x.Closed || (x.Closed && x.EndDateUtc >= utcNow))) .Where(x => x.AuctionType == AuctionType.AutomaticAll && x.StartDateUtc <= utcNow && x.StartDateUtc < x.EndDateUtc && (!x.Closed || (x.Closed && x.EndDateUtc >= utcNow)))
.OrderByDescending(x => x.StartDateUtc); .OrderByDescending(x => x.StartDateUtc);
} }
} }

View File

@ -2,62 +2,59 @@
using AyCode.Utils.Extensions; using AyCode.Utils.Extensions;
using Mango.Nop.Services; using Mango.Nop.Services;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Nop.Core;
using Nop.Core.Domain.Customers; using Nop.Core.Domain.Customers;
using Nop.Data; using Nop.Data;
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums; using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using Nop.Plugin.Misc.AuctionPlugin.Hubs; using Nop.Plugin.Misc.AuctionPlugin.Hubs;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages; using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Services.Customers;
using Nop.Services.Logging; using Nop.Services.Logging;
using System.Collections.Generic;
namespace Nop.Plugin.Misc.AuctionPlugin.Services; namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public class AuctionBackgroundService : MgBackgroundServiceBase public class AuctionBackgroundService : MgBackgroundServiceBase, IBackgroundService
{ {
private const int WARNING_STATUS_INTERVAL_SECOND = 15; private const int WARNING_STATUS_INTERVAL_SECOND = 15;
private const string BACKGROUND_TASK_AUCTION_USER_EMAIL = "builtin@background_task_auction.com";
private readonly ILogger _logger;
private readonly IHubContext<AuctionHub> _auctionHubContext; private readonly IHubContext<AuctionHub> _auctionHubContext;
private readonly ILockService _lockService; private readonly ILockService _lockService;
private readonly AuctionService _auctionService; private readonly AuctionService _auctionService;
private readonly Customer _auctionSystemCustomer; private readonly Customer _auctionSystemCustomer;
public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext<AuctionHub> auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository<Customer> customerService) : base(logger, service) public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext<AuctionHub> auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository<Customer> customerService)
: base(logger, service, WARNING_STATUS_INTERVAL_SECOND * 1000)
{ {
_logger = logger; //ExecuteIntervalMs = WARNING_STATUS_INTERVAL_SECOND * 1000;
_auctionHubContext = auctionHubContext;
_lockService = lockService; _lockService = lockService;
_auctionService = auctionService; _auctionService = auctionService;
_auctionHubContext = auctionHubContext;
_auctionSystemCustomer = customerService.Table.FirstOrDefault(x => x.Email == "builtin@background_task_auction.com"); _auctionSystemCustomer = customerService.Table.FirstOrDefault(x => x.Email == BACKGROUND_TASK_AUCTION_USER_EMAIL);
if (_auctionSystemCustomer == null) if (_auctionSystemCustomer == null)
_logger.Error($"AuctionBackgroundService.AuctionBackgroundService(); _auctionSystemCustomer == null;", null, null); Logger.Error($"AuctionBackgroundService.AuctionBackgroundService(); _auctionSystemCustomer == null; email: {BACKGROUND_TASK_AUCTION_USER_EMAIL}", null, null);
} }
protected override async Task OnExecuteAsync() protected override async Task OnExecuteAsync(CancellationToken stoppingToken)
{ {
await Task.Delay(WARNING_STATUS_INTERVAL_SECOND * 1000); //Az elejére kell tenni! ha exception lenne, akkor ne kezdje el darálni... - J.
if (_auctionSystemCustomer == null) return; if (_auctionSystemCustomer == null) return;
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Before lock; ", null, _auctionSystemCustomer); await Logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Before lock; ", null, _auctionSystemCustomer);
using (await _lockService.SemaphoreSlim.UseWaitAsync()) using (await _lockService.SemaphoreSlim.UseWaitAsync(stoppingToken))
{ {
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Enter lock;", null, _auctionSystemCustomer); if (stoppingToken.IsCancellationRequested) return;
await Logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Enter lock;", null, _auctionSystemCustomer);
var currentAutomaticAuctions = await _auctionService.GetAllCurrentAutoOpenAndClosedAuctionsAsync(); var currentAutomaticAuctions = await _auctionService.GetAllCurrentAutoOpenAndClosedAuctionsAsync();
if (currentAutomaticAuctions.Count > 0) if (currentAutomaticAuctions.Count > 0)
{ {
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); currentAutomaticAuctions.Count > 0; count: {currentAutomaticAuctions.Count}; names: {string.Join("; ", currentAutomaticAuctions.Select(x => x.AuctionName))}", null, _auctionSystemCustomer); await Logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); currentAutomaticAuctions.Count > 0; count: {currentAutomaticAuctions.Count}; names: {string.Join("; ", currentAutomaticAuctions.Select(x => x.AuctionName))}", null, _auctionSystemCustomer);
var statusChangedMessageWrapper = new MessageWrapper var statusChangedMessageWrapper = new MessageWrapper
{ {
@ -74,7 +71,7 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
} }
//await _auctionHubContext.Clients.All.SendAsync("OnDateTimeReceive", DateTime.Now.ToString("G")); //TODO: ez csak a teszt időszakig van itt!!! - J. //await _auctionHubContext.Clients.All.SendAsync("OnDateTimeReceive", DateTime.Now.ToString("G")); //TODO: ez csak a teszt időszakig van itt!!! - J.
await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Exit lock;", null, _auctionSystemCustomer); await Logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Exit lock;", null, _auctionSystemCustomer);
} }
} }
@ -82,7 +79,7 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
{ {
try try
{ {
if (automaticAuction.Closed || automaticAuction.EndDateUtc >= DateTime.UtcNow.AddSeconds(2 * WARNING_STATUS_INTERVAL_SECOND)) return; if (automaticAuction.Closed || automaticAuction.EndDateUtc > DateTime.UtcNow.AddSeconds(2 * WARNING_STATUS_INTERVAL_SECOND)) return;
var activeProductToAuctions = await _auctionService.GetProductToAuctionsByAuctionIdAsync(automaticAuction.Id, true); var activeProductToAuctions = await _auctionService.GetProductToAuctionsByAuctionIdAsync(automaticAuction.Id, true);
if (activeProductToAuctions.Count == 0) return; if (activeProductToAuctions.Count == 0) return;
@ -118,7 +115,7 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
} }
catch (Exception ex) catch (Exception ex)
{ {
await _logger.ErrorAsync($"AuctionBackgroundService.CheckAndUpdateProductToAuctionsStatusAsync(); auctionId: {automaticAuction.Id}; auctionName: {automaticAuction.AuctionName}", ex, _auctionSystemCustomer); await Logger.ErrorAsync($"AuctionBackgroundService.CheckAndUpdateProductToAuctionsStatusAsync(); auctionId: {automaticAuction.Id}; auctionName: {automaticAuction.AuctionName}", ex, _auctionSystemCustomer);
} }
} }
@ -158,11 +155,12 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {automaticAuction.AuctionName}").ToJson(); statusChangedMessageWrapper.Data = new ProductToAuctionStatusNotification(auctionDto, 0, $"Az aukciót megnyitottuk: {automaticAuction.AuctionName}").ToJson();
statusChangedMessageWrapper.HideToaster = true; statusChangedMessageWrapper.HideToaster = true;
await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson()); await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson());
} }
catch (Exception ex) catch (Exception ex)
{ {
await _logger.ErrorAsync($"AuctionBackgroundService.OpenOrCloseAutomaticAuctionsAsync(); auctionId: {automaticAuction.Id}; name: {automaticAuction.AuctionName}", ex, _auctionSystemCustomer); await Logger.ErrorAsync($"AuctionBackgroundService.OpenOrCloseAutomaticAuctionsAsync(); auctionId: {automaticAuction.Id}; name: {automaticAuction.AuctionName}", ex, _auctionSystemCustomer);
} }
} }
} }

View File

@ -0,0 +1,7 @@
using Mango.Nop.Services;
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public interface IBackgroundService : IMgBackgroundService
{
}