BaseApplicationHost.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. using System.Text;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Events;
  4. using MediaBrowser.Common.Implementations.Archiving;
  5. using MediaBrowser.Common.Implementations.IO;
  6. using MediaBrowser.Common.Implementations.ScheduledTasks;
  7. using MediaBrowser.Common.Implementations.Security;
  8. using MediaBrowser.Common.Implementations.Serialization;
  9. using MediaBrowser.Common.Implementations.Updates;
  10. using MediaBrowser.Common.IO;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Common.Plugins;
  13. using MediaBrowser.Common.Progress;
  14. using MediaBrowser.Common.ScheduledTasks;
  15. using MediaBrowser.Common.Security;
  16. using MediaBrowser.Common.Updates;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Logging;
  19. using MediaBrowser.Model.Serialization;
  20. using MediaBrowser.Model.Updates;
  21. using ServiceStack;
  22. using SimpleInjector;
  23. using System;
  24. using System.Collections.Generic;
  25. using System.IO;
  26. using System.Linq;
  27. using System.Net;
  28. using System.Reflection;
  29. using System.Threading;
  30. using System.Threading.Tasks;
  31. namespace MediaBrowser.Common.Implementations
  32. {
  33. /// <summary>
  34. /// Class BaseApplicationHost
  35. /// </summary>
  36. /// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam>
  37. public abstract class BaseApplicationHost<TApplicationPathsType> : IApplicationHost
  38. where TApplicationPathsType : class, IApplicationPaths
  39. {
  40. /// <summary>
  41. /// Occurs when [has pending restart changed].
  42. /// </summary>
  43. public event EventHandler HasPendingRestartChanged;
  44. /// <summary>
  45. /// Occurs when [application updated].
  46. /// </summary>
  47. public event EventHandler<GenericEventArgs<Version>> ApplicationUpdated;
  48. /// <summary>
  49. /// Gets or sets a value indicating whether this instance has changes that require the entire application to restart.
  50. /// </summary>
  51. /// <value><c>true</c> if this instance has pending application restart; otherwise, <c>false</c>.</value>
  52. public bool HasPendingRestart { get; private set; }
  53. /// <summary>
  54. /// Gets or sets the logger.
  55. /// </summary>
  56. /// <value>The logger.</value>
  57. protected ILogger Logger { get; private set; }
  58. /// <summary>
  59. /// Gets or sets the plugins.
  60. /// </summary>
  61. /// <value>The plugins.</value>
  62. public IEnumerable<IPlugin> Plugins { get; protected set; }
  63. /// <summary>
  64. /// Gets or sets the log manager.
  65. /// </summary>
  66. /// <value>The log manager.</value>
  67. public ILogManager LogManager { get; protected set; }
  68. /// <summary>
  69. /// Gets the application paths.
  70. /// </summary>
  71. /// <value>The application paths.</value>
  72. protected TApplicationPathsType ApplicationPaths { get; private set; }
  73. /// <summary>
  74. /// The container
  75. /// </summary>
  76. protected readonly Container Container = new Container();
  77. /// <summary>
  78. /// The json serializer
  79. /// </summary>
  80. public IJsonSerializer JsonSerializer { get; private set; }
  81. /// <summary>
  82. /// The _XML serializer
  83. /// </summary>
  84. protected readonly IXmlSerializer XmlSerializer = new XmlSerializer();
  85. /// <summary>
  86. /// Gets assemblies that failed to load
  87. /// </summary>
  88. /// <value>The failed assemblies.</value>
  89. public List<string> FailedAssemblies { get; protected set; }
  90. /// <summary>
  91. /// Gets all types within all running assemblies
  92. /// </summary>
  93. /// <value>All types.</value>
  94. public Type[] AllTypes { get; protected set; }
  95. /// <summary>
  96. /// Gets all concrete types.
  97. /// </summary>
  98. /// <value>All concrete types.</value>
  99. public Type[] AllConcreteTypes { get; protected set; }
  100. /// <summary>
  101. /// The disposable parts
  102. /// </summary>
  103. protected readonly List<IDisposable> DisposableParts = new List<IDisposable>();
  104. /// <summary>
  105. /// Gets a value indicating whether this instance is first run.
  106. /// </summary>
  107. /// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
  108. public bool IsFirstRun { get; private set; }
  109. /// <summary>
  110. /// Gets the kernel.
  111. /// </summary>
  112. /// <value>The kernel.</value>
  113. protected ITaskManager TaskManager { get; private set; }
  114. /// <summary>
  115. /// Gets the security manager.
  116. /// </summary>
  117. /// <value>The security manager.</value>
  118. protected ISecurityManager SecurityManager { get; private set; }
  119. /// <summary>
  120. /// Gets the HTTP client.
  121. /// </summary>
  122. /// <value>The HTTP client.</value>
  123. protected IHttpClient HttpClient { get; private set; }
  124. /// <summary>
  125. /// Gets the network manager.
  126. /// </summary>
  127. /// <value>The network manager.</value>
  128. protected INetworkManager NetworkManager { get; private set; }
  129. /// <summary>
  130. /// Gets the configuration manager.
  131. /// </summary>
  132. /// <value>The configuration manager.</value>
  133. protected IConfigurationManager ConfigurationManager { get; private set; }
  134. /// <summary>
  135. /// Gets or sets the installation manager.
  136. /// </summary>
  137. /// <value>The installation manager.</value>
  138. protected IInstallationManager InstallationManager { get; private set; }
  139. protected IFileSystem FileSystemManager { get; private set; }
  140. /// <summary>
  141. /// Gets or sets the zip client.
  142. /// </summary>
  143. /// <value>The zip client.</value>
  144. protected IZipClient ZipClient { get; private set; }
  145. protected IIsoManager IsoManager { get; private set; }
  146. /// <summary>
  147. /// Gets the name.
  148. /// </summary>
  149. /// <value>The name.</value>
  150. public abstract string Name { get; }
  151. /// <summary>
  152. /// Initializes a new instance of the <see cref="BaseApplicationHost{TApplicationPathsType}"/> class.
  153. /// </summary>
  154. protected BaseApplicationHost(TApplicationPathsType applicationPaths, ILogManager logManager)
  155. {
  156. FailedAssemblies = new List<string>();
  157. ApplicationPaths = applicationPaths;
  158. LogManager = logManager;
  159. ConfigurationManager = GetConfigurationManager();
  160. }
  161. /// <summary>
  162. /// Inits this instance.
  163. /// </summary>
  164. /// <returns>Task.</returns>
  165. public virtual async Task Init(IProgress<double> progress)
  166. {
  167. try
  168. {
  169. // https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.IntegrationTests/Web.config#L4
  170. Licensing.RegisterLicense("1001-e1JlZjoxMDAxLE5hbWU6VGVzdCBCdXNpbmVzcyxUeXBlOkJ1c2luZXNzLEhhc2g6UHVNTVRPclhvT2ZIbjQ5MG5LZE1mUTd5RUMzQnBucTFEbTE3TDczVEF4QUNMT1FhNXJMOWkzVjFGL2ZkVTE3Q2pDNENqTkQyUktRWmhvUVBhYTBiekJGUUZ3ZE5aZHFDYm9hL3lydGlwUHI5K1JsaTBYbzNsUC85cjVJNHE5QVhldDN6QkE4aTlvdldrdTgyTk1relY2eis2dFFqTThYN2lmc0JveHgycFdjPSxFeHBpcnk6MjAxMy0wMS0wMX0=");
  171. }
  172. catch
  173. {
  174. // Failing under mono
  175. }
  176. progress.Report(1);
  177. JsonSerializer = CreateJsonSerializer();
  178. Logger = LogManager.GetLogger("App");
  179. IsFirstRun = !ConfigurationManager.CommonConfiguration.IsStartupWizardCompleted;
  180. progress.Report(2);
  181. LogManager.LogSeverity = ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging
  182. ? LogSeverity.Debug
  183. : LogSeverity.Info;
  184. // Put the app config in the log for troubleshooting purposes
  185. Logger.LogMultiline("Application Configuration:", LogSeverity.Info, new StringBuilder(JsonSerializer.SerializeToString(ConfigurationManager.CommonConfiguration)));
  186. progress.Report(3);
  187. DiscoverTypes();
  188. progress.Report(14);
  189. Logger.Info("Version {0} initializing", ApplicationVersion);
  190. SetHttpLimit();
  191. progress.Report(15);
  192. var innerProgress = new ActionableProgress<double>();
  193. innerProgress.RegisterAction(p => progress.Report((.8 * p) + 15));
  194. await RegisterResources(innerProgress).ConfigureAwait(false);
  195. FindParts();
  196. progress.Report(95);
  197. await InstallIsoMounters(CancellationToken.None).ConfigureAwait(false);
  198. progress.Report(100);
  199. }
  200. protected virtual IJsonSerializer CreateJsonSerializer()
  201. {
  202. return new JsonSerializer();
  203. }
  204. private void SetHttpLimit()
  205. {
  206. try
  207. {
  208. // Increase the max http request limit
  209. ServicePointManager.DefaultConnectionLimit = Math.Max(96, ServicePointManager.DefaultConnectionLimit);
  210. }
  211. catch (Exception ex)
  212. {
  213. Logger.ErrorException("Error setting http limit", ex);
  214. }
  215. }
  216. /// <summary>
  217. /// Installs the iso mounters.
  218. /// </summary>
  219. /// <param name="cancellationToken">The cancellation token.</param>
  220. /// <returns>Task.</returns>
  221. private async Task InstallIsoMounters(CancellationToken cancellationToken)
  222. {
  223. var list = new List<IIsoMounter>();
  224. foreach (var isoMounter in GetExports<IIsoMounter>())
  225. {
  226. try
  227. {
  228. if (isoMounter.RequiresInstallation && !isoMounter.IsInstalled)
  229. {
  230. Logger.Info("Installing {0}", isoMounter.Name);
  231. await isoMounter.Install(cancellationToken).ConfigureAwait(false);
  232. }
  233. list.Add(isoMounter);
  234. }
  235. catch (Exception ex)
  236. {
  237. Logger.ErrorException("{0} failed to load.", ex, isoMounter.Name);
  238. }
  239. }
  240. IsoManager.AddParts(list);
  241. }
  242. /// <summary>
  243. /// Runs the startup tasks.
  244. /// </summary>
  245. /// <returns>Task.</returns>
  246. public virtual Task RunStartupTasks()
  247. {
  248. return Task.Run(() =>
  249. {
  250. Resolve<ITaskManager>().AddTasks(GetExports<IScheduledTask>(false));
  251. Task.Run(() => ConfigureAutorun());
  252. ConfigurationManager.ConfigurationUpdated += OnConfigurationUpdated;
  253. });
  254. }
  255. /// <summary>
  256. /// Configures the autorun.
  257. /// </summary>
  258. private void ConfigureAutorun()
  259. {
  260. try
  261. {
  262. ConfigureAutoRunAtStartup(ConfigurationManager.CommonConfiguration.RunAtStartup);
  263. }
  264. catch (Exception ex)
  265. {
  266. Logger.ErrorException("Error configuring autorun", ex);
  267. }
  268. }
  269. /// <summary>
  270. /// Gets the composable part assemblies.
  271. /// </summary>
  272. /// <returns>IEnumerable{Assembly}.</returns>
  273. protected abstract IEnumerable<Assembly> GetComposablePartAssemblies();
  274. /// <summary>
  275. /// Gets the configuration manager.
  276. /// </summary>
  277. /// <returns>IConfigurationManager.</returns>
  278. protected abstract IConfigurationManager GetConfigurationManager();
  279. /// <summary>
  280. /// Finds the parts.
  281. /// </summary>
  282. protected virtual void FindParts()
  283. {
  284. Plugins = GetExports<IPlugin>();
  285. }
  286. /// <summary>
  287. /// Discovers the types.
  288. /// </summary>
  289. protected void DiscoverTypes()
  290. {
  291. FailedAssemblies.Clear();
  292. var assemblies = GetComposablePartAssemblies().ToList();
  293. foreach (var assembly in assemblies)
  294. {
  295. Logger.Info("Loading {0}", assembly.FullName);
  296. }
  297. AllTypes = assemblies.SelectMany(GetTypes).ToArray();
  298. AllConcreteTypes = AllTypes.Where(t => t.IsClass && !t.IsAbstract && !t.IsInterface && !t.IsGenericType).ToArray();
  299. }
  300. /// <summary>
  301. /// Registers resources that classes will depend on
  302. /// </summary>
  303. /// <returns>Task.</returns>
  304. protected virtual Task RegisterResources(IProgress<double> progress)
  305. {
  306. return Task.Run(() =>
  307. {
  308. RegisterSingleInstance(ConfigurationManager);
  309. RegisterSingleInstance<IApplicationHost>(this);
  310. RegisterSingleInstance<IApplicationPaths>(ApplicationPaths);
  311. TaskManager = new TaskManager(ApplicationPaths, JsonSerializer, Logger);
  312. RegisterSingleInstance(JsonSerializer);
  313. RegisterSingleInstance(XmlSerializer);
  314. RegisterSingleInstance(LogManager);
  315. RegisterSingleInstance(Logger);
  316. RegisterSingleInstance(TaskManager);
  317. FileSystemManager = CreateFileSystemManager();
  318. RegisterSingleInstance(FileSystemManager);
  319. HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, Logger, FileSystemManager);
  320. RegisterSingleInstance(HttpClient);
  321. NetworkManager = CreateNetworkManager();
  322. RegisterSingleInstance(NetworkManager);
  323. SecurityManager = new PluginSecurityManager(this, HttpClient, JsonSerializer, ApplicationPaths, NetworkManager, LogManager);
  324. RegisterSingleInstance(SecurityManager);
  325. InstallationManager = new InstallationManager(Logger, this, ApplicationPaths, HttpClient, JsonSerializer, SecurityManager, NetworkManager, ConfigurationManager);
  326. RegisterSingleInstance(InstallationManager);
  327. ZipClient = new ZipClient();
  328. RegisterSingleInstance(ZipClient);
  329. IsoManager = new IsoManager();
  330. RegisterSingleInstance(IsoManager);
  331. });
  332. }
  333. protected virtual IFileSystem CreateFileSystemManager()
  334. {
  335. return new CommonFileSystem(Logger, true);
  336. }
  337. /// <summary>
  338. /// Gets a list of types within an assembly
  339. /// This will handle situations that would normally throw an exception - such as a type within the assembly that depends on some other non-existant reference
  340. /// </summary>
  341. /// <param name="assembly">The assembly.</param>
  342. /// <returns>IEnumerable{Type}.</returns>
  343. /// <exception cref="System.ArgumentNullException">assembly</exception>
  344. protected IEnumerable<Type> GetTypes(Assembly assembly)
  345. {
  346. if (assembly == null)
  347. {
  348. throw new ArgumentNullException("assembly");
  349. }
  350. try
  351. {
  352. return assembly.GetTypes();
  353. }
  354. catch (ReflectionTypeLoadException ex)
  355. {
  356. // If it fails we can still get a list of the Types it was able to resolve
  357. return ex.Types.Where(t => t != null);
  358. }
  359. }
  360. protected abstract INetworkManager CreateNetworkManager();
  361. /// <summary>
  362. /// Creates an instance of type and resolves all constructor dependancies
  363. /// </summary>
  364. /// <param name="type">The type.</param>
  365. /// <returns>System.Object.</returns>
  366. public object CreateInstance(Type type)
  367. {
  368. try
  369. {
  370. return Container.GetInstance(type);
  371. }
  372. catch (Exception ex)
  373. {
  374. Logger.Error("Error creating {0}", ex, type.Name);
  375. throw;
  376. }
  377. }
  378. /// <summary>
  379. /// Creates the instance safe.
  380. /// </summary>
  381. /// <param name="type">The type.</param>
  382. /// <returns>System.Object.</returns>
  383. protected object CreateInstanceSafe(Type type)
  384. {
  385. try
  386. {
  387. return Container.GetInstance(type);
  388. }
  389. catch (Exception ex)
  390. {
  391. Logger.Error("Error creating {0}", ex, type.Name);
  392. // Don't blow up in release mode
  393. return null;
  394. }
  395. }
  396. /// <summary>
  397. /// Registers the specified obj.
  398. /// </summary>
  399. /// <typeparam name="T"></typeparam>
  400. /// <param name="obj">The obj.</param>
  401. /// <param name="manageLifetime">if set to <c>true</c> [manage lifetime].</param>
  402. protected void RegisterSingleInstance<T>(T obj, bool manageLifetime = true)
  403. where T : class
  404. {
  405. Container.RegisterSingle(obj);
  406. if (manageLifetime)
  407. {
  408. var disposable = obj as IDisposable;
  409. if (disposable != null)
  410. {
  411. DisposableParts.Add(disposable);
  412. }
  413. }
  414. }
  415. /// <summary>
  416. /// Registers the single instance.
  417. /// </summary>
  418. /// <typeparam name="T"></typeparam>
  419. /// <param name="func">The func.</param>
  420. protected void RegisterSingleInstance<T>(Func<T> func)
  421. where T : class
  422. {
  423. Container.RegisterSingle(func);
  424. }
  425. /// <summary>
  426. /// Resolves this instance.
  427. /// </summary>
  428. /// <typeparam name="T"></typeparam>
  429. /// <returns>``0.</returns>
  430. public T Resolve<T>()
  431. {
  432. return (T)Container.GetRegistration(typeof(T), true).GetInstance();
  433. }
  434. /// <summary>
  435. /// Resolves this instance.
  436. /// </summary>
  437. /// <typeparam name="T"></typeparam>
  438. /// <returns>``0.</returns>
  439. public T TryResolve<T>()
  440. {
  441. var result = Container.GetRegistration(typeof(T), false);
  442. if (result == null)
  443. {
  444. return default(T);
  445. }
  446. return (T)result.GetInstance();
  447. }
  448. /// <summary>
  449. /// Loads the assembly.
  450. /// </summary>
  451. /// <param name="file">The file.</param>
  452. /// <returns>Assembly.</returns>
  453. protected Assembly LoadAssembly(string file)
  454. {
  455. try
  456. {
  457. return Assembly.Load(File.ReadAllBytes((file)));
  458. }
  459. catch (Exception ex)
  460. {
  461. FailedAssemblies.Add(file);
  462. Logger.ErrorException("Error loading assembly {0}", ex, file);
  463. return null;
  464. }
  465. }
  466. /// <summary>
  467. /// Gets the export types.
  468. /// </summary>
  469. /// <typeparam name="T"></typeparam>
  470. /// <returns>IEnumerable{Type}.</returns>
  471. public IEnumerable<Type> GetExportTypes<T>()
  472. {
  473. var currentType = typeof(T);
  474. return AllConcreteTypes.AsParallel().Where(currentType.IsAssignableFrom);
  475. }
  476. /// <summary>
  477. /// Gets the exports.
  478. /// </summary>
  479. /// <typeparam name="T"></typeparam>
  480. /// <param name="manageLiftime">if set to <c>true</c> [manage liftime].</param>
  481. /// <returns>IEnumerable{``0}.</returns>
  482. public IEnumerable<T> GetExports<T>(bool manageLiftime = true)
  483. {
  484. var parts = GetExportTypes<T>()
  485. .Select(CreateInstanceSafe)
  486. .Where(i => i != null)
  487. .Cast<T>()
  488. .ToList();
  489. if (manageLiftime)
  490. {
  491. lock (DisposableParts)
  492. {
  493. DisposableParts.AddRange(parts.OfType<IDisposable>());
  494. }
  495. }
  496. return parts;
  497. }
  498. /// <summary>
  499. /// Gets the current application version
  500. /// </summary>
  501. /// <value>The application version.</value>
  502. public Version ApplicationVersion
  503. {
  504. get
  505. {
  506. return GetType().Assembly.GetName().Version;
  507. }
  508. }
  509. /// <summary>
  510. /// Handles the ConfigurationUpdated event of the ConfigurationManager control.
  511. /// </summary>
  512. /// <param name="sender">The source of the event.</param>
  513. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  514. /// <exception cref="System.NotImplementedException"></exception>
  515. protected virtual void OnConfigurationUpdated(object sender, EventArgs e)
  516. {
  517. ConfigureAutorun();
  518. }
  519. protected abstract void ConfigureAutoRunAtStartup(bool autorun);
  520. /// <summary>
  521. /// Removes the plugin.
  522. /// </summary>
  523. /// <param name="plugin">The plugin.</param>
  524. public void RemovePlugin(IPlugin plugin)
  525. {
  526. var list = Plugins.ToList();
  527. list.Remove(plugin);
  528. Plugins = list;
  529. }
  530. /// <summary>
  531. /// Gets a value indicating whether this instance can self restart.
  532. /// </summary>
  533. /// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value>
  534. public abstract bool CanSelfRestart { get; }
  535. /// <summary>
  536. /// Notifies that the kernel that a change has been made that requires a restart
  537. /// </summary>
  538. public void NotifyPendingRestart()
  539. {
  540. HasPendingRestart = true;
  541. EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger);
  542. }
  543. /// <summary>
  544. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  545. /// </summary>
  546. public void Dispose()
  547. {
  548. Dispose(true);
  549. }
  550. /// <summary>
  551. /// Releases unmanaged and - optionally - managed resources.
  552. /// </summary>
  553. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  554. protected virtual void Dispose(bool dispose)
  555. {
  556. if (dispose)
  557. {
  558. var type = GetType();
  559. Logger.Info("Disposing " + type.Name);
  560. var parts = DisposableParts.Distinct().Where(i => i.GetType() != type).ToList();
  561. DisposableParts.Clear();
  562. foreach (var part in parts)
  563. {
  564. Logger.Info("Disposing " + part.GetType().Name);
  565. part.Dispose();
  566. }
  567. }
  568. }
  569. /// <summary>
  570. /// Restarts this instance.
  571. /// </summary>
  572. public abstract Task Restart();
  573. /// <summary>
  574. /// Gets or sets a value indicating whether this instance can self update.
  575. /// </summary>
  576. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  577. public abstract bool CanSelfUpdate { get; }
  578. /// <summary>
  579. /// Checks for update.
  580. /// </summary>
  581. /// <param name="cancellationToken">The cancellation token.</param>
  582. /// <param name="progress">The progress.</param>
  583. /// <returns>Task{CheckForUpdateResult}.</returns>
  584. public abstract Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken,
  585. IProgress<double> progress);
  586. /// <summary>
  587. /// Updates the application.
  588. /// </summary>
  589. /// <param name="package">The package that contains the update</param>
  590. /// <param name="cancellationToken">The cancellation token.</param>
  591. /// <param name="progress">The progress.</param>
  592. /// <returns>Task.</returns>
  593. public abstract Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken,
  594. IProgress<double> progress);
  595. /// <summary>
  596. /// Shuts down.
  597. /// </summary>
  598. public abstract Task Shutdown();
  599. /// <summary>
  600. /// Called when [application updated].
  601. /// </summary>
  602. /// <param name="newVersion">The new version.</param>
  603. protected void OnApplicationUpdated(Version newVersion)
  604. {
  605. Logger.Info("Application has been updated to version {0}", newVersion);
  606. EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs<Version> { Argument = newVersion }, Logger);
  607. NotifyPendingRestart();
  608. }
  609. }
  610. }