Notifier.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Common.Plugins;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Common.Updates;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Notifications;
  7. using MediaBrowser.Controller.Plugins;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Notifications;
  10. using System;
  11. using System.Linq;
  12. using System.Threading;
  13. using MediaBrowser.Model.Tasks;
  14. namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
  15. {
  16. /// <summary>
  17. /// Creates notifications for various system events
  18. /// </summary>
  19. public class Notifications : IServerEntryPoint
  20. {
  21. private readonly INotificationsRepository _notificationsRepo;
  22. private readonly IInstallationManager _installationManager;
  23. private readonly IUserManager _userManager;
  24. private readonly ILogger _logger;
  25. private readonly ITaskManager _taskManager;
  26. public Notifications(IInstallationManager installationManager, INotificationsRepository notificationsRepo, IUserManager userManager, ILogger logger, ITaskManager taskManager)
  27. {
  28. _installationManager = installationManager;
  29. _notificationsRepo = notificationsRepo;
  30. _userManager = userManager;
  31. _logger = logger;
  32. _taskManager = taskManager;
  33. }
  34. public void Run()
  35. {
  36. _installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
  37. _installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
  38. _installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
  39. _taskManager.TaskCompleted += _taskManager_TaskCompleted;
  40. }
  41. async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
  42. {
  43. var result = e.Argument;
  44. if (result.Status == TaskCompletionStatus.Failed)
  45. {
  46. foreach (var user in _userManager
  47. .Users
  48. .Where(i => i.Configuration.IsAdministrator)
  49. .ToList())
  50. {
  51. var notification = new Notification
  52. {
  53. UserId = user.Id,
  54. Category = "ScheduledTaskFailed",
  55. Name = result.Name + " failed",
  56. RelatedId = result.Name,
  57. Description = result.ErrorMessage,
  58. Level = NotificationLevel.Error
  59. };
  60. try
  61. {
  62. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  63. }
  64. catch (Exception ex)
  65. {
  66. _logger.ErrorException("Error adding notification", ex);
  67. }
  68. }
  69. }
  70. }
  71. async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
  72. {
  73. var plugin = e.Argument;
  74. foreach (var user in _userManager
  75. .Users
  76. .Where(i => i.Configuration.IsAdministrator)
  77. .ToList())
  78. {
  79. var notification = new Notification
  80. {
  81. UserId = user.Id,
  82. Category = "PluginUninstalled",
  83. Name = plugin.Name + " has been uninstalled",
  84. RelatedId = plugin.Id.ToString()
  85. };
  86. try
  87. {
  88. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  89. }
  90. catch (Exception ex)
  91. {
  92. _logger.ErrorException("Error adding notification", ex);
  93. }
  94. }
  95. }
  96. async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e)
  97. {
  98. var installationInfo = e.InstallationInfo;
  99. foreach (var user in _userManager
  100. .Users
  101. .Where(i => i.Configuration.IsAdministrator)
  102. .ToList())
  103. {
  104. var notification = new Notification
  105. {
  106. UserId = user.Id,
  107. Category = "PackageInstallationCompleted",
  108. Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
  109. RelatedId = installationInfo.Name,
  110. Description = e.PackageVersionInfo.description
  111. };
  112. try
  113. {
  114. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  115. }
  116. catch (Exception ex)
  117. {
  118. _logger.ErrorException("Error adding notification", ex);
  119. }
  120. }
  121. }
  122. async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
  123. {
  124. var installationInfo = e.InstallationInfo;
  125. foreach (var user in _userManager
  126. .Users
  127. .Where(i => i.Configuration.IsAdministrator)
  128. .ToList())
  129. {
  130. var notification = new Notification
  131. {
  132. UserId = user.Id,
  133. Category = "PackageInstallationFailed",
  134. Level = NotificationLevel.Error,
  135. Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
  136. RelatedId = installationInfo.Name,
  137. Description = e.Exception.Message
  138. };
  139. try
  140. {
  141. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  142. }
  143. catch (Exception ex)
  144. {
  145. _logger.ErrorException("Error adding notification", ex);
  146. }
  147. }
  148. }
  149. public void Dispose()
  150. {
  151. _installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
  152. _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
  153. }
  154. }
  155. }