Files
StoreWeb/Plugins/CloudProduct/Services/GoodsIndexJob.cs
T

33 lines
1.2 KiB
C#

using MAX.Plugins;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace Store.Modules.CloudProduct.Services
{
/// <summary>
/// Hangfire 定时任务:把数据库中未同步(IndexStatus=0)的商品写入 OpenSearch 分词索引。
/// </summary>
public class GoodsIndexJob
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly IConfiguration _configuration;
private readonly ILogger<GoodsIndexJob> _logger;
public GoodsIndexJob(IServiceScopeFactory scopeFactory, [FromKeyedServices("CloudProduct")] PluginContext context,ILogger<GoodsIndexJob> logger)
{
_scopeFactory = scopeFactory;
_configuration = context.Configuration;
_logger = logger;
}
public async Task ProcessPendingAsync()
{
var batchSize = _configuration.GetValue<int?>("Indexing:BatchSize") ?? 1000;
using var scope = _scopeFactory.CreateScope();
var service = scope.ServiceProvider.GetRequiredService<GoodsService>();
var count = await service.IndexPendingGoodsAsync(batchSize);
_logger.LogInformation($"[Hangfire] 商品分词定时任务完成,本次同步 {count} 条");
}
}
}