AuctionBackgroundService improvements, fixes, etc...

This commit is contained in:
Loretta 2024-12-21 16:15:49 +01:00
parent 54d70baddf
commit 6de242f629
3 changed files with 29 additions and 24 deletions

View File

@ -31,7 +31,7 @@ public class AuctionDbTable : MgDbTableBase<Auction>
var utcNow = DateTime.UtcNow;
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);
}
}

View File

@ -2,62 +2,59 @@
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.Domains.DataLayer;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using Nop.Plugin.Misc.AuctionPlugin.Hubs;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Services.Customers;
using Nop.Services.Logging;
using System.Collections.Generic;
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 string BACKGROUND_TASK_AUCTION_USER_EMAIL = "builtin@background_task_auction.com";
private readonly ILogger _logger;
private readonly IHubContext<AuctionHub> _auctionHubContext;
private readonly ILockService _lockService;
private readonly AuctionService _auctionService;
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;
_auctionHubContext = auctionHubContext;
//ExecuteIntervalMs = WARNING_STATUS_INTERVAL_SECOND * 1000;
_lockService = lockService;
_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)
_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;
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();
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
{
@ -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 _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
{
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);
if (activeProductToAuctions.Count == 0) return;
@ -118,7 +115,7 @@ public class AuctionBackgroundService : MgBackgroundServiceBase
}
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.HideToaster = true;
await _auctionHubContext.Clients.All.SendAsync("send", statusChangedMessageWrapper.ToJson());
}
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
{
}