示例#1
0
        public EventConsumerManagerTests()
        {
            A.CallTo(() => consumer1.Name).Returns(consumerName1);
            A.CallTo(() => consumer2.Name).Returns(consumerName2);

            A.CallTo(() => factory.CreateAsync <EventConsumerGrain>(consumerName1)).Returns(actor1);
            A.CallTo(() => factory.CreateAsync <EventConsumerGrain>(consumerName2)).Returns(actor2);

            sut = new EventConsumerGrainManager(new IEventConsumer[] { consumer1, consumer2 }, pubSub, factory);
        }
        public async Task UpdateAsync()
        {
            await eventStore.GetEventsAsync(async storedEvent =>
            {
                var @event = ParseKnownEvent(storedEvent);

                if (@event != null)
                {
                    var version = storedEvent.EventStreamNumber;

                    if (@event.Payload is ContentEvent contentEvent)
                    {
                        try
                        {
                            var content = await stateFactory.CreateAsync <ContentDomainObject>(contentEvent.ContentId);

                            content.UpdateState(content.State.Apply(@event));

                            await content.WriteStateAsync(version);
                        }
                        catch (DomainObjectNotFoundException)
                        {
                            // Schema has been deleted.
                        }
                    }
                    else if (@event.Payload is AssetEvent assetEvent)
                    {
                        var asset = await stateFactory.CreateAsync <AssetDomainObject>(assetEvent.AssetId);

                        asset.UpdateState(asset.State.Apply(@event));

                        await asset.WriteStateAsync(version);
                    }
                    else if (@event.Payload is SchemaEvent schemaEvent)
                    {
                        var schema = await stateFactory.GetSingleAsync <SchemaDomainObject>(schemaEvent.SchemaId.Id);

                        schema.UpdateState(schema.State.Apply(@event, fieldRegistry));

                        await schema.WriteStateAsync(version);
                    }
                    else if (@event.Payload is AppEvent appEvent)
                    {
                        var app = await stateFactory.GetSingleAsync <AppDomainObject>(appEvent.AppId.Id);

                        app.UpdateState(app.State.Apply(@event));

                        await app.WriteStateAsync(version);
                    }
                }
            }, CancellationToken.None);
        }
示例#3
0
        public async Task RebuildAssetsAsync()
        {
            await snapshotAssetStore.ClearAsync();

            const string filter = "^asset\\-";

            var handledIds = new HashSet <Guid>();

            await eventStore.QueryAsync(async storedEvent =>
            {
                var @event = ParseKnownEvent(storedEvent);

                if (@event != null)
                {
                    if (@event.Payload is AssetEvent assetEvent && handledIds.Add(assetEvent.AssetId))
                    {
                        var asset = await stateFactory.CreateAsync <AssetGrain>(assetEvent.AssetId);

                        asset.ApplySnapshot(asset.Snapshot.Apply(@event));

                        await asset.WriteSnapshotAsync();
                    }
                }
            }, filter, ct : CancellationToken.None);
        }
示例#4
0
        public GrainCommandMiddlewareTests()
        {
            A.CallTo(() => factory.CreateAsync <MyGrain>(id))
            .Returns(grain);

            sut = new GrainCommandMiddleware <MatchingCommand, MyGrain>(factory);
        }
示例#5
0
        private async Task <T> InvokeAsync <T>(CommandContext context, Func <T, Task> handler, bool isUpdate) where T : class, IDomainObject
        {
            Guard.NotNull(context, nameof(context));

            var domainCommand  = GetCommand(context);
            var domainObjectId = domainCommand.AggregateId;
            var domainObject   = await stateFactory.CreateAsync <T>(domainObjectId);

            if (domainCommand.ExpectedVersion != EtagVersion.Any && domainCommand.ExpectedVersion != domainObject.Version)
            {
                throw new DomainObjectVersionException(domainObjectId.ToString(), typeof(T), domainObject.Version, domainCommand.ExpectedVersion);
            }

            await handler(domainObject);

            await domainObject.WriteAsync();

            if (!context.IsCompleted)
            {
                if (isUpdate)
                {
                    context.Complete(new EntitySavedResult(domainObject.Version));
                }
                else
                {
                    context.Complete(EntityCreatedResult.Create(domainObjectId, domainObject.Version));
                }
            }

            return(domainObject);
        }
示例#6
0
        public async Task UpdateAsync()
        {
            var ids = await appRepository.QueryAppIdsAsync();

            foreach (var id in ids)
            {
                var app = await stateFactory.CreateAsync <AppDomainObject>(id);

                if (app.Snapshot.Patterns.Count == 0)
                {
                    foreach (var pattern in initialPatterns.Values)
                    {
                        var command =
                            new AddPattern
                        {
                            Actor     = app.Snapshot.CreatedBy,
                            AppId     = new NamedId <Guid>(app.Snapshot.Id, app.Snapshot.Name),
                            Name      = pattern.Name,
                            PatternId = Guid.NewGuid(),
                            Pattern   = pattern.Pattern,
                            Message   = pattern.Message
                        };

                        app.AddPattern(command);
                    }

                    await app.WriteAsync();
                }
            }
        }
示例#7
0
        protected async Task <object> ExecuteCommandAsync(TCommand typedCommand)
        {
            var grain = await stateFactory.CreateAsync <TGrain>(typedCommand.AggregateId);

            var result = await grain.ExecuteAsync(typedCommand);

            return(result);
        }
        public AssetCommandMiddlewareTests()
        {
            file = new AssetFile("my-image.png", "image/png", 1024, () => stream);

            asset = new AssetGrain(Store);
            asset.ActivateAsync(Id).Wait();

            A.CallTo(() => stateFactory.CreateAsync <AssetGrain>(Id))
            .Returns(asset);

            sut = new AssetCommandMiddleware(stateFactory, assetStore, assetThumbnailGenerator);
        }
示例#9
0
        public void Connect()
        {
            var actors = new Dictionary <string, EventConsumerGrain>();

            foreach (var consumer in consumers)
            {
                var actor = factory.CreateAsync <EventConsumerGrain>(consumer.Name).Result;

                actors[consumer.Name] = actor;
                actor.Activate(consumer);
            }

            subscriptions.Add(pubSub.Subscribe <StartConsumerMessage>(m =>
            {
                if (actors.TryGetValue(m.ConsumerName, out var actor))
                {
                    actor.Start();
                }
            }));

            subscriptions.Add(pubSub.Subscribe <StopConsumerMessage>(m =>
            {
                if (actors.TryGetValue(m.ConsumerName, out var actor))
                {
                    actor.Stop();
                }
            }));

            subscriptions.Add(pubSub.Subscribe <ResetConsumerMessage>(m =>
            {
                if (actors.TryGetValue(m.ConsumerName, out var actor))
                {
                    actor.Reset();
                }
            }));

            subscriptions.Add(pubSub.ReceiveAsync <GetStatesRequest, GetStatesResponse>(request =>
            {
                var states = actors.Values.Select(x => x.GetState()).ToArray();

                return(Task.FromResult(new GetStatesResponse {
                    States = states
                }));
            }));
        }
示例#10
0
        public AggregateHandlerTests()
        {
            command = new MyCommand {
                AggregateId = domainObjectId, ExpectedVersion = EtagVersion.Any
            };
            context = new CommandContext(command, A.Dummy <ICommandBus>());

            A.CallTo(() => store.WithSnapshotsAndEventSourcing(domainObjectId, A <Func <MyDomainState, Task> > .Ignored, A <Func <Envelope <IEvent>, Task> > .Ignored))
            .Returns(persistence);

            A.CallTo(() => stateFactory.CreateAsync <MyDomainObject>(domainObjectId))
            .Returns(Task.FromResult(domainObject));

            A.CallTo(() => stateFactory.GetSingleAsync <MyDomainObject>(domainObjectId))
            .Returns(Task.FromResult(domainObject));

            sut = new AggregateHandler(stateFactory, serviceProvider);

            domainObject.ActivateAsync(domainObjectId, store).Wait();
        }
示例#11
0
        private Task MigrateAssetsAsync()
        {
            const string filter = "^asset\\-";

            var handledIds = new HashSet <Guid>();

            return(eventStore.GetEventsAsync(async storedEvent =>
            {
                var @event = ParseKnownEvent(storedEvent);

                if (@event != null)
                {
                    if (@event.Payload is AssetEvent assetEvent && handledIds.Add(assetEvent.AssetId))
                    {
                        var asset = await stateFactory.CreateAsync <AssetDomainObject>(assetEvent.AssetId);

                        asset.ApplySnapshot(asset.Snapshot.Apply(@event));

                        await asset.WriteSnapshotAsync();
                    }
                }
            }, CancellationToken.None, filter));
        }
示例#12
0
 public static StateObject <T> CreateAsync <T>(IActorStateManager stateManager, string key, Func <Task <T> > factory, Func <T, T, Task> onChange = null, IEqualityComparer <T> equalityComparer = null)
 {
     return(Default.CreateAsync(stateManager, key, factory, onChange, equalityComparer));
 }