From 25bf0de335dcdc5d4a55f1612c9d83a27e3aa111 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 13 Apr 2020 17:23:12 +0200 Subject: [PATCH] Id fix. --- .../Rules/RuleJob.cs | 2 +- .../HandleRules/RuleService.cs | 2 +- .../Rules/MongoRuleEventRepository.cs | 23 +++++++++++-- .../UniqueConstraintException.cs | 34 +++++++++++++++++++ .../HandleRules/RuleServiceTests.cs | 2 +- .../Rules/RuleDequeuerTests.cs | 2 +- 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 backend/src/Squidex.Infrastructure/UniqueConstraintException.cs diff --git a/backend/src/Squidex.Domain.Apps.Core.Model/Rules/RuleJob.cs b/backend/src/Squidex.Domain.Apps.Core.Model/Rules/RuleJob.cs index 106358450..559a4dfef 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Model/Rules/RuleJob.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Model/Rules/RuleJob.cs @@ -12,7 +12,7 @@ namespace Squidex.Domain.Apps.Core.Rules { public sealed class RuleJob { - public Guid Id { get; set; } + public Guid EventId { get; set; } public Guid AppId { get; set; } diff --git a/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleService.cs b/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleService.cs index c95fb00a4..f8e164f35 100644 --- a/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleService.cs +++ b/backend/src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleService.cs @@ -141,12 +141,12 @@ namespace Squidex.Domain.Apps.Core.HandleRules var job = new RuleJob { - Id = Guid.NewGuid(), ActionData = json, ActionName = actionName, AppId = enrichedEvent.AppId.Id, Created = now, Description = actionData.Description, + EventId = @event.Headers.EventId(), EventName = enrichedEvent.Name, ExecutionPartition = enrichedEvent.Partition, Expires = expires, diff --git a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Rules/MongoRuleEventRepository.cs b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Rules/MongoRuleEventRepository.cs index 1deb73603..987cb629d 100644 --- a/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Rules/MongoRuleEventRepository.cs +++ b/backend/src/Squidex.Domain.Apps.Entities.MongoDb/Rules/MongoRuleEventRepository.cs @@ -15,6 +15,7 @@ using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.Rules; using Squidex.Domain.Apps.Entities.Rules; using Squidex.Domain.Apps.Entities.Rules.Repositories; +using Squidex.Infrastructure; using Squidex.Infrastructure.MongoDb; using Squidex.Infrastructure.Reflection; @@ -93,11 +94,27 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Rules return Collection.UpdateOneAsync(x => x.Id == id, Update.Set(x => x.NextAttempt, nextAttempt)); } - public Task EnqueueAsync(RuleJob job, Instant nextAttempt) + public async Task EnqueueAsync(RuleJob job, Instant nextAttempt) { var entity = SimpleMapper.Map(job, new MongoRuleEventEntity { Job = job, Created = nextAttempt, NextAttempt = nextAttempt }); - return Collection.InsertOneIfNotExistsAsync(entity); + if (job.EventId != default) + { + entity.Id = job.EventId; + } + else + { + entity.Id = Guid.NewGuid(); + } + + try + { + await Collection.InsertOneIfNotExistsAsync(entity); + } + catch (MongoWriteException ex) when (ex.WriteError.Category == ServerErrorCategory.DuplicateKey) + { + throw new UniqueConstraintException(); + } } public Task CancelAsync(Guid id) @@ -119,7 +136,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Rules await statisticsCollection.IncrementFailed(job.AppId, job.RuleId, finished); } - await Collection.UpdateOneAsync(x => x.Id == job.Id, + await Collection.UpdateOneAsync(x => x.Id == job.EventId, Update .Set(x => x.Result, result) .Set(x => x.LastDump, dump) diff --git a/backend/src/Squidex.Infrastructure/UniqueConstraintException.cs b/backend/src/Squidex.Infrastructure/UniqueConstraintException.cs new file mode 100644 index 000000000..96570ad79 --- /dev/null +++ b/backend/src/Squidex.Infrastructure/UniqueConstraintException.cs @@ -0,0 +1,34 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschraenkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System; + +namespace Squidex.Infrastructure +{ + [Serializable] + public class UniqueConstraintException : Exception + { + public UniqueConstraintException() + { + } + + public UniqueConstraintException(string message) + : base(message) + { + } + + public UniqueConstraintException(string message, Exception inner) + : base(message, inner) + { + } + + protected UniqueConstraintException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + } +} diff --git a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs index 6d34b5343..970b2749d 100644 --- a/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleServiceTests.cs @@ -376,7 +376,7 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules Assert.Equal(now, job.Created); Assert.Equal(now.Plus(Duration.FromDays(30)), job.Expires); - Assert.NotEqual(Guid.Empty, job.Id); + Assert.NotEqual(Guid.Empty, job.EventId); } } } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs index e24e35476..b802bd6dc 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs @@ -141,7 +141,7 @@ namespace Squidex.Domain.Apps.Entities.Rules var job = new RuleJob { - Id = id, + EventId = id, ActionData = actionData, ActionName = actionName, Created = clock.GetCurrentInstant()