SystemManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Common.Updates;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Model.System;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.Extensions.Hosting;
  10. namespace Emby.Server.Implementations;
  11. /// <inheritdoc />
  12. public class SystemManager : ISystemManager
  13. {
  14. private readonly IHostApplicationLifetime _applicationLifetime;
  15. private readonly IServerApplicationHost _applicationHost;
  16. private readonly IServerApplicationPaths _applicationPaths;
  17. private readonly IServerConfigurationManager _configurationManager;
  18. private readonly IStartupOptions _startupOptions;
  19. private readonly IInstallationManager _installationManager;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="SystemManager"/> class.
  22. /// </summary>
  23. /// <param name="applicationLifetime">Instance of <see cref="IHostApplicationLifetime"/>.</param>
  24. /// <param name="applicationHost">Instance of <see cref="IServerApplicationHost"/>.</param>
  25. /// <param name="applicationPaths">Instance of <see cref="IServerApplicationPaths"/>.</param>
  26. /// <param name="configurationManager">Instance of <see cref="IServerConfigurationManager"/>.</param>
  27. /// <param name="startupOptions">Instance of <see cref="IStartupOptions"/>.</param>
  28. /// <param name="installationManager">Instance of <see cref="IInstallationManager"/>.</param>
  29. public SystemManager(
  30. IHostApplicationLifetime applicationLifetime,
  31. IServerApplicationHost applicationHost,
  32. IServerApplicationPaths applicationPaths,
  33. IServerConfigurationManager configurationManager,
  34. IStartupOptions startupOptions,
  35. IInstallationManager installationManager)
  36. {
  37. _applicationLifetime = applicationLifetime;
  38. _applicationHost = applicationHost;
  39. _applicationPaths = applicationPaths;
  40. _configurationManager = configurationManager;
  41. _startupOptions = startupOptions;
  42. _installationManager = installationManager;
  43. }
  44. /// <inheritdoc />
  45. public SystemInfo GetSystemInfo(HttpRequest request)
  46. {
  47. return new SystemInfo
  48. {
  49. HasPendingRestart = _applicationHost.HasPendingRestart,
  50. IsShuttingDown = _applicationLifetime.ApplicationStopping.IsCancellationRequested,
  51. Version = _applicationHost.ApplicationVersionString,
  52. ProductName = _applicationHost.Name,
  53. WebSocketPortNumber = _applicationHost.HttpPort,
  54. CompletedInstallations = _installationManager.CompletedInstallations.ToArray(),
  55. Id = _applicationHost.SystemId,
  56. ProgramDataPath = _applicationPaths.ProgramDataPath,
  57. WebPath = _applicationPaths.WebPath,
  58. LogPath = _applicationPaths.LogDirectoryPath,
  59. ItemsByNamePath = _applicationPaths.InternalMetadataPath,
  60. InternalMetadataPath = _applicationPaths.InternalMetadataPath,
  61. CachePath = _applicationPaths.CachePath,
  62. TranscodingTempPath = _configurationManager.GetTranscodePath(),
  63. ServerName = _applicationHost.FriendlyName,
  64. LocalAddress = _applicationHost.GetSmartApiUrl(request),
  65. StartupWizardCompleted = _configurationManager.CommonConfiguration.IsStartupWizardCompleted,
  66. SupportsLibraryMonitor = true,
  67. PackageName = _startupOptions.PackageName,
  68. CastReceiverApplications = _configurationManager.Configuration.CastReceiverApplications
  69. };
  70. }
  71. /// <inheritdoc />
  72. public PublicSystemInfo GetPublicSystemInfo(HttpRequest request)
  73. {
  74. return new PublicSystemInfo
  75. {
  76. Version = _applicationHost.ApplicationVersionString,
  77. ProductName = _applicationHost.Name,
  78. Id = _applicationHost.SystemId,
  79. ServerName = _applicationHost.FriendlyName,
  80. LocalAddress = _applicationHost.GetSmartApiUrl(request),
  81. StartupWizardCompleted = _configurationManager.CommonConfiguration.IsStartupWizardCompleted
  82. };
  83. }
  84. /// <inheritdoc />
  85. public void Restart() => ShutdownInternal(true);
  86. /// <inheritdoc />
  87. public void Shutdown() => ShutdownInternal(false);
  88. private void ShutdownInternal(bool restart)
  89. {
  90. Task.Run(async () =>
  91. {
  92. await Task.Delay(100).ConfigureAwait(false);
  93. _applicationHost.ShouldRestart = restart;
  94. _applicationLifetime.StopApplication();
  95. });
  96. }
  97. }