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); }
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); }
public GrainCommandMiddlewareTests() { A.CallTo(() => factory.CreateAsync <MyGrain>(id)) .Returns(grain); sut = new GrainCommandMiddleware <MatchingCommand, MyGrain>(factory); }
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); }
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(); } } }
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); }
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 })); })); }
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(); }
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)); }
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)); }