SystemManager.cs 4.4 KB

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