SystemUpdateTask.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Model.Tasks;
  9. namespace Emby.Server.Implementations.ScheduledTasks
  10. {
  11. /// <summary>
  12. /// Plugin Update Task
  13. /// </summary>
  14. public class SystemUpdateTask : IScheduledTask
  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<TaskTriggerInfo> GetDefaultTriggers()
  47. {
  48. return new[] {
  49. // At startup
  50. new TaskTriggerInfo {Type = TaskTriggerInfo.TriggerStartup},
  51. // Every so often
  52. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  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. EventHandler<double> innerProgressHandler = (sender, e) => progress.Report(e * .1);
  64. // Create a progress object for the update check
  65. var innerProgress = new Progress<double>();
  66. innerProgress.ProgressChanged += innerProgressHandler;
  67. var updateInfo = await _appHost.CheckForApplicationUpdate(cancellationToken, innerProgress).ConfigureAwait(false);
  68. // Release the event handler
  69. innerProgress.ProgressChanged -= innerProgressHandler;
  70. progress.Report(10);
  71. if (!updateInfo.IsUpdateAvailable)
  72. {
  73. Logger.Debug("No application update available.");
  74. progress.Report(100);
  75. return;
  76. }
  77. cancellationToken.ThrowIfCancellationRequested();
  78. if (!_appHost.CanSelfUpdate) return;
  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 " + _appHost.Name + " 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. public string Key
  120. {
  121. get { return "SystemUpdateTask"; }
  122. }
  123. }
  124. }