32 lines
1.1 KiB
C#
32 lines
1.1 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
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, IConfiguration configuration,ILogger<GoodsIndexJob> logger)
|
|
{
|
|
_scopeFactory = scopeFactory;
|
|
_configuration = 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} 条");
|
|
}
|
|
}
|
|
} |