SystemUpdateTask.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using MediaBrowser.Common.Kernel;
  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 kernel.
  21. /// </summary>
  22. /// <value>The kernel.</value>
  23. private IKernel Kernel { 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="kernel">The kernel.</param>
  34. /// <param name="logger">The logger.</param>
  35. public SystemUpdateTask(IApplicationHost appHost, IKernel kernel, ILogger logger)
  36. {
  37. _appHost = appHost;
  38. Kernel = kernel;
  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. return new ITaskTrigger[] {
  48. // 1am
  49. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1) },
  50. new IntervalTrigger { Interval = TimeSpan.FromHours(2)}
  51. };
  52. }
  53. /// <summary>
  54. /// Returns the task to be executed
  55. /// </summary>
  56. /// <param name="cancellationToken">The cancellation token.</param>
  57. /// <param name="progress">The progress.</param>
  58. /// <returns>Task.</returns>
  59. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  60. {
  61. if (!_appHost.CanSelfUpdate) return;
  62. EventHandler<double> innerProgressHandler = (sender, e) => progress.Report(e * .1);
  63. // Create a progress object for the update check
  64. var innerProgress = new Progress<double>();
  65. innerProgress.ProgressChanged += innerProgressHandler;
  66. var updateInfo = await _appHost.CheckForApplicationUpdate(cancellationToken, innerProgress).ConfigureAwait(false);
  67. // Release the event handler
  68. innerProgress.ProgressChanged -= innerProgressHandler;
  69. progress.Report(10);
  70. if (!updateInfo.IsUpdateAvailable)
  71. {
  72. progress.Report(100);
  73. return;
  74. }
  75. cancellationToken.ThrowIfCancellationRequested();
  76. if (Kernel.Configuration.EnableAutoUpdate)
  77. {
  78. Logger.Info("Update Revision {0} available. Updating...", updateInfo.AvailableVersion);
  79. innerProgressHandler = (sender, e) => progress.Report((e * .9) + .1);
  80. innerProgress = new Progress<double>();
  81. innerProgress.ProgressChanged += innerProgressHandler;
  82. await _appHost.UpdateApplication(updateInfo.Package, cancellationToken, innerProgress).ConfigureAwait(false);
  83. // Release the event handler
  84. innerProgress.ProgressChanged -= innerProgressHandler;
  85. Kernel.OnApplicationUpdated(updateInfo.AvailableVersion);
  86. }
  87. else
  88. {
  89. Logger.Info("A new version of Media Browser is available.");
  90. }
  91. progress.Report(100);
  92. }
  93. /// <summary>
  94. /// Gets the name of the task
  95. /// </summary>
  96. /// <value>The name.</value>
  97. public string Name
  98. {
  99. get { return "Check for application updates"; }
  100. }
  101. /// <summary>
  102. /// Gets the description.
  103. /// </summary>
  104. /// <value>The description.</value>
  105. public string Description
  106. {
  107. get { return "Downloads and installs application updates."; }
  108. }
  109. /// <summary>
  110. /// Gets the category.
  111. /// </summary>
  112. /// <value>The category.</value>
  113. public string Category
  114. {
  115. get { return "Application"; }
  116. }
  117. }
  118. }