SystemService.cs 5.4 KB

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