SystemManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. private bool CanLaunchWebBrowser => Environment.UserInteractive
  46. && !_startupOptions.IsService
  47. && (OperatingSystem.IsWindows() || OperatingSystem.IsMacOS());
  48. /// <inheritdoc />
  49. public SystemInfo GetSystemInfo(HttpRequest request)
  50. {
  51. return new SystemInfo
  52. {
  53. HasPendingRestart = _applicationHost.HasPendingRestart,
  54. IsShuttingDown = _applicationLifetime.ApplicationStopping.IsCancellationRequested,
  55. Version = _applicationHost.ApplicationVersionString,
  56. WebSocketPortNumber = _applicationHost.HttpPort,
  57. CompletedInstallations = _installationManager.CompletedInstallations.ToArray(),
  58. Id = _applicationHost.SystemId,
  59. ProgramDataPath = _applicationPaths.ProgramDataPath,
  60. WebPath = _applicationPaths.WebPath,
  61. LogPath = _applicationPaths.LogDirectoryPath,
  62. ItemsByNamePath = _applicationPaths.InternalMetadataPath,
  63. InternalMetadataPath = _applicationPaths.InternalMetadataPath,
  64. CachePath = _applicationPaths.CachePath,
  65. CanLaunchWebBrowser = CanLaunchWebBrowser,
  66. TranscodingTempPath = _configurationManager.GetTranscodePath(),
  67. ServerName = _applicationHost.FriendlyName,
  68. LocalAddress = _applicationHost.GetSmartApiUrl(request),
  69. SupportsLibraryMonitor = true,
  70. PackageName = _startupOptions.PackageName
  71. };
  72. }
  73. /// <inheritdoc />
  74. public PublicSystemInfo GetPublicSystemInfo(HttpRequest request)
  75. {
  76. return new PublicSystemInfo
  77. {
  78. Version = _applicationHost.ApplicationVersionString,
  79. ProductName = _applicationHost.Name,
  80. Id = _applicationHost.SystemId,
  81. ServerName = _applicationHost.FriendlyName,
  82. LocalAddress = _applicationHost.GetSmartApiUrl(request),
  83. StartupWizardCompleted = _configurationManager.CommonConfiguration.IsStartupWizardCompleted
  84. };
  85. }
  86. /// <inheritdoc />
  87. public void Restart() => ShutdownInternal(true);
  88. /// <inheritdoc />
  89. public void Shutdown() => ShutdownInternal(false);
  90. private void ShutdownInternal(bool restart)
  91. {
  92. Task.Run(async () =>
  93. {
  94. await Task.Delay(100).ConfigureAwait(false);
  95. _applicationHost.ShouldRestart = restart;
  96. _applicationLifetime.StopApplication();
  97. });
  98. }
  99. }