SystemUpdateTask.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Common.Progress;
  9. using MediaBrowser.Model.Tasks;
  10. namespace Emby.Server.Implementations.ScheduledTasks
  11. {
  12. /// <summary>
  13. /// Plugin Update Task
  14. /// </summary>
  15. public class SystemUpdateTask : IScheduledTask
  16. {
  17. /// <summary>
  18. /// The _app host
  19. /// </summary>
  20. private readonly IApplicationHost _appHost;
  21. /// <summary>
  22. /// Gets or sets the configuration manager.
  23. /// </summary>
  24. /// <value>The configuration manager.</value>
  25. private IConfigurationManager ConfigurationManager { get; set; }
  26. /// <summary>
  27. /// Gets or sets the logger.
  28. /// </summary>
  29. /// <value>The logger.</value>
  30. private ILogger Logger { get; set; }
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="SystemUpdateTask" /> class.
  33. /// </summary>
  34. /// <param name="appHost">The app host.</param>
  35. /// <param name="configurationManager">The configuration manager.</param>
  36. /// <param name="logger">The logger.</param>
  37. public SystemUpdateTask(IApplicationHost appHost, IConfigurationManager configurationManager, ILogger logger)
  38. {
  39. _appHost = appHost;
  40. ConfigurationManager = configurationManager;
  41. Logger = logger;
  42. }
  43. /// <summary>
  44. /// Creates the triggers that define when the task will run
  45. /// </summary>
  46. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  47. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  48. {
  49. return new[] {
  50. // At startup
  51. new TaskTriggerInfo {Type = TaskTriggerInfo.TriggerStartup},
  52. // Every so often
  53. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  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. // Create a progress object for the update check
  65. var updateInfo = await _appHost.CheckForApplicationUpdate(cancellationToken, new SimpleProgress<double>()).ConfigureAwait(false);
  66. progress.Report(10);
  67. if (!updateInfo.IsUpdateAvailable)
  68. {
  69. Logger.Debug("No application update available.");
  70. return;
  71. }
  72. cancellationToken.ThrowIfCancellationRequested();
  73. if (!_appHost.CanSelfUpdate) return;
  74. if (ConfigurationManager.CommonConfiguration.EnableAutoUpdate)
  75. {
  76. Logger.Info("Update Revision {0} available. Updating...", updateInfo.AvailableVersion);
  77. EventHandler<double> innerProgressHandler = (sender, e) => progress.Report(e * .9 + .1);
  78. var innerProgress = new SimpleProgress<double>();
  79. innerProgress.ProgressChanged += innerProgressHandler;
  80. await _appHost.UpdateApplication(updateInfo.Package, cancellationToken, innerProgress).ConfigureAwait(false);
  81. // Release the event handler
  82. innerProgress.ProgressChanged -= innerProgressHandler;
  83. }
  84. else
  85. {
  86. Logger.Info("A new version of " + _appHost.Name + " is available.");
  87. }
  88. }
  89. /// <summary>
  90. /// Gets the name of the task
  91. /// </summary>
  92. /// <value>The name.</value>
  93. public string Name
  94. {
  95. get { return "Check for application updates"; }
  96. }
  97. /// <summary>
  98. /// Gets the description.
  99. /// </summary>
  100. /// <value>The description.</value>
  101. public string Description
  102. {
  103. get { return "Downloads and installs application updates."; }
  104. }
  105. /// <summary>
  106. /// Gets the category.
  107. /// </summary>
  108. /// <value>The category.</value>
  109. public string Category
  110. {
  111. get { return "Application"; }
  112. }
  113. public string Key
  114. {
  115. get { return "SystemUpdateTask"; }
  116. }
  117. }
  118. }