Browse Source

Fixes to rules.

pull/760/head
Sebastian 4 years ago
parent
commit
176bb42949
  1. 6
      backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/JobResult.cs
  2. 2
      backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleService.cs
  3. 4
      backend/src/Squidex.Domain.Apps.Entities.MongoDb/Rules/MongoRuleEventRepository.cs
  4. 4
      backend/src/Squidex.Domain.Apps.Entities/Rules/RuleEnqueuer.cs
  5. 2
      backend/src/Squidex.Domain.Apps.Entities/Rules/Runner/DefaultRuleRunnerService.cs
  6. 14
      backend/src/Squidex.Domain.Apps.Entities/Rules/Runner/RuleRunnerGrain.cs
  7. 6
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs
  8. 21
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs
  9. 1
      frontend/app/features/rules/pages/events/rule-event.component.scss

6
backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/JobResult.cs

@ -57,17 +57,17 @@ namespace Squidex.Domain.Apps.Core.HandleRules
public EnrichedEvent? EnrichedEvent { get; init; } public EnrichedEvent? EnrichedEvent { get; init; }
public Exception? Exception { get; init; } public Exception? EnrichmentError { get; init; }
public SkipReason SkipReason { get; init; } public SkipReason SkipReason { get; init; }
public static JobResult Failed(Exception exception, EnrichedEvent? enrichedEvent = null, RuleJob? job = null) public static JobResult Failed(Exception error, EnrichedEvent? enrichedEvent = null, RuleJob? job = null)
{ {
return new JobResult return new JobResult
{ {
Job = job, Job = job,
EnrichedEvent = enrichedEvent, EnrichedEvent = enrichedEvent,
Exception = exception, EnrichmentError = error,
SkipReason = SkipReason.Failed SkipReason = SkipReason.Failed
}; };
} }

2
backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleService.cs

@ -238,7 +238,7 @@ namespace Squidex.Domain.Apps.Core.HandleRules
jobs.Add(new JobResult jobs.Add(new JobResult
{ {
EnrichedEvent = enrichedEvent, EnrichedEvent = enrichedEvent,
Exception = ex, EnrichmentError = ex,
SkipReason = SkipReason.Failed SkipReason = SkipReason.Failed
}); });
} }

4
backend/src/Squidex.Domain.Apps.Entities.MongoDb/Rules/MongoRuleEventRepository.cs

@ -119,7 +119,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Rules
public Task CancelByRuleAsync(DomainId ruleId) public Task CancelByRuleAsync(DomainId ruleId)
{ {
return Collection.UpdateOneAsync(x => x.RuleId == ruleId, return Collection.UpdateManyAsync(x => x.RuleId == ruleId,
Update Update
.Set(x => x.NextAttempt, null) .Set(x => x.NextAttempt, null)
.Set(x => x.JobResult, RuleJobResult.Cancelled)); .Set(x => x.JobResult, RuleJobResult.Cancelled));
@ -127,7 +127,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Rules
public Task CancelByAppAsync(DomainId appId) public Task CancelByAppAsync(DomainId appId)
{ {
return Collection.UpdateOneAsync(x => x.DocumentId == appId, return Collection.UpdateManyAsync(x => x.AppId == appId,
Update Update
.Set(x => x.NextAttempt, null) .Set(x => x.NextAttempt, null)
.Set(x => x.JobResult, RuleJobResult.Cancelled)); .Set(x => x.JobResult, RuleJobResult.Cancelled));

4
backend/src/Squidex.Domain.Apps.Entities/Rules/RuleEnqueuer.cs

@ -61,9 +61,9 @@ namespace Squidex.Domain.Apps.Entities.Rules
await foreach (var job in jobs) await foreach (var job in jobs)
{ {
if (job.Job != null) if (job.Job != null && job.SkipReason == SkipReason.None)
{ {
await ruleEventRepository.EnqueueAsync(job.Job, job.Exception); await ruleEventRepository.EnqueueAsync(job.Job, job.EnrichmentError);
} }
} }
} }

2
backend/src/Squidex.Domain.Apps.Entities/Rules/Runner/DefaultRuleRunnerService.cs

@ -67,7 +67,7 @@ namespace Squidex.Domain.Apps.Entities.Rules.Runner
ActionData = result.Job?.ActionData, ActionData = result.Job?.ActionData,
ActionName = result.Job?.ActionName, ActionName = result.Job?.ActionName,
EnrichedEvent = result.EnrichedEvent, EnrichedEvent = result.EnrichedEvent,
Error = result.Exception?.Message, Error = result.EnrichmentError?.Message,
Event = @event.Payload, Event = @event.Payload,
EventName = eventName, EventName = eventName,
SkipReason = result.SkipReason SkipReason = result.SkipReason

14
backend/src/Squidex.Domain.Apps.Entities/Rules/Runner/RuleRunnerGrain.cs

@ -219,20 +219,20 @@ namespace Squidex.Domain.Apps.Entities.Rules.Runner
await foreach (var job in ruleService.CreateSnapshotJobsAsync(context, ct)) await foreach (var job in ruleService.CreateSnapshotJobsAsync(context, ct))
{ {
if (job.Job != null) if (job.Job != null && job.SkipReason == SkipReason.None)
{ {
await ruleEventRepository.EnqueueAsync(job.Job, job.Exception); await ruleEventRepository.EnqueueAsync(job.Job, job.EnrichmentError);
} }
else if (job.Exception != null) else if (job.EnrichmentError != null)
{ {
errors++; errors++;
if (errors >= MaxErrors) if (errors >= MaxErrors)
{ {
throw job.Exception; throw job.EnrichmentError;
} }
log.LogWarning(job.Exception, w => w log.LogWarning(job.EnrichmentError, w => w
.WriteProperty("action", "runRule") .WriteProperty("action", "runRule")
.WriteProperty("status", "failedPartially")); .WriteProperty("status", "failedPartially"));
} }
@ -257,9 +257,9 @@ namespace Squidex.Domain.Apps.Entities.Rules.Runner
await foreach (var job in jobs) await foreach (var job in jobs)
{ {
if (job.Job != null) if (job.Job != null && job.SkipReason == SkipReason.None)
{ {
await ruleEventRepository.EnqueueAsync(job.Job, job.Exception); await ruleEventRepository.EnqueueAsync(job.Job, job.EnrichmentError);
} }
} }
} }

6
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs

@ -250,7 +250,7 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
var jobs = await sut.CreateSnapshotJobsAsync(context).ToListAsync(); var jobs = await sut.CreateSnapshotJobsAsync(context).ToListAsync();
Assert.Equal(2, jobs.Count(x => x.Job != null && x.Exception == null)); Assert.Equal(2, jobs.Count(x => x.Job != null && x.EnrichmentError == null));
} }
[Fact] [Fact]
@ -273,7 +273,7 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
var jobs = await sut.CreateSnapshotJobsAsync(context).ToListAsync(); var jobs = await sut.CreateSnapshotJobsAsync(context).ToListAsync();
Assert.Equal(2, jobs.Count(x => x.Job == null && x.Exception != null)); Assert.Equal(2, jobs.Count(x => x.Job == null && x.EnrichmentError != null));
} }
[Fact] [Fact]
@ -587,7 +587,7 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
var job = await sut.CreateJobsAsync(@event, context).SingleAsync(); var job = await sut.CreateJobsAsync(@event, context).SingleAsync();
Assert.NotNull(job.Exception); Assert.NotNull(job.EnrichmentError);
Assert.NotNull(job.Job?.ActionData); Assert.NotNull(job.Job?.ActionData);
Assert.NotNull(job.Job?.Description); Assert.NotNull(job.Job?.Description);
Assert.Equal(enrichedEvent, job.EnrichedEvent); Assert.Equal(enrichedEvent, job.EnrichedEvent);

21
backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleEnqueuerTests.cs

@ -96,6 +96,27 @@ namespace Squidex.Domain.Apps.Entities.Rules
.MustNotHaveHappened(); .MustNotHaveHappened();
} }
[Fact]
public async Task Should_not_insert_job_if_job_has_a_skip_reason()
{
var @event = Envelope.Create<IEvent>(new ContentCreated { AppId = appId });
var rule = CreateRule();
var job = new RuleJob
{
Created = now
};
A.CallTo(() => ruleService.CreateJobsAsync(@event, A<RuleContext>.That.Matches(x => x.Rule == rule.RuleDef), default))
.Returns(new List<JobResult> { new JobResult { Job = job, SkipReason = SkipReason.TooOld } }.ToAsyncEnumerable());
await sut.EnqueueAsync(rule.RuleDef, rule.Id, @event);
A.CallTo(() => ruleEventRepository.EnqueueAsync(A<RuleJob>._, (Exception?)null))
.MustNotHaveHappened();
}
[Fact] [Fact]
public async Task Should_update_repository_if_enqueing() public async Task Should_update_repository_if_enqueing()
{ {

1
frontend/app/features/rules/pages/events/rule-event.component.scss

@ -27,7 +27,6 @@ td {
&-stats { &-stats {
font-size: $font-smallest; font-size: $font-smallest;
font-weight: normal; font-weight: normal;
padding-bottom: 0;
} }
&-header { &-header {

Loading…
Cancel
Save