44using Microsoft . AspNetCore . Mvc ;
55using Microsoft . AspNetCore . SignalR ;
66using Microsoft . Extensions . Logging ;
7- using System ;
8- using System . Collections . Generic ;
97using System . Linq ;
10- using System . Threading . Tasks ;
118using WART_Core . Entity ;
129using WART_Core . Filters ;
10+ using Microsoft . Extensions . DependencyInjection ;
11+ using WART_Core . Services ;
1312
1413namespace WART_Core . Controllers
1514{
@@ -19,6 +18,8 @@ public abstract class WartBaseController<THub> : Controller where THub : Hub
1918 private readonly IHubContext < THub > _hubContext ;
2019 private const string RouteDataKey = "REQUEST" ;
2120
21+ private WartEventQueueService _eventQueue ;
22+
2223 protected WartBaseController ( IHubContext < THub > hubContext , ILogger logger )
2324 {
2425 _hubContext = hubContext ;
@@ -39,7 +40,7 @@ public override void OnActionExecuting(ActionExecutingContext context)
3940 /// Processes the executed action and sends the event to the SignalR hub if applicable.
4041 /// </summary>
4142 /// <param name="context">The action executed context.</param>
42- public override async void OnActionExecuted ( ActionExecutedContext context )
43+ public override void OnActionExecuted ( ActionExecutedContext context )
4344 {
4445 if ( context ? . Result is ObjectResult objectResult )
4546 {
@@ -52,107 +53,13 @@ public override async void OnActionExecuted(ActionExecutedContext context)
5253 var response = objectResult . Value ;
5354
5455 var wartEvent = new WartEvent ( request , response , httpMethod , httpPath , remoteAddress ) ;
55- await SendToHub ( wartEvent , [ .. context . Filters ] ) ;
56- }
57- }
58-
59- base . OnActionExecuted ( context ) ;
60- }
6156
62- /// <summary>
63- /// Sends the current event to the SignalR hub.
64- /// </summary>
65- /// <param name="wartEvent">The current WartEvent.</param>
66- /// <param name="filters">The list of filters applied to the request.</param>
67- private async Task SendToHub ( WartEvent wartEvent , List < IFilterMetadata > filters )
68- {
69- try
70- {
71- // Retrieve the target groups based on the filters.
72- var groups = GetTargetGroups ( filters ) ;
73-
74- // If there are groups specified, send the event to each group in parallel.
75- if ( groups . Any ( ) )
76- {
77- // Sending events to multiple groups in parallel.
78- var tasks = groups . Select ( group => SendEventToGroup ( wartEvent , group ) ) ;
79- await Task . WhenAll ( tasks ) ;
57+ _eventQueue = context . HttpContext ? . RequestServices . GetService < WartEventQueueService > ( ) ;
58+ _eventQueue ? . Enqueue ( new WartEventWithFilters ( wartEvent , [ .. context . Filters ] ) ) ;
8059 }
81- else
82- {
83- // If no specific groups are defined, send the event to all connected clients.
84- await SendEventToAllClients ( wartEvent ) ;
85- }
86- }
87- catch ( Exception ex )
88- {
89- _logger ? . LogError ( ex , "Error sending WartEvent to clients" ) ;
9060 }
91- }
92-
93- /// <summary>
94- /// Retrieves the list of groups that the WartEvent should be sent to, based on the provided filters.
95- /// </summary>
96- /// <param name="filters">The list of filters that may contain group-related information.</param>
97- /// <returns>A list of group names to send the WartEvent to.</returns>
98- private IEnumerable < string > GetTargetGroups ( List < IFilterMetadata > filters )
99- {
100- var groups = new List < string > ( ) ;
10161
102- // Check if there is a GroupWartAttribute filter indicating groups.
103- if ( filters . Any ( f => f . GetType ( ) . Name == nameof ( GroupWartAttribute ) ) )
104- {
105- var wartGroup = filters . FirstOrDefault ( f => f . GetType ( ) == typeof ( GroupWartAttribute ) ) as GroupWartAttribute ;
106- if ( wartGroup != null )
107- {
108- groups . AddRange ( wartGroup . GroupNames ) ;
109- }
110- }
111-
112- return groups ;
113- }
114-
115- /// <summary>
116- /// Sends the WartEvent to a specific group of clients.
117- /// </summary>
118- /// <param name="wartEvent">The WartEvent to send to the group.</param>
119- /// <param name="group">The group name to which the event will be sent.</param>
120- /// <returns>A Task representing the asynchronous operation.</returns>
121- private async Task SendEventToGroup ( WartEvent wartEvent , string group )
122- {
123- try
124- {
125- await _hubContext ? . Clients
126- . Group ( group )
127- . SendAsync ( "Send" , wartEvent . ToString ( ) ) ;
128-
129- _logger ? . LogInformation ( $ "Group: { group } , WartEvent: { wartEvent } ") ;
130- }
131- catch ( Exception ex )
132- {
133- _logger ? . LogError ( ex , $ "Error sending WartEvent to group { group } ") ;
134- }
135- }
136-
137- /// <summary>
138- /// Sends the WartEvent to all connected clients.
139- /// </summary>
140- /// <param name="wartEvent">The WartEvent to send to all clients.</param>
141- /// <returns>A Task representing the asynchronous operation.</returns>
142- private async Task SendEventToAllClients ( WartEvent wartEvent )
143- {
144- try
145- {
146- // Send the WartEvent to all connected clients using SignalR.
147- await _hubContext ? . Clients . All
148- . SendAsync ( "Send" , wartEvent . ToString ( ) ) ;
149-
150- _logger ? . LogInformation ( "Event: {EventName}, Details: {EventDetails}" , nameof ( WartEvent ) , wartEvent . ToString ( ) ) ;
151- }
152- catch ( Exception ex )
153- {
154- _logger ? . LogError ( ex , "Error sending WartEvent to all clients" ) ;
155- }
62+ base . OnActionExecuted ( context ) ;
15663 }
15764 }
15865}
0 commit comments