using System;
using System.Threading.Tasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Events;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Implementations.Events
{
///
/// Handles the firing of events.
///
public class EventManager : IEventManager
{
private readonly ILogger _logger;
private readonly IServerApplicationHost _appHost;
///
/// Initializes a new instance of the class.
///
/// The logger.
/// The application host.
public EventManager(ILogger logger, IServerApplicationHost appHost)
{
_logger = logger;
_appHost = appHost;
}
///
public void Publish(T eventArgs)
where T : EventArgs
{
Task.WaitAll(PublishInternal(eventArgs));
}
///
public async Task PublishAsync(T eventArgs)
where T : EventArgs
{
await PublishInternal(eventArgs).ConfigureAwait(false);
}
private async Task PublishInternal(T eventArgs)
where T : EventArgs
{
using var scope = _appHost.ServiceProvider.CreateScope();
foreach (var service in scope.ServiceProvider.GetServices>())
{
try
{
await service.OnEvent(eventArgs).ConfigureAwait(false);
}
catch (Exception e)
{
_logger.LogError(e, "Uncaught exception in EventConsumer {type}: ", service.GetType());
}
}
}
}
}