SystemService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Serialization;
  6. using MediaBrowser.Model.System;
  7. using MediaBrowser.Server.Implementations.HttpServer;
  8. using ServiceStack.ServiceHost;
  9. using System;
  10. using System.IO;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Api
  13. {
  14. /// <summary>
  15. /// Class GetSystemInfo
  16. /// </summary>
  17. [Route("/System/Info", "GET")]
  18. [ServiceStack.ServiceHost.Api(Description = "Gets information about the server")]
  19. public class GetSystemInfo : IReturn<SystemInfo>
  20. {
  21. }
  22. /// <summary>
  23. /// Class RestartApplication
  24. /// </summary>
  25. [Route("/System/Restart", "POST")]
  26. [ServiceStack.ServiceHost.Api(("Restarts the application, if needed"))]
  27. public class RestartApplication
  28. {
  29. }
  30. [Route("/System/Shutdown", "POST")]
  31. [ServiceStack.ServiceHost.Api(("Shuts down the application"))]
  32. public class ShutdownApplication
  33. {
  34. }
  35. /// <summary>
  36. /// Class GetConfiguration
  37. /// </summary>
  38. [Route("/System/Configuration", "GET")]
  39. [ServiceStack.ServiceHost.Api(("Gets application configuration"))]
  40. public class GetConfiguration : IReturn<ServerConfiguration>
  41. {
  42. }
  43. /// <summary>
  44. /// Class UpdateConfiguration
  45. /// </summary>
  46. [Route("/System/Configuration", "POST")]
  47. [ServiceStack.ServiceHost.Api(("Updates application configuration"))]
  48. public class UpdateConfiguration : ServerConfiguration, IReturnVoid
  49. {
  50. }
  51. /// <summary>
  52. /// Class SystemInfoService
  53. /// </summary>
  54. public class SystemService : BaseRestService
  55. {
  56. /// <summary>
  57. /// The _json serializer
  58. /// </summary>
  59. private readonly IJsonSerializer _jsonSerializer;
  60. /// <summary>
  61. /// The _app host
  62. /// </summary>
  63. private readonly IServerApplicationHost _appHost;
  64. /// <summary>
  65. /// The _configuration manager
  66. /// </summary>
  67. private readonly IServerConfigurationManager _configurationManager;
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="SystemService" /> class.
  70. /// </summary>
  71. /// <param name="jsonSerializer">The json serializer.</param>
  72. /// <param name="appHost">The app host.</param>
  73. /// <param name="configurationManager">The configuration manager.</param>
  74. /// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
  75. public SystemService(IJsonSerializer jsonSerializer, IServerApplicationHost appHost, IServerConfigurationManager configurationManager)
  76. : base()
  77. {
  78. if (jsonSerializer == null)
  79. {
  80. throw new ArgumentNullException("jsonSerializer");
  81. }
  82. if (appHost == null)
  83. {
  84. throw new ArgumentNullException("appHost");
  85. }
  86. _appHost = appHost;
  87. _configurationManager = configurationManager;
  88. _jsonSerializer = jsonSerializer;
  89. }
  90. /// <summary>
  91. /// Gets the specified request.
  92. /// </summary>
  93. /// <param name="request">The request.</param>
  94. /// <returns>System.Object.</returns>
  95. public object Get(GetSystemInfo request)
  96. {
  97. var result = _appHost.GetSystemInfo();
  98. return ToOptimizedResult(result);
  99. }
  100. /// <summary>
  101. /// Gets the specified request.
  102. /// </summary>
  103. /// <param name="request">The request.</param>
  104. /// <returns>System.Object.</returns>
  105. public object Get(GetConfiguration request)
  106. {
  107. var dateModified = File.GetLastWriteTimeUtc(_configurationManager.ApplicationPaths.SystemConfigurationFilePath);
  108. var cacheKey = (_configurationManager.ApplicationPaths.SystemConfigurationFilePath + dateModified.Ticks).GetMD5();
  109. return ToOptimizedResultUsingCache(cacheKey, dateModified, null, () => _configurationManager.Configuration);
  110. }
  111. /// <summary>
  112. /// Posts the specified request.
  113. /// </summary>
  114. /// <param name="request">The request.</param>
  115. public void Post(RestartApplication request)
  116. {
  117. Task.Run(async () =>
  118. {
  119. await Task.Delay(100);
  120. _appHost.PerformPendingRestart();
  121. });
  122. }
  123. /// <summary>
  124. /// Posts the specified request.
  125. /// </summary>
  126. /// <param name="request">The request.</param>
  127. public void Post(ShutdownApplication request)
  128. {
  129. Task.Run(async () =>
  130. {
  131. await Task.Delay(100);
  132. _appHost.Shutdown();
  133. });
  134. }
  135. /// <summary>
  136. /// Posts the specified configuraiton.
  137. /// </summary>
  138. /// <param name="request">The request.</param>
  139. public void Post(UpdateConfiguration request)
  140. {
  141. // Silly, but we need to serialize and deserialize or the XmlSerializer will write the xml with an element name of UpdateConfiguration
  142. var json = _jsonSerializer.SerializeToString(request);
  143. var config = _jsonSerializer.DeserializeFromString<ServerConfiguration>(json);
  144. _configurationManager.ReplaceConfiguration(config);
  145. }
  146. }
  147. }