| | | 1 | | using System; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using MediaBrowser.Controller; |
| | | 4 | | using MediaBrowser.Controller.Events; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace Jellyfin.Server.Implementations.Events |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Handles the firing of events. |
| | | 12 | | /// </summary> |
| | | 13 | | public class EventManager : IEventManager |
| | | 14 | | { |
| | | 15 | | private readonly ILogger<EventManager> _logger; |
| | | 16 | | private readonly IServerApplicationHost _appHost; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="EventManager"/> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="logger">The logger.</param> |
| | | 22 | | /// <param name="appHost">The application host.</param> |
| | | 23 | | public EventManager(ILogger<EventManager> logger, IServerApplicationHost appHost) |
| | | 24 | | { |
| | 21 | 25 | | _logger = logger; |
| | 21 | 26 | | _appHost = appHost; |
| | 21 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public void Publish<T>(T eventArgs) |
| | | 31 | | where T : EventArgs |
| | | 32 | | { |
| | 15 | 33 | | PublishInternal(eventArgs).GetAwaiter().GetResult(); |
| | 15 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public async Task PublishAsync<T>(T eventArgs) |
| | | 38 | | where T : EventArgs |
| | | 39 | | { |
| | | 40 | | await PublishInternal(eventArgs).ConfigureAwait(false); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private async Task PublishInternal<T>(T eventArgs) |
| | | 44 | | where T : EventArgs |
| | | 45 | | { |
| | | 46 | | using var scope = _appHost.ServiceProvider?.CreateScope(); |
| | | 47 | | if (scope is null) |
| | | 48 | | { |
| | | 49 | | return; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | foreach (var service in scope.ServiceProvider.GetServices<IEventConsumer<T>>()) |
| | | 53 | | { |
| | | 54 | | try |
| | | 55 | | { |
| | | 56 | | await service.OnEvent(eventArgs).ConfigureAwait(false); |
| | | 57 | | } |
| | | 58 | | catch (Exception e) |
| | | 59 | | { |
| | | 60 | | _logger.LogError(e, "Uncaught exception in EventConsumer {Type}: ", service.GetType()); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | } |