-
Notifications
You must be signed in to change notification settings - Fork 8
[Deprecated] Extract streaming from GAgentBase. #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 6 commits
183b76a
b6d8772
ca9c50e
caf0120
b76ea1b
6b45438
7cd23b3
e188a82
ecb43ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| namespace Aevatar.Core.Abstractions.Projections; | ||
|
|
||
| public interface IProjectionGrain<TState> : IGrainWithGuidKey | ||
| where TState : StateBase, new() | ||
| public interface IProjectionGrain : IGrainWithStringKey | ||
| { | ||
| Task ActivateAsync(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| using System.Collections.Concurrent; | ||
| using Aevatar.Core.Abstractions; | ||
| using Aevatar.Core.Abstractions.Projections; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
@@ -45,7 +44,11 @@ public abstract partial class | |
| private Lazy<IStreamProvider> LazyStreamProvider => new(() | ||
| => this.GetStreamProvider(AevatarCoreConstants.StreamProvider)); | ||
|
|
||
| private Lazy<IGAgentFactory> LazyGAgentFactory => new(() | ||
| => ServiceProvider.GetRequiredService<IGAgentFactory>()); | ||
|
|
||
| protected IStreamProvider StreamProvider => LazyStreamProvider.Value; | ||
| protected IGAgentFactory GAgentFactory => LazyGAgentFactory.Value; | ||
|
|
||
| public ILogger Logger { get; set; } = NullLogger.Instance; | ||
|
|
||
|
|
@@ -74,12 +77,16 @@ public async Task RegisterAsync(IGAgent gAgent) | |
|
|
||
| public async Task SubscribeToAsync(IGAgent gAgent) | ||
| { | ||
| var parentGrainId = gAgent.GetGrainId(); | ||
| var parentStream = GetEventBaseStreamForChildren(parentGrainId); | ||
| var asyncObserver = new GAgentAsyncObserver(_observers); | ||
| await ResumeOrSubscribeAsync(parentStream, asyncObserver); | ||
| await SetParentAsync(gAgent.GetGrainId()); | ||
| } | ||
|
|
||
| public Task UnsubscribeFromAsync(IGAgent gAgent) | ||
| public async Task UnsubscribeFromAsync(IGAgent gAgent) | ||
| { | ||
| return ClearParentAsync(gAgent.GetGrainId()); | ||
| await ClearParentAsync(gAgent.GetGrainId()); | ||
| } | ||
|
|
||
| public async Task UnregisterAsync(IGAgent gAgent) | ||
|
|
@@ -89,7 +96,7 @@ public async Task UnregisterAsync(IGAgent gAgent) | |
| await OnUnregisterAgentAsync(gAgent.GetGrainId()); | ||
| } | ||
|
|
||
| public virtual Task<List<Type>?> GetAllSubscribedEventsAsync(bool includeBaseHandlers = false) | ||
| public async virtual Task<List<Type>?> GetAllSubscribedEventsAsync(bool includeBaseHandlers = false) | ||
| { | ||
| var eventHandlerMethods = GetEventHandlerMethods(GetType()); | ||
| eventHandlerMethods = eventHandlerMethods.Where(m => | ||
|
|
@@ -101,7 +108,7 @@ public async Task UnregisterAsync(IGAgent gAgent) | |
| handlingTypes = handlingTypes.Where(t => t != typeof(RequestAllSubscriptionsEvent)); | ||
| } | ||
|
|
||
| return Task.FromResult(handlingTypes.ToList())!; | ||
| return handlingTypes.ToList(); | ||
| } | ||
|
|
||
| public Task<List<GrainId>> GetChildrenAsync() | ||
|
|
@@ -127,6 +134,18 @@ public async Task ConfigAsync(ConfigurationBase configuration) | |
| } | ||
| } | ||
|
|
||
| public async Task<IAsyncObserver<EventWrapperBase>> GetGAgentAsyncObserverAsync() | ||
| { | ||
| var asyncObserver = new GAgentAsyncObserver(_observers); | ||
| return asyncObserver; | ||
| } | ||
|
|
||
| public async Task ResumeSubscriptionAsync(IAsyncStream<EventWrapperBase> stream) | ||
| { | ||
| var asyncObserver = new GAgentAsyncObserver(_observers); | ||
| await ResumeOrSubscribeAsync(stream, asyncObserver); | ||
| } | ||
|
|
||
| protected virtual Task PerformConfigAsync(TConfiguration configuration) | ||
| { | ||
| return Task.CompletedTask; | ||
|
|
@@ -238,21 +257,35 @@ private async Task BaseOnActivateAsync(CancellationToken cancellationToken) | |
| var initTasks = new[] | ||
| { | ||
| InitializeOrResumeEventBaseStreamAsync(), | ||
| ResumeEventBaseStreamForChildrenAsync(), | ||
| ActivateProjectionGrainAsync() | ||
| }; | ||
| await Task.WhenAll(initTasks); | ||
| } | ||
|
|
||
| private async Task InitializeOrResumeEventBaseStreamAsync() | ||
| { | ||
| var streamOfThisGAgent = GetEventBaseStream(this.GetGrainId()); | ||
| var streamOfThisGAgent = GetEventBaseStreamForOwn(this.GetGrainId()); | ||
| var asyncObserver = new GAgentAsyncObserver(_observers); | ||
| await ResumeOrSubscribeAsync(streamOfThisGAgent, asyncObserver); | ||
| } | ||
|
|
||
| private async Task ResumeEventBaseStreamForChildrenAsync() | ||
| { | ||
| var streamForChildren = GetEventBaseStreamForChildren(this.GetGrainId()); | ||
| var tasks = new List<Task>(); | ||
| foreach (var childGrainId in State.Children) | ||
| { | ||
| var child = await GAgentFactory.GetGAgentAsync(childGrainId); | ||
| tasks.Add(child.ResumeSubscriptionAsync(streamForChildren)); | ||
| } | ||
|
|
||
| await Task.WhenAll(tasks); | ||
| } | ||
|
|
||
| private async Task ActivateProjectionGrainAsync() | ||
| { | ||
| var projectionGrain = GrainFactory.GetGrain<IProjectionGrain<TState>>(Guid.Empty); | ||
| var projectionGrain = GrainFactory.GetGrain<IProjectionGrain>(typeof(TState).FullName); | ||
| await projectionGrain.ActivateAsync(); | ||
| } | ||
|
|
||
|
|
@@ -330,16 +363,21 @@ protected virtual Task HandleRaiseEventAsync() | |
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private IAsyncStream<EventWrapperBase> GetEventBaseStream(GrainId grainId) | ||
| private IAsyncStream<EventWrapperBase> GetEventBaseStreamForOwn(GrainId grainId) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This naming convention is very difficult to understand. Can't really explain how
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about changing it to |
||
| { | ||
| var grainIdString = grainId.ToString(); | ||
| var streamId = StreamId.Create(AevatarOptions!.StreamNamespace, grainIdString); | ||
| return StreamProvider.GetStream<EventWrapperBase>(streamId); | ||
| return GetEventBaseStream(grainIdString); | ||
| } | ||
|
|
||
| private IAsyncStream<StateWrapper<TState>> GetStateProjectionStream() | ||
| private IAsyncStream<EventWrapperBase> GetEventBaseStreamForChildren(GrainId grainId) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| var streamId = StreamId.Create(AevatarOptions!.StreamNamespace, typeof(StateWrapper<TState>).FullName!); | ||
| return StreamProvider.GetStream<StateWrapper<TState>>(streamId); | ||
| var grainIdString = $"{grainId.ToString()}/Children"; | ||
| return GetEventBaseStream(grainIdString); | ||
| } | ||
|
|
||
| private IAsyncStream<EventWrapperBase> GetEventBaseStream(string streamKey) | ||
| { | ||
| var streamId = StreamId.Create(AevatarOptions!.StreamNamespace, streamKey); | ||
| return StreamProvider.GetStream<EventWrapperBase>(streamId); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't find the use of this anywhere