PluginUpdateTask.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using MediaBrowser.Common.ScheduledTasks;
  2. using MediaBrowser.Model.Logging;
  3. using MediaBrowser.Model.Net;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.ScheduledTasks
  11. {
  12. /// <summary>
  13. /// Plugin Update Task
  14. /// </summary>
  15. public class PluginUpdateTask : IScheduledTask
  16. {
  17. /// <summary>
  18. /// The _kernel
  19. /// </summary>
  20. private readonly Kernel _kernel;
  21. /// <summary>
  22. /// The _logger
  23. /// </summary>
  24. private readonly ILogger _logger;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="PluginUpdateTask" /> class.
  27. /// </summary>
  28. /// <param name="kernel">The kernel.</param>
  29. /// <param name="logger">The logger.</param>
  30. public PluginUpdateTask(Kernel kernel, ILogger logger)
  31. {
  32. _kernel = kernel;
  33. _logger = logger;
  34. }
  35. /// <summary>
  36. /// Creates the triggers that define when the task will run
  37. /// </summary>
  38. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  39. public IEnumerable<ITaskTrigger> GetDefaultTriggers()
  40. {
  41. return new ITaskTrigger[] {
  42. // 1:30am
  43. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1.5) },
  44. new IntervalTrigger { Interval = TimeSpan.FromHours(2)}
  45. };
  46. }
  47. /// <summary>
  48. /// Update installed plugins
  49. /// </summary>
  50. /// <param name="cancellationToken">The cancellation token.</param>
  51. /// <param name="progress">The progress.</param>
  52. /// <returns>Task.</returns>
  53. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  54. {
  55. progress.Report(0);
  56. var packagesToInstall = (await _kernel.InstallationManager.GetAvailablePluginUpdates(true, cancellationToken).ConfigureAwait(false)).ToList();
  57. progress.Report(10);
  58. var numComplete = 0;
  59. // Create tasks for each one
  60. var tasks = packagesToInstall.Select(i => Task.Run(async () =>
  61. {
  62. cancellationToken.ThrowIfCancellationRequested();
  63. try
  64. {
  65. await _kernel.InstallationManager.InstallPackage(i, new Progress<double> { }, cancellationToken).ConfigureAwait(false);
  66. }
  67. catch (OperationCanceledException)
  68. {
  69. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  70. if (cancellationToken.IsCancellationRequested)
  71. {
  72. throw;
  73. }
  74. }
  75. catch (HttpException ex)
  76. {
  77. _logger.ErrorException("Error downloading {0}", ex, i.name);
  78. }
  79. catch (IOException ex)
  80. {
  81. _logger.ErrorException("Error updating {0}", ex, i.name);
  82. }
  83. // Update progress
  84. lock (progress)
  85. {
  86. numComplete++;
  87. double percent = numComplete;
  88. percent /= packagesToInstall.Count;
  89. progress.Report((90 * percent) + 10);
  90. }
  91. }));
  92. cancellationToken.ThrowIfCancellationRequested();
  93. await Task.WhenAll(tasks).ConfigureAwait(false);
  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 plugin 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 updates for plugins that are configured to update automatically."; }
  111. }
  112. public string Category
  113. {
  114. get { return "Application"; }
  115. }
  116. }
  117. }