jellyfin/Emby.Server.Implementations/EntryPoints/ServerEventNotifier.cs

230 lines
8.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
2019-01-27 14:40:37 +00:00
using System.Threading.Tasks;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Updates;
2013-03-07 05:34:00 +00:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
2013-10-03 01:22:50 +00:00
using MediaBrowser.Controller.Session;
2014-05-08 20:09:53 +00:00
using MediaBrowser.Model.Events;
2016-10-23 19:47:34 +00:00
using MediaBrowser.Model.Tasks;
namespace Emby.Server.Implementations.EntryPoints
{
/// <summary>
2019-11-01 17:38:54 +00:00
/// Class WebSocketEvents.
/// </summary>
public class ServerEventNotifier : IServerEntryPoint
{
/// <summary>
/// The user manager.
/// </summary>
private readonly IUserManager _userManager;
/// <summary>
/// The installation manager.
/// </summary>
private readonly IInstallationManager _installationManager;
/// <summary>
/// The kernel.
/// </summary>
2013-03-07 05:34:00 +00:00
private readonly IServerApplicationHost _appHost;
2013-04-15 18:45:58 +00:00
/// <summary>
/// The task manager.
2013-04-15 18:45:58 +00:00
/// </summary>
2013-03-07 05:34:00 +00:00
private readonly ITaskManager _taskManager;
2013-04-15 18:45:58 +00:00
private readonly ISessionManager _sessionManager;
2013-10-03 01:22:50 +00:00
2019-11-01 17:38:54 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="ServerEventNotifier"/> class.
/// </summary>
/// <param name="appHost">The application host.</param>
/// <param name="userManager">The user manager.</param>
/// <param name="installationManager">The installation manager.</param>
/// <param name="taskManager">The task manager.</param>
/// <param name="sessionManager">The session manager.</param>
public ServerEventNotifier(
IServerApplicationHost appHost,
IUserManager userManager,
IInstallationManager installationManager,
ITaskManager taskManager,
ISessionManager sessionManager)
{
_userManager = userManager;
_installationManager = installationManager;
2013-03-07 05:34:00 +00:00
_appHost = appHost;
_taskManager = taskManager;
2013-10-03 01:22:50 +00:00
_sessionManager = sessionManager;
}
2019-11-01 17:38:54 +00:00
/// <inheritdoc />
2019-01-27 14:40:37 +00:00
public Task RunAsync()
{
2019-11-01 17:38:54 +00:00
_userManager.UserDeleted += OnUserDeleted;
_userManager.UserUpdated += OnUserUpdated;
_userManager.UserPolicyUpdated += OnUserPolicyUpdated;
_userManager.UserConfigurationUpdated += OnUserConfigurationUpdated;
2019-11-01 17:38:54 +00:00
_appHost.HasPendingRestartChanged += OnHasPendingRestartChanged;
2019-11-01 17:38:54 +00:00
_installationManager.PluginUninstalled += OnPluginUninstalled;
_installationManager.PackageInstalling += OnPackageInstalling;
_installationManager.PackageInstallationCancelled += OnPackageInstallationCancelled;
_installationManager.PackageInstallationCompleted += OnPackageInstallationCompleted;
_installationManager.PackageInstallationFailed += OnPackageInstallationFailed;
2013-03-07 05:34:00 +00:00
2019-11-01 17:38:54 +00:00
_taskManager.TaskCompleted += OnTaskCompleted;
2019-01-27 14:40:37 +00:00
return Task.CompletedTask;
2013-03-07 05:34:00 +00:00
}
2019-11-01 17:38:54 +00:00
private void OnPackageInstalling(object sender, InstallationEventArgs e)
2013-03-07 05:34:00 +00:00
{
2018-09-12 17:26:21 +00:00
SendMessageToAdminSessions("PackageInstalling", e.InstallationInfo);
2013-03-07 05:34:00 +00:00
}
2019-11-01 17:38:54 +00:00
private void OnPackageInstallationCancelled(object sender, InstallationEventArgs e)
2013-03-07 05:34:00 +00:00
{
2018-09-12 17:26:21 +00:00
SendMessageToAdminSessions("PackageInstallationCancelled", e.InstallationInfo);
}
2019-11-01 17:38:54 +00:00
private void OnPackageInstallationCompleted(object sender, InstallationEventArgs e)
{
2018-09-12 17:26:21 +00:00
SendMessageToAdminSessions("PackageInstallationCompleted", e.InstallationInfo);
}
2019-11-01 17:38:54 +00:00
private void OnPackageInstallationFailed(object sender, InstallationFailedEventArgs e)
{
2018-09-12 17:26:21 +00:00
SendMessageToAdminSessions("PackageInstallationFailed", e.InstallationInfo);
}
2019-11-01 17:38:54 +00:00
private void OnTaskCompleted(object sender, TaskCompletionEventArgs e)
{
2018-09-12 17:26:21 +00:00
SendMessageToAdminSessions("ScheduledTaskEnded", e.Result);
}
/// <summary>
/// Installations the manager_ plugin uninstalled.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
2019-11-01 17:38:54 +00:00
private void OnPluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
{
2018-09-12 17:26:21 +00:00
SendMessageToAdminSessions("PluginUninstalled", e.Argument.GetPluginInfo());
}
/// <summary>
/// Handles the HasPendingRestartChanged event of the kernel control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
2019-11-01 17:38:54 +00:00
private void OnHasPendingRestartChanged(object sender, EventArgs e)
{
_sessionManager.SendRestartRequiredNotification(CancellationToken.None);
}
/// <summary>
/// Users the manager_ user updated.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
2019-11-01 17:38:54 +00:00
private void OnUserUpdated(object sender, GenericEventArgs<User> e)
{
2014-08-15 16:35:41 +00:00
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserUpdated", dto);
}
/// <summary>
/// Users the manager_ user deleted.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
2019-11-01 17:38:54 +00:00
private void OnUserDeleted(object sender, GenericEventArgs<User> e)
{
SendMessageToUserSession(e.Argument, "UserDeleted", e.Argument.Id.ToString("N", CultureInfo.InvariantCulture));
}
2019-11-01 17:38:54 +00:00
private void OnUserPolicyUpdated(object sender, GenericEventArgs<User> e)
2018-09-12 17:26:21 +00:00
{
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserPolicyUpdated", dto);
}
2019-11-01 17:38:54 +00:00
private void OnUserConfigurationUpdated(object sender, GenericEventArgs<User> e)
{
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserConfigurationUpdated", dto);
}
2018-09-12 17:26:21 +00:00
private async void SendMessageToAdminSessions<T>(string name, T data)
{
try
{
2019-10-25 10:47:20 +00:00
await _sessionManager.SendMessageToAdminSessions(name, data, CancellationToken.None).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
}
catch (Exception)
{
2018-12-20 12:11:26 +00:00
2018-09-12 17:26:21 +00:00
}
}
private async void SendMessageToUserSession<T>(User user, string name, T data)
{
2018-09-12 17:26:21 +00:00
try
{
2019-11-01 17:38:54 +00:00
await _sessionManager.SendMessageToUserSessions(
new List<Guid> { user.Id },
name,
data,
CancellationToken.None).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
}
catch (Exception)
{
2018-12-20 12:11:26 +00:00
2018-09-12 17:26:21 +00:00
}
}
2019-11-01 17:38:54 +00:00
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
2019-11-01 17:38:54 +00:00
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
2019-11-01 17:38:54 +00:00
_userManager.UserDeleted -= OnUserDeleted;
_userManager.UserUpdated -= OnUserUpdated;
_userManager.UserPolicyUpdated -= OnUserPolicyUpdated;
_userManager.UserConfigurationUpdated -= OnUserConfigurationUpdated;
_installationManager.PluginUninstalled -= OnPluginUninstalled;
_installationManager.PackageInstalling -= OnPackageInstalling;
_installationManager.PackageInstallationCancelled -= OnPackageInstallationCancelled;
_installationManager.PackageInstallationCompleted -= OnPackageInstallationCompleted;
_installationManager.PackageInstallationFailed -= OnPackageInstallationFailed;
_appHost.HasPendingRestartChanged -= OnHasPendingRestartChanged;
_taskManager.TaskCompleted -= OnTaskCompleted;
}
}
}
}