2020-08-15 19:55:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Events;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-08-15 22:36:37 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-08-15 19:55:58 +00:00
|
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Implementations.Events
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the firing of events.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class EventManager : IEventManager
|
|
|
|
|
{
|
2020-08-15 22:36:37 +00:00
|
|
|
|
private readonly ILogger<EventManager> _logger;
|
2020-08-15 19:55:58 +00:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="EventManager"/> class.
|
|
|
|
|
/// </summary>
|
2020-08-15 22:36:37 +00:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2020-08-15 19:55:58 +00:00
|
|
|
|
/// <param name="appHost">The application host.</param>
|
2020-08-15 22:36:37 +00:00
|
|
|
|
public EventManager(ILogger<EventManager> logger, IServerApplicationHost appHost)
|
2020-08-15 19:55:58 +00:00
|
|
|
|
{
|
2020-08-15 22:36:37 +00:00
|
|
|
|
_logger = logger;
|
2020-08-15 19:55:58 +00:00
|
|
|
|
_appHost = appHost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Publish<T>(T eventArgs)
|
|
|
|
|
where T : EventArgs
|
|
|
|
|
{
|
2021-06-14 12:44:55 +00:00
|
|
|
|
PublishInternal(eventArgs).GetAwaiter().GetResult();
|
2020-08-15 19:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task PublishAsync<T>(T eventArgs)
|
|
|
|
|
where T : EventArgs
|
|
|
|
|
{
|
|
|
|
|
await PublishInternal(eventArgs).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task PublishInternal<T>(T eventArgs)
|
|
|
|
|
where T : EventArgs
|
|
|
|
|
{
|
2021-05-28 12:33:54 +00:00
|
|
|
|
using var scope = _appHost.ServiceProvider?.CreateScope();
|
|
|
|
|
if (scope == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-15 19:55:58 +00:00
|
|
|
|
foreach (var service in scope.ServiceProvider.GetServices<IEventConsumer<T>>())
|
|
|
|
|
{
|
2020-08-15 22:36:37 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await service.OnEvent(eventArgs).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Uncaught exception in EventConsumer {type}: ", service.GetType());
|
|
|
|
|
}
|
2020-08-15 19:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|