BaseKernel.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Localization;
  4. using MediaBrowser.Common.Mef;
  5. using MediaBrowser.Common.Net;
  6. using MediaBrowser.Common.Plugins;
  7. using MediaBrowser.Common.ScheduledTasks;
  8. using MediaBrowser.Common.Serialization;
  9. using MediaBrowser.Model.Configuration;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.System;
  12. using NLog;
  13. using NLog.Config;
  14. using NLog.Targets;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.ComponentModel.Composition;
  18. using System.ComponentModel.Composition.Hosting;
  19. using System.Deployment.Application;
  20. using System.Diagnostics;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Reflection;
  24. using System.Threading;
  25. using System.Threading.Tasks;
  26. namespace MediaBrowser.Common.Kernel
  27. {
  28. /// <summary>
  29. /// Represents a shared base kernel for both the Ui and server apps
  30. /// </summary>
  31. /// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
  32. /// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam>
  33. public abstract class BaseKernel<TConfigurationType, TApplicationPathsType> : IDisposable, IKernel
  34. where TConfigurationType : BaseApplicationConfiguration, new()
  35. where TApplicationPathsType : BaseApplicationPaths, new()
  36. {
  37. /// <summary>
  38. /// Occurs when [has pending restart changed].
  39. /// </summary>
  40. public event EventHandler HasPendingRestartChanged;
  41. /// <summary>
  42. /// Notifiies the containing application that a restart has been requested
  43. /// </summary>
  44. public event EventHandler ApplicationRestartRequested;
  45. #region ConfigurationUpdated Event
  46. /// <summary>
  47. /// Occurs when [configuration updated].
  48. /// </summary>
  49. public event EventHandler<EventArgs> ConfigurationUpdated;
  50. /// <summary>
  51. /// Called when [configuration updated].
  52. /// </summary>
  53. internal void OnConfigurationUpdated()
  54. {
  55. EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty);
  56. // Notify connected clients
  57. TcpManager.SendWebSocketMessage("ConfigurationUpdated", Configuration);
  58. }
  59. #endregion
  60. #region LoggerLoaded Event
  61. /// <summary>
  62. /// Fires whenever the logger is loaded
  63. /// </summary>
  64. public event EventHandler LoggerLoaded;
  65. /// <summary>
  66. /// Called when [logger loaded].
  67. /// </summary>
  68. private void OnLoggerLoaded()
  69. {
  70. EventHelper.QueueEventIfNotNull(LoggerLoaded, this, EventArgs.Empty);
  71. }
  72. #endregion
  73. #region ReloadBeginning Event
  74. /// <summary>
  75. /// Fires whenever the kernel begins reloading
  76. /// </summary>
  77. public event EventHandler<EventArgs> ReloadBeginning;
  78. /// <summary>
  79. /// Called when [reload beginning].
  80. /// </summary>
  81. private void OnReloadBeginning()
  82. {
  83. EventHelper.QueueEventIfNotNull(ReloadBeginning, this, EventArgs.Empty);
  84. }
  85. #endregion
  86. #region ReloadCompleted Event
  87. /// <summary>
  88. /// Fires whenever the kernel completes reloading
  89. /// </summary>
  90. public event EventHandler<EventArgs> ReloadCompleted;
  91. /// <summary>
  92. /// Called when [reload completed].
  93. /// </summary>
  94. private void OnReloadCompleted()
  95. {
  96. EventHelper.QueueEventIfNotNull(ReloadCompleted, this, EventArgs.Empty);
  97. }
  98. #endregion
  99. #region ApplicationUpdated Event
  100. /// <summary>
  101. /// Occurs when [application updated].
  102. /// </summary>
  103. public event EventHandler<GenericEventArgs<Version>> ApplicationUpdated;
  104. /// <summary>
  105. /// Called when [application updated].
  106. /// </summary>
  107. /// <param name="newVersion">The new version.</param>
  108. public void OnApplicationUpdated(Version newVersion)
  109. {
  110. EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs<Version> {Argument = newVersion});
  111. NotifyPendingRestart();
  112. }
  113. #endregion
  114. /// <summary>
  115. /// The _configuration loaded
  116. /// </summary>
  117. private bool _configurationLoaded;
  118. /// <summary>
  119. /// The _configuration sync lock
  120. /// </summary>
  121. private object _configurationSyncLock = new object();
  122. /// <summary>
  123. /// The _configuration
  124. /// </summary>
  125. private TConfigurationType _configuration;
  126. /// <summary>
  127. /// Gets the system configuration
  128. /// </summary>
  129. /// <value>The configuration.</value>
  130. public TConfigurationType Configuration
  131. {
  132. get
  133. {
  134. // Lazy load
  135. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => XmlSerializer.GetXmlConfiguration<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath));
  136. return _configuration;
  137. }
  138. protected set
  139. {
  140. _configuration = value;
  141. if (value == null)
  142. {
  143. _configurationLoaded = false;
  144. }
  145. }
  146. }
  147. /// <summary>
  148. /// Gets a value indicating whether this instance is first run.
  149. /// </summary>
  150. /// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
  151. public bool IsFirstRun { get; private set; }
  152. /// <summary>
  153. /// The version of the application to display
  154. /// </summary>
  155. /// <value>The display version.</value>
  156. public string DisplayVersion { get { return ApplicationVersion.ToString(); } }
  157. /// <summary>
  158. /// Gets or sets a value indicating whether this instance has changes that require the entire application to restart.
  159. /// </summary>
  160. /// <value><c>true</c> if this instance has pending application restart; otherwise, <c>false</c>.</value>
  161. public bool HasPendingRestart { get; private set; }
  162. /// <summary>
  163. /// Gets the application paths.
  164. /// </summary>
  165. /// <value>The application paths.</value>
  166. public TApplicationPathsType ApplicationPaths { get; private set; }
  167. /// <summary>
  168. /// The _failed assembly loads
  169. /// </summary>
  170. private readonly List<string> _failedPluginAssemblies = new List<string>();
  171. /// <summary>
  172. /// Gets the plugin assemblies that failed to load.
  173. /// </summary>
  174. /// <value>The failed assembly loads.</value>
  175. public IEnumerable<string> FailedPluginAssemblies
  176. {
  177. get { return _failedPluginAssemblies; }
  178. }
  179. /// <summary>
  180. /// Gets the list of currently loaded plugins
  181. /// </summary>
  182. /// <value>The plugins.</value>
  183. [ImportMany(typeof(IPlugin))]
  184. public IEnumerable<IPlugin> Plugins { get; protected set; }
  185. /// <summary>
  186. /// Gets the list of Scheduled Tasks
  187. /// </summary>
  188. /// <value>The scheduled tasks.</value>
  189. [ImportMany(typeof(IScheduledTask))]
  190. public IEnumerable<IScheduledTask> ScheduledTasks { get; private set; }
  191. /// <summary>
  192. /// Gets the web socket listeners.
  193. /// </summary>
  194. /// <value>The web socket listeners.</value>
  195. [ImportMany(typeof(IWebSocketListener))]
  196. public IEnumerable<IWebSocketListener> WebSocketListeners { get; private set; }
  197. /// <summary>
  198. /// Gets the list of Localized string files
  199. /// </summary>
  200. /// <value>The string files.</value>
  201. [ImportMany(typeof(LocalizedStringData))]
  202. public IEnumerable<LocalizedStringData> StringFiles { get; private set; }
  203. /// <summary>
  204. /// Gets the MEF CompositionContainer
  205. /// </summary>
  206. /// <value>The composition container.</value>
  207. private CompositionContainer CompositionContainer { get; set; }
  208. /// <summary>
  209. /// The _HTTP manager
  210. /// </summary>
  211. /// <value>The HTTP manager.</value>
  212. public HttpManager HttpManager { get; private set; }
  213. /// <summary>
  214. /// Gets or sets the TCP manager.
  215. /// </summary>
  216. /// <value>The TCP manager.</value>
  217. public TcpManager TcpManager { get; private set; }
  218. /// <summary>
  219. /// Gets the task manager.
  220. /// </summary>
  221. /// <value>The task manager.</value>
  222. public TaskManager TaskManager { get; private set; }
  223. /// <summary>
  224. /// Gets the iso manager.
  225. /// </summary>
  226. /// <value>The iso manager.</value>
  227. public IIsoManager IsoManager { get; private set; }
  228. /// <summary>
  229. /// Gets the rest services.
  230. /// </summary>
  231. /// <value>The rest services.</value>
  232. [ImportMany(typeof(IRestfulService))]
  233. public IEnumerable<IRestfulService> RestServices { get; private set; }
  234. /// <summary>
  235. /// The _protobuf serializer initialized
  236. /// </summary>
  237. private bool _protobufSerializerInitialized;
  238. /// <summary>
  239. /// The _protobuf serializer sync lock
  240. /// </summary>
  241. private object _protobufSerializerSyncLock = new object();
  242. /// <summary>
  243. /// Gets a dynamically compiled generated serializer that can serialize protocontracts without reflection
  244. /// </summary>
  245. private DynamicProtobufSerializer _protobufSerializer;
  246. /// <summary>
  247. /// Gets the protobuf serializer.
  248. /// </summary>
  249. /// <value>The protobuf serializer.</value>
  250. public DynamicProtobufSerializer ProtobufSerializer
  251. {
  252. get
  253. {
  254. // Lazy load
  255. LazyInitializer.EnsureInitialized(ref _protobufSerializer, ref _protobufSerializerInitialized, ref _protobufSerializerSyncLock, () => DynamicProtobufSerializer.Create(Assemblies));
  256. return _protobufSerializer;
  257. }
  258. private set
  259. {
  260. _protobufSerializer = value;
  261. if (value == null)
  262. {
  263. _protobufSerializerInitialized = false;
  264. }
  265. }
  266. }
  267. /// <summary>
  268. /// Gets the UDP server port number.
  269. /// This can't be configurable because then the user would have to configure their client to discover the server.
  270. /// </summary>
  271. /// <value>The UDP server port number.</value>
  272. public abstract int UdpServerPortNumber { get; }
  273. /// <summary>
  274. /// Gets the name of the web application that can be used for url building.
  275. /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
  276. /// </summary>
  277. /// <value>The name of the web application.</value>
  278. public string WebApplicationName
  279. {
  280. get { return "mediabrowser"; }
  281. }
  282. /// <summary>
  283. /// Gets the HTTP server URL prefix.
  284. /// </summary>
  285. /// <value>The HTTP server URL prefix.</value>
  286. public virtual string HttpServerUrlPrefix
  287. {
  288. get
  289. {
  290. return "http://+:" + Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/";
  291. }
  292. }
  293. /// <summary>
  294. /// Gets the kernel context. Subclasses will have to override.
  295. /// </summary>
  296. /// <value>The kernel context.</value>
  297. public abstract KernelContext KernelContext { get; }
  298. /// <summary>
  299. /// Gets the log file path.
  300. /// </summary>
  301. /// <value>The log file path.</value>
  302. public string LogFilePath { get; private set; }
  303. /// <summary>
  304. /// Gets the logger.
  305. /// </summary>
  306. /// <value>The logger.</value>
  307. protected ILogger Logger { get; private set; }
  308. /// <summary>
  309. /// Gets the assemblies.
  310. /// </summary>
  311. /// <value>The assemblies.</value>
  312. public Assembly[] Assemblies { get; private set; }
  313. /// <summary>
  314. /// Initializes the Kernel
  315. /// </summary>
  316. /// <param name="isoManager">The iso manager.</param>
  317. /// <returns>Task.</returns>
  318. public async Task Init(IIsoManager isoManager)
  319. {
  320. IsoManager = isoManager;
  321. Logger = Logging.LogManager.GetLogger(GetType().Name);
  322. ApplicationPaths = new TApplicationPathsType();
  323. IsFirstRun = !File.Exists(ApplicationPaths.SystemConfigurationFilePath);
  324. // Performs initializations that can be reloaded at anytime
  325. await Reload().ConfigureAwait(false);
  326. }
  327. /// <summary>
  328. /// Performs initializations that can be reloaded at anytime
  329. /// </summary>
  330. /// <returns>Task.</returns>
  331. public async Task Reload()
  332. {
  333. OnReloadBeginning();
  334. await ReloadInternal().ConfigureAwait(false);
  335. OnReloadCompleted();
  336. Logger.Info("Kernel.Reload Complete");
  337. }
  338. /// <summary>
  339. /// Performs initializations that can be reloaded at anytime
  340. /// </summary>
  341. /// <returns>Task.</returns>
  342. protected virtual async Task ReloadInternal()
  343. {
  344. // Set these to null so that they can be lazy loaded again
  345. Configuration = null;
  346. ProtobufSerializer = null;
  347. ReloadLogger();
  348. Logger.Info("Version {0} initializing", ApplicationVersion);
  349. DisposeHttpManager();
  350. HttpManager = new HttpManager(this);
  351. await OnConfigurationLoaded().ConfigureAwait(false);
  352. DisposeTaskManager();
  353. TaskManager = new TaskManager(this);
  354. Logger.Info("Loading Plugins");
  355. await ReloadComposableParts().ConfigureAwait(false);
  356. DisposeTcpManager();
  357. TcpManager = new TcpManager(this);
  358. }
  359. /// <summary>
  360. /// Called when [configuration loaded].
  361. /// </summary>
  362. /// <returns>Task.</returns>
  363. protected virtual Task OnConfigurationLoaded()
  364. {
  365. return Task.FromResult<object>(null);
  366. }
  367. /// <summary>
  368. /// Disposes and reloads all loggers
  369. /// </summary>
  370. public void ReloadLogger()
  371. {
  372. DisposeLogger();
  373. LogFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, KernelContext + "-" + DateTime.Now.Ticks + ".log");
  374. var logFile = new FileTarget();
  375. logFile.FileName = LogFilePath;
  376. logFile.Layout = "${longdate}, ${level}, ${logger}, ${message}";
  377. AddLogTarget(logFile, "ApplicationLogFile");
  378. Logging.Logger.LoggerInstance = Logging.LogManager.GetLogger("Global");
  379. OnLoggerLoaded();
  380. }
  381. /// <summary>
  382. /// Adds the log target.
  383. /// </summary>
  384. /// <param name="target">The target.</param>
  385. /// <param name="name">The name.</param>
  386. private void AddLogTarget(Target target, string name)
  387. {
  388. var config = LogManager.Configuration;
  389. config.RemoveTarget(name);
  390. target.Name = name;
  391. config.AddTarget(name, target);
  392. var level = Configuration.EnableDebugLevelLogging ? LogLevel.Debug : LogLevel.Info;
  393. var rule = new LoggingRule("*", level, target);
  394. config.LoggingRules.Add(rule);
  395. LogManager.Configuration = config;
  396. }
  397. /// <summary>
  398. /// Uses MEF to locate plugins
  399. /// Subclasses can use this to locate types within plugins
  400. /// </summary>
  401. /// <returns>Task.</returns>
  402. private async Task ReloadComposableParts()
  403. {
  404. _failedPluginAssemblies.Clear();
  405. DisposeComposableParts();
  406. Assemblies = GetComposablePartAssemblies().ToArray();
  407. CompositionContainer = MefUtils.GetSafeCompositionContainer(Assemblies.Select(i => new AssemblyCatalog(i)));
  408. CompositionContainer.ComposeExportedValue("kernel", this);
  409. CompositionContainer.ComposeParts(this);
  410. await OnComposablePartsLoaded().ConfigureAwait(false);
  411. CompositionContainer.Catalog.Dispose();
  412. }
  413. /// <summary>
  414. /// Gets the composable part assemblies.
  415. /// </summary>
  416. /// <returns>IEnumerable{Assembly}.</returns>
  417. protected virtual IEnumerable<Assembly> GetComposablePartAssemblies()
  418. {
  419. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  420. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  421. var pluginAssemblies = Directory.EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  422. .Select(file =>
  423. {
  424. try
  425. {
  426. return Assembly.Load(File.ReadAllBytes((file)));
  427. }
  428. catch (Exception ex)
  429. {
  430. _failedPluginAssemblies.Add(file);
  431. Logger.ErrorException("Error loading {0}", ex, file);
  432. return null;
  433. }
  434. }).Where(a => a != null);
  435. foreach (var pluginAssembly in pluginAssemblies)
  436. {
  437. yield return pluginAssembly;
  438. }
  439. var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
  440. var corePluginDirectory = Path.Combine(runningDirectory, "CorePlugins");
  441. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  442. pluginAssemblies = Directory.EnumerateFiles(corePluginDirectory, "*.dll", SearchOption.TopDirectoryOnly)
  443. .Select(file =>
  444. {
  445. try
  446. {
  447. return Assembly.Load(File.ReadAllBytes((file)));
  448. }
  449. catch (Exception ex)
  450. {
  451. _failedPluginAssemblies.Add(file);
  452. Logger.ErrorException("Error loading {0}", ex, file);
  453. return null;
  454. }
  455. }).Where(a => a != null);
  456. foreach (var pluginAssembly in pluginAssemblies)
  457. {
  458. yield return pluginAssembly;
  459. }
  460. // Include composable parts in the Model assembly
  461. yield return typeof (SystemInfo).Assembly;
  462. // Include composable parts in the Common assembly
  463. yield return Assembly.GetExecutingAssembly();
  464. // Include composable parts in the subclass assembly
  465. yield return GetType().Assembly;
  466. }
  467. /// <summary>
  468. /// Fires after MEF finishes finding composable parts within plugin assemblies
  469. /// </summary>
  470. /// <returns>Task.</returns>
  471. protected virtual Task OnComposablePartsLoaded()
  472. {
  473. return Task.Run(() =>
  474. {
  475. foreach (var listener in WebSocketListeners)
  476. {
  477. listener.Initialize(this);
  478. }
  479. foreach (var task in ScheduledTasks)
  480. {
  481. task.Initialize(this);
  482. }
  483. // Start-up each plugin
  484. Parallel.ForEach(Plugins, plugin =>
  485. {
  486. Logger.Info("Initializing {0} {1}", plugin.Name, plugin.Version);
  487. try
  488. {
  489. plugin.Initialize(this);
  490. Logger.Info("{0} {1} initialized.", plugin.Name, plugin.Version);
  491. }
  492. catch (Exception ex)
  493. {
  494. Logger.ErrorException("Error initializing {0}", ex, plugin.Name);
  495. }
  496. });
  497. });
  498. }
  499. /// <summary>
  500. /// Notifies that the kernel that a change has been made that requires a restart
  501. /// </summary>
  502. public void NotifyPendingRestart()
  503. {
  504. HasPendingRestart = true;
  505. TcpManager.SendWebSocketMessage("HasPendingRestartChanged", GetSystemInfo());
  506. EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty);
  507. }
  508. /// <summary>
  509. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  510. /// </summary>
  511. public void Dispose()
  512. {
  513. Dispose(true);
  514. GC.SuppressFinalize(this);
  515. }
  516. /// <summary>
  517. /// Releases unmanaged and - optionally - managed resources.
  518. /// </summary>
  519. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  520. protected virtual void Dispose(bool dispose)
  521. {
  522. if (dispose)
  523. {
  524. DisposeTcpManager();
  525. DisposeTaskManager();
  526. DisposeIsoManager();
  527. DisposeHttpManager();
  528. DisposeComposableParts();
  529. }
  530. }
  531. /// <summary>
  532. /// Disposes the iso manager.
  533. /// </summary>
  534. private void DisposeIsoManager()
  535. {
  536. if (IsoManager != null)
  537. {
  538. IsoManager.Dispose();
  539. IsoManager = null;
  540. }
  541. }
  542. /// <summary>
  543. /// Disposes the TCP manager.
  544. /// </summary>
  545. private void DisposeTcpManager()
  546. {
  547. if (TcpManager != null)
  548. {
  549. TcpManager.Dispose();
  550. TcpManager = null;
  551. }
  552. }
  553. /// <summary>
  554. /// Disposes the task manager.
  555. /// </summary>
  556. private void DisposeTaskManager()
  557. {
  558. if (TaskManager != null)
  559. {
  560. TaskManager.Dispose();
  561. TaskManager = null;
  562. }
  563. }
  564. /// <summary>
  565. /// Disposes the HTTP manager.
  566. /// </summary>
  567. private void DisposeHttpManager()
  568. {
  569. if (HttpManager != null)
  570. {
  571. HttpManager.Dispose();
  572. HttpManager = null;
  573. }
  574. }
  575. /// <summary>
  576. /// Disposes all objects gathered through MEF composable parts
  577. /// </summary>
  578. protected virtual void DisposeComposableParts()
  579. {
  580. if (CompositionContainer != null)
  581. {
  582. CompositionContainer.Dispose();
  583. }
  584. }
  585. /// <summary>
  586. /// Disposes all logger resources
  587. /// </summary>
  588. private void DisposeLogger()
  589. {
  590. // Dispose all current loggers
  591. var listeners = Trace.Listeners.OfType<TraceListener>().ToList();
  592. Trace.Listeners.Clear();
  593. foreach (var listener in listeners)
  594. {
  595. listener.Dispose();
  596. }
  597. }
  598. /// <summary>
  599. /// Gets the current application version
  600. /// </summary>
  601. /// <value>The application version.</value>
  602. public Version ApplicationVersion
  603. {
  604. get
  605. {
  606. return GetType().Assembly.GetName().Version;
  607. }
  608. }
  609. /// <summary>
  610. /// Performs the pending restart.
  611. /// </summary>
  612. /// <returns>Task.</returns>
  613. public void PerformPendingRestart()
  614. {
  615. if (HasPendingRestart)
  616. {
  617. RestartApplication();
  618. }
  619. else
  620. {
  621. Logger.Info("PerformPendingRestart - not needed");
  622. }
  623. }
  624. /// <summary>
  625. /// Restarts the application.
  626. /// </summary>
  627. protected void RestartApplication()
  628. {
  629. Logger.Info("Restarting the application");
  630. EventHelper.QueueEventIfNotNull(ApplicationRestartRequested, this, EventArgs.Empty);
  631. }
  632. /// <summary>
  633. /// Gets the system status.
  634. /// </summary>
  635. /// <returns>SystemInfo.</returns>
  636. public virtual SystemInfo GetSystemInfo()
  637. {
  638. return new SystemInfo
  639. {
  640. HasPendingRestart = HasPendingRestart,
  641. Version = DisplayVersion,
  642. IsNetworkDeployed = ApplicationDeployment.IsNetworkDeployed,
  643. WebSocketPortNumber = TcpManager.WebSocketPortNumber,
  644. SupportsNativeWebSocket = TcpManager.SupportsNativeWebSocket,
  645. FailedPluginAssemblies = FailedPluginAssemblies.ToArray()
  646. };
  647. }
  648. /// <summary>
  649. /// The _save lock
  650. /// </summary>
  651. private readonly object _configurationSaveLock = new object();
  652. /// <summary>
  653. /// Saves the current configuration
  654. /// </summary>
  655. public void SaveConfiguration()
  656. {
  657. lock (_configurationSaveLock)
  658. {
  659. XmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
  660. }
  661. OnConfigurationUpdated();
  662. }
  663. /// <summary>
  664. /// Gets the application paths.
  665. /// </summary>
  666. /// <value>The application paths.</value>
  667. BaseApplicationPaths IKernel.ApplicationPaths
  668. {
  669. get { return ApplicationPaths; }
  670. }
  671. /// <summary>
  672. /// Gets the configuration.
  673. /// </summary>
  674. /// <value>The configuration.</value>
  675. BaseApplicationConfiguration IKernel.Configuration
  676. {
  677. get { return Configuration; }
  678. }
  679. }
  680. }