SystemUpdateTask.cs 4.8 KB

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