BaseKernel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Events;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Model.System;
  5. using System;
  6. namespace MediaBrowser.Common.Kernel
  7. {
  8. /// <summary>
  9. /// Represents a shared base kernel for both the Ui and server apps
  10. /// </summary>
  11. public abstract class BaseKernel : IKernel
  12. {
  13. /// <summary>
  14. /// Occurs when [has pending restart changed].
  15. /// </summary>
  16. public event EventHandler HasPendingRestartChanged;
  17. #region ApplicationUpdated Event
  18. /// <summary>
  19. /// Occurs when [application updated].
  20. /// </summary>
  21. public event EventHandler<GenericEventArgs<Version>> ApplicationUpdated;
  22. /// <summary>
  23. /// Called when [application updated].
  24. /// </summary>
  25. /// <param name="newVersion">The new version.</param>
  26. public void OnApplicationUpdated(Version newVersion)
  27. {
  28. EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs<Version> { Argument = newVersion }, Logger);
  29. NotifyPendingRestart();
  30. }
  31. #endregion
  32. /// <summary>
  33. /// Gets or sets a value indicating whether this instance has changes that require the entire application to restart.
  34. /// </summary>
  35. /// <value><c>true</c> if this instance has pending application restart; otherwise, <c>false</c>.</value>
  36. public bool HasPendingRestart { get; private set; }
  37. /// <summary>
  38. /// Gets the UDP server port number.
  39. /// This can't be configurable because then the user would have to configure their client to discover the server.
  40. /// </summary>
  41. /// <value>The UDP server port number.</value>
  42. public abstract int UdpServerPortNumber { get; }
  43. /// <summary>
  44. /// Gets the name of the web application that can be used for url building.
  45. /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
  46. /// </summary>
  47. /// <value>The name of the web application.</value>
  48. public string WebApplicationName
  49. {
  50. get { return "mediabrowser"; }
  51. }
  52. /// <summary>
  53. /// Gets the HTTP server URL prefix.
  54. /// </summary>
  55. /// <value>The HTTP server URL prefix.</value>
  56. public virtual string HttpServerUrlPrefix
  57. {
  58. get
  59. {
  60. return "http://+:" + _configurationManager.CommonConfiguration.HttpServerPortNumber + "/" + WebApplicationName + "/";
  61. }
  62. }
  63. /// <summary>
  64. /// Gets the kernel context. Subclasses will have to override.
  65. /// </summary>
  66. /// <value>The kernel context.</value>
  67. public abstract KernelContext KernelContext { get; }
  68. /// <summary>
  69. /// Gets the logger.
  70. /// </summary>
  71. /// <value>The logger.</value>
  72. protected ILogger Logger { get; private set; }
  73. /// <summary>
  74. /// Gets or sets the application host.
  75. /// </summary>
  76. /// <value>The application host.</value>
  77. protected IApplicationHost ApplicationHost { get; private set; }
  78. private readonly IConfigurationManager _configurationManager;
  79. /// <summary>
  80. /// Initializes a new instance of the <see cref="BaseKernel" /> class.
  81. /// </summary>
  82. /// <param name="appHost">The app host.</param>
  83. /// <param name="logManager">The log manager.</param>
  84. protected BaseKernel(IApplicationHost appHost, ILogManager logManager, IConfigurationManager configurationManager)
  85. {
  86. ApplicationHost = appHost;
  87. _configurationManager = configurationManager;
  88. Logger = logManager.GetLogger("Kernel");
  89. }
  90. /// <summary>
  91. /// Initializes the Kernel
  92. /// </summary>
  93. /// <returns>Task.</returns>
  94. public void Init()
  95. {
  96. ReloadInternal();
  97. Logger.Info("Kernel.Init Complete");
  98. }
  99. /// <summary>
  100. /// Performs initializations that can be reloaded at anytime
  101. /// </summary>
  102. /// <returns>Task.</returns>
  103. protected virtual void ReloadInternal()
  104. {
  105. }
  106. /// <summary>
  107. /// Notifies that the kernel that a change has been made that requires a restart
  108. /// </summary>
  109. public void NotifyPendingRestart()
  110. {
  111. HasPendingRestart = true;
  112. EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger);
  113. }
  114. /// <summary>
  115. /// Performs the pending restart.
  116. /// </summary>
  117. /// <returns>Task.</returns>
  118. public void PerformPendingRestart()
  119. {
  120. if (HasPendingRestart)
  121. {
  122. Logger.Info("Restarting the application");
  123. ApplicationHost.Restart();
  124. }
  125. else
  126. {
  127. Logger.Info("PerformPendingRestart - not needed");
  128. }
  129. }
  130. /// <summary>
  131. /// Gets the system status.
  132. /// </summary>
  133. /// <returns>SystemInfo.</returns>
  134. public virtual SystemInfo GetSystemInfo()
  135. {
  136. return new SystemInfo
  137. {
  138. HasPendingRestart = HasPendingRestart,
  139. Version = ApplicationHost.ApplicationVersion.ToString(),
  140. IsNetworkDeployed = ApplicationHost.CanSelfUpdate,
  141. WebSocketPortNumber = ApplicationHost.Resolve<IServerManager>().WebSocketPortNumber,
  142. SupportsNativeWebSocket = ApplicationHost.Resolve<IServerManager>().SupportsNativeWebSocket,
  143. FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray()
  144. };
  145. }
  146. }
  147. }