Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HttpBackgroundJob instead of Deferred Tasks during search indexing #14063

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3136ea5
Use HttpBackgourndJob replace ShellScope AddDeferredTask
Aug 3, 2023
848e63e
Merge branch 'origin_main' into LuceneIndexingContentHandler
hyzx86 Aug 7, 2023
adbcc5c
update ElasticIndexingContentHandler
hyzx86 Aug 7, 2023
d7f9d99
Fixes unit tests
jtkech Aug 8, 2023
7e2558d
Minor change
jtkech Aug 8, 2023
6fc95a0
Increase the delay
jtkech Aug 8, 2023
b536827
Merge branch 'main' into LuceneIndexingContentHandler
Piedone Jan 10, 2024
be5bc69
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 Mar 22, 2024
d4b89d6
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 Mar 23, 2024
e25416b
update test case
hyzx86 Mar 23, 2024
5c87657
Merge branch 'LuceneIndexingContentHandler' of https://github.com/hyz…
hyzx86 Mar 23, 2024
31b973e
Update test/OrchardCore.Tests/Apis/ContentManagement/DeploymentPlans/…
hyzx86 Mar 25, 2024
aa6f5f4
update code
Mar 25, 2024
c85f67e
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 Mar 25, 2024
72be022
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 Apr 2, 2024
353ad25
Merge branch 'main' into LuceneIndexingContentHandler
hishamco Apr 7, 2024
b7574f4
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 Apr 30, 2024
5395aa0
add TimeoutTaskRunner
Apr 30, 2024
8f4a2da
update TimeoutTaskRunner
Apr 30, 2024
dbbca42
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 May 1, 2024
49ee107
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 May 2, 2024
38fa7ce
Merge branch 'main' into LuceneIndexingContentHandler
hyzx86 Jun 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OrchardCore.BackgroundJobs;
using OrchardCore.ContentLocalization;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Handlers;
Expand Down Expand Up @@ -50,8 +51,9 @@ private Task AddContextAsync(ContentContextBase context)
{
var contexts = _contexts;

// Using a local var prevents the lambda from holding a ref on this scoped service.
ShellScope.AddDeferredTask(scope => IndexingAsync(scope, contexts));
HttpBackgroundJob.ExecuteAfterEndOfRequestAsync(
nameof(LuceneIndexingContentHandler),
scope => IndexingAsync(scope, contexts));
}

_contexts.Add(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OrchardCore.BackgroundJobs;
using OrchardCore.ContentLocalization;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Handlers;
Expand Down Expand Up @@ -56,7 +57,9 @@ private Task AddContextAsync(ContentContextBase context)
var contexts = _contexts;

// Using a local var prevents the lambda from holding a ref on this scoped service.
ShellScope.AddDeferredTask(scope => IndexingAsync(scope, contexts));
HttpBackgroundJob.ExecuteAfterEndOfRequestAsync(
nameof(ElasticIndexingContentHandler),
scope => IndexingAsync(scope, contexts));
}

_contexts.Add(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Http;
using OrchardCore.Autoroute.Models;
using OrchardCore.ContentManagement;
using OrchardCore.Tests.Apis.Context;
using OrchardCore.Tests.Utilities;

namespace OrchardCore.Tests.Apis.ContentManagement.DeploymentPlans
{
Expand Down Expand Up @@ -34,21 +36,30 @@ public async Task ShouldUpdateLuceneIndexesOnImport()

await context.PostRecipeAsync(recipe);

// Test
var result = await context
.GraphQLClient
.Content
.Query("RecentBlogPosts", builder =>
{
builder
.WithField("displayText");
});

var nodes = result["data"]["recentBlogPosts"];

Assert.Equal(2, nodes.AsArray().Count);
Assert.Equal("new version", nodes[0]["displayText"].ToString());
Assert.Equal("second content item display text", nodes[1]["displayText"].ToString());
// Search indexes are no longer updated in a deferred task at the end of a shell scope
// but in a background job after the http request, so they are not already up to date.

await TimeoutTaskRunner.RunAsync(TimeSpan.FromSeconds(5), async () =>
{
// Test
var result = await context
.GraphQLClient
.Content
.Query("RecentBlogPosts", builder =>
{
builder
.WithField("displayText");
});

var nodes = result["data"]["recentBlogPosts"];

return nodes is not null
&& nodes.AsArray().Count == 2
&& "new version" == nodes[0]["displayText"].ToString()
&& "second content item display text" == nodes[1]["displayText"].ToString();
}, "The Lucene index wasn't updated after the import within 5s and thus the test timed out.");


}
}
}
4 changes: 4 additions & 0 deletions test/OrchardCore.Tests/Apis/Context/SiteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public virtual async Task InitializeAsync()
TenantName = tenantName;
}

// Search indexes are no longer updated in a deferred task at the end of a shell scope
// but in a background job after the http request, so they are not already up to date.
await Task.Delay(2_000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can make the test flaky. Instead, explicitly wait for the process to finish, don't really on occasionally correct time.

See how you fixed this under another PR of yours when I pointed it out.

Also in RecentBlogPostsQueryTests.

hyzx86 marked this conversation as resolved.
Show resolved Hide resolved

if (PermissionsContext != null)
{
var permissionContextKey = Guid.NewGuid().ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using OrchardCore.ContentManagement;
using OrchardCore.Lists.Models;
using OrchardCore.Tests.Apis.Context;
using OrchardCore.Tests.Utilities;

namespace OrchardCore.Tests.Apis.GraphQL
{
Expand All @@ -26,20 +27,32 @@ public async Task ShouldListBlogPostWhenCallingAQuery()
});
});

var result = await context
.GraphQLClient
.Content
.Query("RecentBlogPosts", builder =>
// Search indexes are no longer updated in a deferred task at the end of a shell scope
// but in a background job after the http request, so they are not already up to date.
await TimeoutTaskRunner.RunAsync(TimeSpan.FromSeconds(5), async () =>
{
var result = await context
.GraphQLClient
.Content
.Query("RecentBlogPosts", builder =>
{
builder
.WithField("displayText");
});
var nodes = result["data"]["recentBlogPosts"];
if (nodes.AsArray().Count != 2)
{
builder
.WithField("displayText");
});
return true; // next loop
}
Assert.Equal(2, nodes.AsArray().Count);
Assert.Equal("Some sorta blogpost in a Query!", nodes[0]["displayText"].ToString());
Assert.Equal("Man must explore, and this is exploration at its greatest", nodes[1]["displayText"].ToString());

return false;

}, "The Lucene index wasn't updated after the import within 5s and thus the test timed out.");

var nodes = result["data"]["recentBlogPosts"];

Assert.Equal(2, nodes.AsArray().Count);
Assert.Equal("Some sorta blogpost in a Query!", nodes[0]["displayText"].ToString());
Assert.Equal("Man must explore, and this is exploration at its greatest", nodes[1]["displayText"].ToString());
}
}
}
21 changes: 21 additions & 0 deletions test/OrchardCore.Tests/Utilities/TimeoutTaskRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace OrchardCore.Tests.Utilities;

public class TimeoutTaskRunner
{
public static async Task RunAsync(TimeSpan timeout, Func<Task<bool>> checkNextLoop, string failMessage)
{
var timeoutTask = Task.Delay(timeout);
while (true)
{
if (timeoutTask.IsCompleted)
{
Assert.Fail(failMessage);
hyzx86 marked this conversation as resolved.
Show resolved Hide resolved
}
await Task.Delay(500);
if (!await checkNextLoop.Invoke())
{
break;
}
hyzx86 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading