ServerEventNotifier.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Common.Plugins;
  5. using MediaBrowser.Common.Updates;
  6. using MediaBrowser.Controller;
  7. using MediaBrowser.Controller.Plugins;
  8. using MediaBrowser.Controller.Session;
  9. using MediaBrowser.Model.Updates;
  10. namespace Emby.Server.Implementations.EntryPoints
  11. {
  12. /// <summary>
  13. /// Class WebSocketEvents.
  14. /// </summary>
  15. public class ServerEventNotifier : IServerEntryPoint
  16. {
  17. /// <summary>
  18. /// The installation manager.
  19. /// </summary>
  20. private readonly IInstallationManager _installationManager;
  21. /// <summary>
  22. /// The kernel.
  23. /// </summary>
  24. private readonly IServerApplicationHost _appHost;
  25. private readonly ISessionManager _sessionManager;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="ServerEventNotifier"/> class.
  28. /// </summary>
  29. /// <param name="appHost">The application host.</param>
  30. /// <param name="installationManager">The installation manager.</param>
  31. /// <param name="sessionManager">The session manager.</param>
  32. public ServerEventNotifier(
  33. IServerApplicationHost appHost,
  34. IInstallationManager installationManager,
  35. ISessionManager sessionManager)
  36. {
  37. _installationManager = installationManager;
  38. _appHost = appHost;
  39. _sessionManager = sessionManager;
  40. }
  41. /// <inheritdoc />
  42. public Task RunAsync()
  43. {
  44. _appHost.HasPendingRestartChanged += OnHasPendingRestartChanged;
  45. _installationManager.PluginUninstalled += OnPluginUninstalled;
  46. _installationManager.PackageInstalling += OnPackageInstalling;
  47. _installationManager.PackageInstallationCancelled += OnPackageInstallationCancelled;
  48. _installationManager.PackageInstallationCompleted += OnPackageInstallationCompleted;
  49. _installationManager.PackageInstallationFailed += OnPackageInstallationFailed;
  50. return Task.CompletedTask;
  51. }
  52. private async void OnPackageInstalling(object sender, InstallationInfo e)
  53. {
  54. await SendMessageToAdminSessions("PackageInstalling", e).ConfigureAwait(false);
  55. }
  56. private async void OnPackageInstallationCancelled(object sender, InstallationInfo e)
  57. {
  58. await SendMessageToAdminSessions("PackageInstallationCancelled", e).ConfigureAwait(false);
  59. }
  60. private async void OnPackageInstallationCompleted(object sender, InstallationInfo e)
  61. {
  62. await SendMessageToAdminSessions("PackageInstallationCompleted", e).ConfigureAwait(false);
  63. }
  64. private async void OnPackageInstallationFailed(object sender, InstallationFailedEventArgs e)
  65. {
  66. await SendMessageToAdminSessions("PackageInstallationFailed", e.InstallationInfo).ConfigureAwait(false);
  67. }
  68. /// <summary>
  69. /// Installations the manager_ plugin uninstalled.
  70. /// </summary>
  71. /// <param name="sender">The sender.</param>
  72. /// <param name="e">The e.</param>
  73. private async void OnPluginUninstalled(object sender, IPlugin e)
  74. {
  75. await SendMessageToAdminSessions("PluginUninstalled", e).ConfigureAwait(false);
  76. }
  77. /// <summary>
  78. /// Handles the HasPendingRestartChanged event of the kernel control.
  79. /// </summary>
  80. /// <param name="sender">The source of the event.</param>
  81. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  82. private async void OnHasPendingRestartChanged(object sender, EventArgs e)
  83. {
  84. await _sessionManager.SendRestartRequiredNotification(CancellationToken.None).ConfigureAwait(false);
  85. }
  86. private async Task SendMessageToAdminSessions<T>(string name, T data)
  87. {
  88. try
  89. {
  90. await _sessionManager.SendMessageToAdminSessions(name, data, CancellationToken.None).ConfigureAwait(false);
  91. }
  92. catch (Exception)
  93. {
  94. }
  95. }
  96. /// <inheritdoc />
  97. public void Dispose()
  98. {
  99. Dispose(true);
  100. GC.SuppressFinalize(this);
  101. }
  102. /// <summary>
  103. /// Releases unmanaged and - optionally - managed resources.
  104. /// </summary>
  105. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  106. protected virtual void Dispose(bool dispose)
  107. {
  108. if (dispose)
  109. {
  110. _installationManager.PluginUninstalled -= OnPluginUninstalled;
  111. _installationManager.PackageInstalling -= OnPackageInstalling;
  112. _installationManager.PackageInstallationCancelled -= OnPackageInstallationCancelled;
  113. _installationManager.PackageInstallationCompleted -= OnPackageInstallationCompleted;
  114. _installationManager.PackageInstallationFailed -= OnPackageInstallationFailed;
  115. _appHost.HasPendingRestartChanged -= OnHasPendingRestartChanged;
  116. }
  117. }
  118. }
  119. }