Skip to content

Commit 63bf13c

Browse files
authored
Mmi 1815 fix-- report blank (bcgov#2486)
* add log * retry elastic search
1 parent 41f0f46 commit 63bf13c

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

libs/net/dal/Extensions/JsonDocumentExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ public static JsonDocument ExcludeBCUpdates(this JsonDocument query)
235235
{
236236
jsonNode = JsonNode.Parse(query.ToJson());
237237
}
238-
catch (JsonException)
238+
catch (JsonException ex)
239239
{
240-
return query;
240+
throw new InvalidOperationException("BC Updates exclusion failed due to JSON parsing error", ex);
241241
}
242242

243243
var json = jsonNode?.AsObject();

libs/net/dal/Services/ReportService.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,10 @@ public IEnumerable<ReportInstance> GetLatestInstances(int id, int? ownerId = nul
885885

886886
// Determine index.
887887
var defaultIndex = filterSettings.SearchUnpublished ? _elasticOptions.ContentIndex : _elasticOptions.PublishedIndex;
888-
var content = await _elasticClient.SearchAsync<API.Areas.Services.Models.Content.ContentModel>(defaultIndex, query);
888+
889+
// Add retry logic for Elasticsearch query to handle failures
890+
var content = await RetryElasticsearchQueryAsync(async () =>
891+
await _elasticClient.SearchAsync<API.Areas.Services.Models.Content.ContentModel>(defaultIndex, query));
889892
var contentHits = content.Hits.Hits.ToArray();
890893

891894
// Fetch custom content versions for the requestor.
@@ -940,6 +943,30 @@ public IEnumerable<ReportInstance> GetLatestInstances(int id, int? ownerId = nul
940943
return searchResults;
941944
}
942945

946+
/// <summary>
947+
/// Retry Elasticsearch query once after 200ms to handle intermittent failures.
948+
/// This helps resolve issues like JSON parsing failures in BC Updates filtering.
949+
/// </summary>
950+
/// <typeparam name="T">The return type of the Elasticsearch operation</typeparam>
951+
/// <param name="operation">The Elasticsearch operation to retry</param>
952+
/// <returns>The result of the successful operation</returns>
953+
private async Task<T> RetryElasticsearchQueryAsync<T>(Func<Task<T>> operation)
954+
{
955+
try
956+
{
957+
return await operation();
958+
}
959+
catch (Exception ex)
960+
{
961+
Logger.LogWarning(ex,
962+
"Elasticsearch query failed. Retrying in 200ms. Error: {ErrorMessage}",
963+
ex.Message);
964+
965+
await Task.Delay(200);
966+
return await operation();
967+
}
968+
}
969+
943970
/// <summary>
944971
/// Find content with Elasticsearch for the specified `reportInstance` and `section`.
945972
/// This method supports regenerating content within a section.
@@ -1061,7 +1088,8 @@ public IEnumerable<ReportInstance> GetLatestInstances(int id, int? ownerId = nul
10611088

10621089
// Determine index.
10631090
var defaultIndex = filterSettings.SearchUnpublished ? _elasticOptions.ContentIndex : _elasticOptions.PublishedIndex;
1064-
var content = await _elasticClient.SearchAsync<API.Areas.Services.Models.Content.ContentModel>(defaultIndex, query);
1091+
var content = await RetryElasticsearchQueryAsync(async () =>
1092+
await _elasticClient.SearchAsync<API.Areas.Services.Models.Content.ContentModel>(defaultIndex, query));
10651093

10661094
// Fetch custom content versions for the requestor.
10671095
var contentIds = content.Hits.Hits.Select(h => h.Source.Id).Distinct().ToArray();

0 commit comments

Comments
 (0)