SystemUpdateTask.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Model.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Server.Implementations.ScheduledTasks
  10. {
  11. /// <summary>
  12. /// Plugin Update Task
  13. /// </summary>
  14. public class SystemUpdateTask : IScheduledTask, IHasKey
  15. {
  16. /// <summary>
  17. /// The _app host
  18. /// </summary>
  19. private readonly IApplicationHost _appHost;
  20. /// <summary>
  21. /// Gets or sets the configuration manager.
  22. /// </summary>
  23. /// <value>The configuration manager.</value>
  24. private IConfigurationManager ConfigurationManager { get; set; }
  25. /// <summary>
  26. /// Gets or sets the logger.
  27. /// </summary>
  28. /// <value>The logger.</value>
  29. private ILogger Logger { get; set; }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="SystemUpdateTask" /> class.
  32. /// </summary>
  33. /// <param name="appHost">The app host.</param>
  34. /// <param name="configurationManager">The configuration manager.</param>
  35. /// <param name="logger">The logger.</param>
  36. public SystemUpdateTask(IApplicationHost appHost, IConfigurationManager configurationManager, ILogger logger)
  37. {
  38. _appHost = appHost;
  39. ConfigurationManager = configurationManager;
  40. Logger = logger;
  41. }
  42. /// <summary>
  43. /// Creates the triggers that define when the task will run
  44. /// </summary>
  45. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  46. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  47. {
  48. // Until we can vary these default triggers per server and MBT, we need something that makes sense for both
  49. return new ITaskTrigger[] {
  50. // At startup
  51. new StartupTrigger(),
  52. // Every so often
  53. new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
  54. };
  55. }
  56. /// <summary>
  57. /// Returns the task to be executed
  58. /// </summary>
  59. /// <param name="cancellationToken">The cancellation token.</param>
  60. /// <param name="progress">The progress.</param>
  61. /// <returns>Task.</returns>
  62. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  63. {
  64. EventHandler<double> innerProgressHandler = (sender, e) => progress.Report(e * .1);
  65. // Create a progress object for the update check
  66. var innerProgress = new Progress<double>();
  67. innerProgress.ProgressChanged += innerProgressHandler;
  68. var updateInfo = await _appHost.CheckForApplicationUpdate(cancellationToken, innerProgress).ConfigureAwait(false);
  69. // Release the event handler
  70. innerProgress.ProgressChanged -= innerProgressHandler;
  71. progress.Report(10);
  72. if (!updateInfo.IsUpdateAvailable)
  73. {
  74. Logger.Debug("No application update available.");
  75. progress.Report(100);
  76. return;
  77. }
  78. cancellationToken.ThrowIfCancellationRequested();
  79. if (!_appHost.CanSelfUpdate) return;
  80. if (ConfigurationManager.CommonConfiguration.EnableAutoUpdate)
  81. {
  82. Logger.Info("Update Revision {0} available. Updating...", updateInfo.AvailableVersion);
  83. innerProgressHandler = (sender, e) => progress.Report((e * .9) + .1);
  84. innerProgress = new Progress<double>();
  85. innerProgress.ProgressChanged += innerProgressHandler;
  86. await _appHost.UpdateApplication(updateInfo.Package, cancellationToken, innerProgress).ConfigureAwait(false);
  87. // Release the event handler
  88. innerProgress.ProgressChanged -= innerProgressHandler;
  89. }
  90. else
  91. {
  92. Logger.Info("A new version of " + _appHost.Name + " is available.");
  93. }
  94. progress.Report(100);
  95. }
  96. /// <summary>
  97. /// Gets the name of the task
  98. /// </summary>
  99. /// <value>The name.</value>
  100. public string Name
  101. {
  102. get { return "Check for application updates"; }
  103. }
  104. /// <summary>
  105. /// Gets the description.
  106. /// </summary>
  107. /// <value>The description.</value>
  108. public string Description
  109. {
  110. get { return "Downloads and installs application updates."; }
  111. }
  112. /// <summary>
  113. /// Gets the category.
  114. /// </summary>
  115. /// <value>The category.</value>
  116. public string Category
  117. {
  118. get { return "Application"; }
  119. }
  120. public string Key
  121. {
  122. get { return "SystemUpdateTask"; }
  123. }
  124. }
  125. }