From 6de242f6295392e81cf17c806af0e669d3262b7b Mon Sep 17 00:00:00 2001 From: Loretta Date: Sat, 21 Dec 2024 16:15:49 +0100 Subject: [PATCH] AuctionBackgroundService improvements, fixes, etc... --- .../Domains/DataLayer/AuctionDbTable.cs | 2 +- .../Services/AuctionBackgroundService.cs | 44 +++++++++---------- .../Services/IBackgroundService.cs | 7 +++ 3 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 Nop.Plugin.Misc.AuctionPlugin/Services/IBackgroundService.cs diff --git a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs index 308d3c1..1f2cf13 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Domains/DataLayer/AuctionDbTable.cs @@ -31,7 +31,7 @@ public class AuctionDbTable : MgDbTableBase 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); } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs index 9966f72..af003bf 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs @@ -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 _auctionHubContext; private readonly ILockService _lockService; private readonly AuctionService _auctionService; private readonly Customer _auctionSystemCustomer; - public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository customerService) : base(logger, service) + public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository 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); } } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/IBackgroundService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/IBackgroundService.cs new file mode 100644 index 0000000..b96f073 --- /dev/null +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/IBackgroundService.cs @@ -0,0 +1,7 @@ +using Mango.Nop.Services; + +namespace Nop.Plugin.Misc.AuctionPlugin.Services; + +public interface IBackgroundService : IMgBackgroundService +{ +} \ No newline at end of file