| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Common.Multitenancy; |
| | | 4 | | |
| | 1 | 5 | | public class TenantEventsManager( |
| | 1 | 6 | | IEnumerable<ITenantActivatedEvent> tenantActivatedEvents, |
| | 1 | 7 | | IEnumerable<ITenantDeactivatedEvent> tenantDeactivatedEvents, |
| | 1 | 8 | | IEnumerable<ITenantDeletedEvent> tenantDeletedEvents, |
| | 1 | 9 | | ILogger<TenantEventsManager> logger) |
| | | 10 | | { |
| | | 11 | | public async Task TenantActivatedAsync(TenantActivatedEventArgs args) |
| | | 12 | | { |
| | 1 | 13 | | await ExecuteEventHandlersAsync( |
| | 1 | 14 | | tenantActivatedEvents, |
| | 4 | 15 | | (handler, eventArgs) => handler.TenantActivatedAsync(eventArgs), |
| | 1 | 16 | | args, |
| | 1 | 17 | | "activated"); |
| | 1 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task TenantDeactivatedAsync(TenantDeactivatedEventArgs args) |
| | | 21 | | { |
| | 1 | 22 | | await ExecuteEventHandlersAsync( |
| | 1 | 23 | | tenantDeactivatedEvents, |
| | 2 | 24 | | (handler, eventArgs) => handler.TenantDeactivatedAsync(eventArgs), |
| | 1 | 25 | | args, |
| | 1 | 26 | | "deactivated"); |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task TenantDeletedAsync(TenantDeletedEventArgs args) |
| | | 30 | | { |
| | 0 | 31 | | await ExecuteEventHandlersAsync( |
| | 0 | 32 | | tenantDeletedEvents, |
| | 0 | 33 | | (handler, eventArgs) => handler.TenantDeletedAsync(eventArgs), |
| | 0 | 34 | | args, |
| | 0 | 35 | | "deleted"); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | private async Task ExecuteEventHandlersAsync<THandler, TArgs>( |
| | | 39 | | IEnumerable<THandler> handlers, |
| | | 40 | | Func<THandler, TArgs, Task> handlerAction, |
| | | 41 | | TArgs args, |
| | | 42 | | string eventType) |
| | | 43 | | { |
| | 16 | 44 | | foreach (var handler in handlers) |
| | | 45 | | { |
| | | 46 | | try |
| | | 47 | | { |
| | 6 | 48 | | await handlerAction(handler, args); |
| | 6 | 49 | | } |
| | 0 | 50 | | catch (Exception e) |
| | | 51 | | { |
| | 0 | 52 | | logger.LogError(e, "Error occurred while processing tenant {EventType} event.", eventType); |
| | 0 | 53 | | } |
| | | 54 | | } |
| | 2 | 55 | | } |
| | | 56 | | } |