Skip to content

Commit 1fbec75

Browse files
authored
Merge pull request #1427 from colinin/fix-esindex-cleanup
fix: Repairing the log index cleanup job
2 parents 9fb19d6 + e2d277b commit 1fbec75

1 file changed

Lines changed: 15 additions & 35 deletions

File tree

aspnet-core/modules/task-management/LINGYUN.Abp.Elasticsearch.Jobs/LINGYUN/Abp/Elasticsearch/Jobs/ExpiredIndicesCleanupJob.cs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using LINGYUN.Abp.BackgroundTasks;
2-
using System;
1+
using Elastic.Transport.Products.Elasticsearch;
2+
using LINGYUN.Abp.BackgroundTasks;
33
using System.Collections.Generic;
44
using System.Threading.Tasks;
55
using Volo.Abp.Timing;
@@ -20,10 +20,6 @@ public class ExpiredIndicesCleanupJob : IJobRunnable
2020
PropertyIndexPrefix,
2121
LocalizableStatic.Create("Indices:IndexPrefix"),
2222
required: true),
23-
new JobDefinitionParamter(
24-
PropertyTimeZone,
25-
LocalizableStatic.Create("Indices:TimeZone"),
26-
LocalizableStatic.Create("Indices:TimeZoneDesc")),
2723
new JobDefinitionParamter(
2824
PropertyExpirationTime,
2925
LocalizableStatic.Create("Indices:ExpirationTime")),
@@ -35,10 +31,6 @@ public class ExpiredIndicesCleanupJob : IJobRunnable
3531
/// </summary>
3632
private const string PropertyIndexPrefix = "IndexPrefix";
3733
/// <summary>
38-
/// 计算时差的时区, 默认Utc
39-
/// </summary>
40-
private const string PropertyTimeZone = "TimeZone";
41-
/// <summary>
4234
/// 过期时间, 单位秒, 默认 5184000 (60天)
4335
/// </summary>
4436
private const string PropertyExpirationTime = "ExpirationTime";
@@ -49,50 +41,36 @@ public async virtual Task ExecuteAsync(JobRunnableContext context)
4941
{
5042
#region Initializes Job Parameters
5143

52-
var timeZone = TimeZoneInfo.Utc;
5344
var indexPrefix = context.GetString(PropertyIndexPrefix);
54-
var timeZoneString = context.GetOrDefaultString(PropertyTimeZone, "utc");
5545
var expirationSecond = context.GetOrDefaultJobData(PropertyExpirationTime, 5184000L);
5646

57-
if (!timeZoneString.IsNullOrWhiteSpace())
58-
{
59-
timeZone = timeZoneString.ToLowerInvariant() switch
60-
{
61-
"local" => TimeZoneInfo.Local,
62-
_ => TimeZoneInfo.Utc,
63-
};
64-
}
65-
6647
var elasticClientFactory = context.GetRequiredService<IElasticsearchClientFactory>();
6748
var elasticClient = elasticClientFactory.Create();
6849

6950
var clock = context.GetRequiredService<IClock>();
7051
var expirationTime = clock.Now.AddSeconds(-expirationSecond);
71-
var startTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), timeZone);
7252
var removeIndices = new List<string>();
7353

7454
#endregion
7555

7656
#region ES indices.get_settings API
7757

7858
// GET demo*/_settings
79-
var settingResponse = await elasticClient.Indices.GetSettingsAsync(indexPrefix);
80-
if (!settingResponse.IsValid)
59+
var indexResponse = await elasticClient.Indices.GetAsync(indexPrefix);
60+
if (!indexResponse.IsValidResponse)
8161
{
82-
throw new AbpJobExecutionException(GetType(), settingResponse.ServerError.ToString(), settingResponse.OriginalException);
62+
indexResponse.TryGetOriginalException(out var originalException);
63+
indexResponse.TryGetElasticsearchServerError(out var elasticsearchServerError);
64+
throw new AbpJobExecutionException(GetType(), elasticsearchServerError?.ToString(), originalException);
8365
}
8466

85-
foreach (var indexSet in settingResponse.Indices)
67+
foreach (var index in indexResponse.Indices)
8668
{
8769
// 索引创建日期
88-
if (indexSet.Value.Settings.TryGetValue("index.creation_date", out var indexSetV) &&
89-
long.TryParse(indexSetV.ToString(), out var timestamp))
70+
if (index.Value.Settings?.CreationDate <= expirationTime ||
71+
index.Value.Settings?.Index?.CreationDate <= expirationTime)
9072
{
91-
var indexCreationDate = startTime.AddMilliseconds(timestamp);
92-
if (indexCreationDate <= expirationTime)
93-
{
94-
removeIndices.Add(indexSet.Key.Name);
95-
}
73+
removeIndices.Add(index.Key);
9674
}
9775
}
9876

@@ -103,9 +81,11 @@ public async virtual Task ExecuteAsync(JobRunnableContext context)
10381
foreach (var index in removeIndices)
10482
{
10583
var delResponse = await elasticClient.Indices.DeleteAsync(index);
106-
if (!delResponse.IsValid)
84+
if (!delResponse.IsValidResponse)
10785
{
108-
throw new AbpJobExecutionException(GetType(), delResponse.ServerError.ToString(), delResponse.OriginalException);
86+
delResponse.TryGetOriginalException(out var originalException);
87+
delResponse.TryGetElasticsearchServerError(out var elasticsearchServerError);
88+
throw new AbpJobExecutionException(GetType(), elasticsearchServerError?.ToString(), originalException);
10989
}
11090
}
11191

0 commit comments

Comments
 (0)