Skip to content

Commit

Permalink
Timeout improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Apr 1, 2019
1 parent 13659d6 commit b9891ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Squidex.Extensions.Actions.Webhook
{
public sealed class WebhookActionHandler : RuleActionHandler<WebhookAction, WebhookJob>
{
private static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(2);
private readonly IHttpClientFactory httpClientFactory;

public WebhookActionHandler(RuleEventFormatter formatter, IHttpClientFactory httpClientFactory)
Expand Down Expand Up @@ -47,6 +48,8 @@ protected override async Task<(string Dump, Exception Exception)> ExecuteJobAsyn
{
using (var httpClient = httpClientFactory.CreateClient())
{
httpClient.Timeout = DefaultTimeout;

var request = new HttpRequestMessage(HttpMethod.Post, job.RequestUrl)
{
Content = new StringContent(job.RequestBody, Encoding.UTF8, "application/json")
Expand Down
67 changes: 25 additions & 42 deletions src/Squidex.Domain.Apps.Entities/Contents/Text/GrainTextIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text
{
public sealed class GrainTextIndexer : ITextIndexer, IEventConsumer
{
private readonly RetryWindow retryWindow = new RetryWindow(TimeSpan.FromMinutes(5), 5);
private readonly IGrainFactory grainFactory;
private readonly ISemanticLog log;

Expand Down Expand Up @@ -58,49 +57,33 @@ public Task ClearAsync()

public async Task On(Envelope<IEvent> @event)
{
try
if (@event.Payload is ContentEvent contentEvent)
{
if (@event.Payload is ContentEvent contentEvent)
{
var index = grainFactory.GetGrain<ITextIndexerGrain>(contentEvent.SchemaId.Id);

var id = contentEvent.ContentId;

switch (@event.Payload)
{
case ContentDeleted _:
await index.DeleteAsync(id);
break;
case ContentCreated contentCreated:
await index.IndexAsync(id, Data(contentCreated.Data), true);
break;
case ContentUpdateProposed contentCreated:
await index.IndexAsync(id, Data(contentCreated.Data), true);
break;
case ContentUpdated contentUpdated:
await index.IndexAsync(id, Data(contentUpdated.Data), false);
break;
case ContentChangesDiscarded _:
await index.CopyAsync(id, false);
break;
case ContentChangesPublished _:
case ContentStatusChanged contentStatusChanged when contentStatusChanged.Status == Status.Published:
await index.CopyAsync(id, true);
break;
}
}
}
catch (Exception ex)
{
if (retryWindow.CanRetryAfterFailure())
{
log.LogError(ex, w => w
.WriteProperty("action", "DeleteTextEntry")
.WriteProperty("status", "Failed"));
}
else
var index = grainFactory.GetGrain<ITextIndexerGrain>(contentEvent.SchemaId.Id);

var id = contentEvent.ContentId;

switch (@event.Payload)
{
throw;
case ContentDeleted _:
await index.DeleteAsync(id);
break;
case ContentCreated contentCreated:
await index.IndexAsync(id, Data(contentCreated.Data), true);
break;
case ContentUpdateProposed contentCreated:
await index.IndexAsync(id, Data(contentCreated.Data), true);
break;
case ContentUpdated contentUpdated:
await index.IndexAsync(id, Data(contentUpdated.Data), false);
break;
case ContentChangesDiscarded _:
await index.CopyAsync(id, false);
break;
case ContentChangesPublished _:
case ContentStatusChanged contentStatusChanged when contentStatusChanged.Status == Status.Published:
await index.CopyAsync(id, true);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,6 @@ public async Task Should_call_grain_when_content_published()
.MustHaveHappened();
}

[Fact]
public async Task Should_catch_exception_when_indexing_failed()
{
A.CallTo(() => grain.IndexAsync(contentId, A<J<IndexData>>.Ignored, false))
.Throws(new InvalidOperationException());

await sut.On(E(new ContentCreated { Data = data }));
}

[Fact]
public async Task Should_not_catch_exception_when_indexing_failed_often()
{
A.CallTo(() => grain.IndexAsync(contentId, A<J<IndexData>>.Ignored, A<bool>.Ignored))
.Throws(new InvalidOperationException());

await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
for (var i = 0; i < 10; i++)
{
await sut.On(E(new ContentCreated { Data = data }));
}
});
}

[Fact]
public async Task Should_call_grain_when_searching()
{
Expand Down

0 comments on commit b9891ff

Please sign in to comment.