@@ -21,8 +21,7 @@ public class PerperStartup
2121 public string Agent { get ; }
2222 public bool UseInstances { get ; set ; } = false ;
2323 private readonly List < Func < Task > > initHandlers = new ( ) ;
24- private readonly Dictionary < string , Func < Task > > callHandlers = new ( ) ;
25- private readonly Dictionary < string , Func < Task > > streamHandlers = new ( ) ;
24+ private readonly Dictionary < string , Func < Task > > executionHandlers = new ( ) ;
2625
2726 public PerperStartup ( string agent ) => Agent = agent ;
2827
@@ -32,15 +31,9 @@ public PerperStartup AddInitHandler(Func<Task> handler)
3231 return this ;
3332 }
3433
35- public PerperStartup AddCallHandler ( string @delegate , Func < Task > handler )
34+ public PerperStartup AddHandler ( string @delegate , Func < Task > handler )
3635 {
37- callHandlers . Add ( @delegate , handler ) ;
38- return this ;
39- }
40-
41- public PerperStartup AddStreamHandler ( string @delegate , Func < Task > handler )
42- {
43- streamHandlers . Add ( @delegate , handler ) ;
36+ executionHandlers . Add ( @delegate , handler ) ;
4437 return this ;
4538 }
4639
@@ -54,7 +47,7 @@ public PerperStartup WithInstances()
5447
5548 public async Task RunAsync ( CancellationToken cancellationToken = default )
5649 {
57- await EnterServicesContext ( Agent , ( ) => RunInServiceContext ( cancellationToken ) , UseInstances ) . ConfigureAwait ( false ) ;
50+ await EnterServicesContext ( ( ) => RunInServiceContext ( cancellationToken ) ) . ConfigureAwait ( false ) ;
5851 }
5952
6053 public static Task RunAsync ( string agent , CancellationToken cancellationToken = default )
@@ -70,31 +63,24 @@ public static Task RunAsync(string agent, string rootNamespace, CancellationToke
7063 #endregion RunAsync
7164 #region Services
7265
73- public static async Task EnterServicesContext ( string agent , Func < Task > context , bool useInstance = false )
66+ public static async Task EnterServicesContext ( Func < Task > context )
7467 {
75- var ( cacheService , notificationService ) = await EstablishConnection ( agent , useInstance ) . ConfigureAwait ( false ) ;
68+ var fabricService = await EstablishConnection ( ) . ConfigureAwait ( false ) ;
7669
77- AsyncLocals . SetConnection ( cacheService , notificationService ) ;
70+ AsyncLocals . SetConnection ( fabricService ) ;
7871
7972 await context ( ) . ConfigureAwait ( false ) ;
8073
81- await notificationService . StopAsync ( ) . ConfigureAwait ( false ) ;
82- notificationService . Dispose ( ) ;
74+ await fabricService . DisposeAsync ( ) . ConfigureAwait ( false ) ;
8375 }
8476
85- public static async Task < ( CacheService , NotificationService ) > EstablishConnection ( string agent , bool useInstance = false )
77+ public static async Task < FabricService > EstablishConnection ( )
8678 {
8779 var apacheIgniteEndpoint = Environment . GetEnvironmentVariable ( "APACHE_IGNITE_ENDPOINT" ) ?? "127.0.0.1:10800" ;
8880 var fabricGrpcAddress = Environment . GetEnvironmentVariable ( "PERPER_FABRIC_ENDPOINT" ) ?? "http://127.0.0.1:40400" ;
89- string ? instance = null ;
9081
9182 Console . WriteLine ( $ "APACHE_IGNITE_ENDPOINT: { apacheIgniteEndpoint } ") ;
9283 Console . WriteLine ( $ "PERPER_FABRIC_ENDPOINT: { fabricGrpcAddress } ") ;
93- if ( useInstance )
94- {
95- instance = Environment . GetEnvironmentVariable ( "X_PERPER_INSTANCE" ) ?? "" ;
96- Console . WriteLine ( $ "X_PERPER_INSTANCE: { instance } ") ;
97- }
9884
9985 var igniteConfiguration = new IgniteClientConfiguration
10086 {
@@ -107,85 +93,73 @@ public static async Task EnterServicesContext(string agent, Func<Task> context,
10793 }
10894 } ;
10995
110- AppContext . SetSwitch ( "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport" , true ) ;
111- var grpcChannel = GrpcChannel . ForAddress ( fabricGrpcAddress ) ;
112-
11396 var ignite = await Policy
11497 . HandleInner < System . Net . Sockets . SocketException > ( )
11598 . WaitAndRetryAsync ( 10 ,
11699 attempt => TimeSpan . FromSeconds ( Math . Pow ( 2 , attempt - 2 ) ) ,
117100 ( exception , timespan ) => Console . WriteLine ( "Failed to connect to Ignite, retrying in {0}s" , timespan . TotalSeconds ) )
118101 . ExecuteAsync ( ( ) => Task . Run ( ( ) => Ignition . StartClient ( igniteConfiguration ) ) ) . ConfigureAwait ( false ) ;
119102
120- var cacheService = new CacheService ( ignite ) ;
121- var notificationService = new NotificationService ( ignite , grpcChannel , agent , instance ) ;
103+ AppContext . SetSwitch ( "System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport" , true ) ;
104+ var grpcChannel = GrpcChannel . ForAddress ( fabricGrpcAddress ) ;
122105
123- await Policy
124- . Handle < Grpc . Core . RpcException > ( ex => ex . Status . DebugException is System . Net . Http . HttpRequestException )
125- . WaitAndRetryAsync ( 10 ,
126- attempt => TimeSpan . FromSeconds ( Math . Pow ( 2 , attempt - 2 ) ) ,
127- ( exception , timespan ) => Console . WriteLine ( "Failed to connect to GRPC, retrying in {0}s" , timespan . TotalSeconds ) )
128- . ExecuteAsync ( notificationService . StartAsync ) . ConfigureAwait ( false ) ;
106+ return new FabricService ( ignite , grpcChannel ) ;
107+ }
129108
130- return ( cacheService , notificationService ) ;
109+ public static string GetConfiguredInstance ( )
110+ {
111+ return Environment . GetEnvironmentVariable ( "X_PERPER_INSTANCE" ) ?? "" ;
131112 }
132113
133114 #endregion Services
134115 #region ListenNotifications
135116
136117 public Task RunInServiceContext ( CancellationToken cancellationToken = default )
137118 {
119+ var instance = UseInstances ? null : GetConfiguredInstance ( ) ;
120+
121+ if ( instance != null )
122+ {
123+ Console . WriteLine ( $ "X_PERPER_INSTANCE: { instance } ") ;
124+ }
125+
138126 var taskCollection = new TaskCollection ( ) ;
139127
140- callHandlers . TryAdd ( PerperContext . StartupFunctionName , async ( ) =>
128+ executionHandlers . TryAdd ( PerperContext . StartupFunctionName , async ( ) =>
141129 {
142- await AsyncLocals . CacheService . CallWriteFinished ( AsyncLocals . Execution ) . ConfigureAwait ( false ) ;
130+ await AsyncLocals . FabricService . WriteExecutionFinished ( AsyncLocals . Execution ) . ConfigureAwait ( false ) ;
143131 } ) ;
144132
133+ var initInstance = instance ?? $ "{ Agent } -init";
134+ var initExecution = new FabricExecution ( Agent , initInstance , "Init" , $ "{ initInstance } -init", cancellationToken ) ;
145135 foreach ( var handler in initHandlers )
146136 {
147- taskCollection . Add ( AsyncLocals . EnterContext ( $ "{ AsyncLocals . Agent } -init", $ "Init-init", handler ) ) ;
148- }
149-
150- foreach ( var ( @delegate , handler ) in callHandlers )
151- {
152- ListenCallNotifications ( taskCollection , @delegate , handler , cancellationToken ) ;
137+ taskCollection . Add ( async ( ) =>
138+ {
139+ AsyncLocals . SetExecution ( initExecution ) ;
140+ await handler ( ) . ConfigureAwait ( false ) ;
141+ } ) ;
153142 }
154143
155- foreach ( var ( @delegate , handler ) in streamHandlers )
144+ foreach ( var ( @delegate , handler ) in executionHandlers )
156145 {
157- ListenStreamNotifications ( taskCollection , @delegate , handler , cancellationToken ) ;
146+ ListenExecutions ( taskCollection , Agent , instance , @delegate , handler , cancellationToken ) ;
158147 }
159148
160149 return taskCollection . GetTask ( ) ;
161150 }
162151
163- public static void ListenCallNotifications ( TaskCollection taskCollection , string @delegate , Func < Task > handler , CancellationToken cancellationToken )
164- {
165- taskCollection . Add ( async ( ) =>
166- {
167- await foreach ( var ( key , notification ) in AsyncLocals . NotificationService . GetCallTriggerNotifications ( @delegate ) . ReadAllAsync ( cancellationToken ) )
168- {
169- taskCollection . Add ( AsyncLocals . EnterContext ( notification . Instance , notification . Call , async ( ) =>
170- {
171- await handler ( ) . ConfigureAwait ( false ) ;
172- await AsyncLocals . NotificationService . ConsumeNotification ( key ) . ConfigureAwait ( false ) ; // TODO?
173- } ) ) ;
174- }
175- } ) ;
176- }
177-
178- public static void ListenStreamNotifications ( TaskCollection taskCollection , string @delegate , Func < Task > handler , CancellationToken cancellationToken )
152+ public static void ListenExecutions ( TaskCollection taskCollection , string agent , string ? instance , string @delegate , Func < Task > handler , CancellationToken cancellationToken )
179153 {
180154 taskCollection . Add ( async ( ) =>
181155 {
182- await foreach ( var ( key , notification ) in AsyncLocals . NotificationService . GetStreamTriggerNotifications ( @delegate ) . ReadAllAsync ( cancellationToken ) )
156+ await foreach ( var execution in AsyncLocals . FabricService . GetExecutionsReader ( agent , instance , @delegate ) . ReadAllAsync ( cancellationToken ) )
183157 {
184- taskCollection . Add ( AsyncLocals . EnterContext ( notification . Instance , notification . Stream , async ( ) =>
158+ taskCollection . Add ( async ( ) =>
185159 {
160+ AsyncLocals . SetExecution ( execution ) ;
186161 await handler ( ) . ConfigureAwait ( false ) ;
187- await AsyncLocals . NotificationService . ConsumeNotification ( key ) . ConfigureAwait ( false ) ; // TODO?
188- } ) ) ;
162+ } ) ;
189163 }
190164 } ) ;
191165 }
0 commit comments