PluginUpdateTask.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 : BaseScheduledTask<Kernel>
  16. {
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="PluginUpdateTask" /> class.
  19. /// </summary>
  20. /// <param name="kernel">The kernel.</param>
  21. /// <param name="logger"></param>
  22. public PluginUpdateTask(Kernel kernel, ITaskManager taskManager, ILogger logger)
  23. : base(kernel, taskManager, logger)
  24. {
  25. }
  26. /// <summary>
  27. /// Creates the triggers that define when the task will run
  28. /// </summary>
  29. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  30. public override IEnumerable<ITaskTrigger> GetDefaultTriggers()
  31. {
  32. return new ITaskTrigger[] {
  33. // 1:30am
  34. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(1.5) },
  35. new IntervalTrigger { Interval = TimeSpan.FromHours(2)}
  36. };
  37. }
  38. /// <summary>
  39. /// Update installed plugins
  40. /// </summary>
  41. /// <param name="cancellationToken">The cancellation token.</param>
  42. /// <param name="progress">The progress.</param>
  43. /// <returns>Task.</returns>
  44. protected override async Task ExecuteInternal(CancellationToken cancellationToken, IProgress<double> progress)
  45. {
  46. progress.Report(0);
  47. var packagesToInstall = (await Kernel.InstallationManager.GetAvailablePluginUpdates(true, cancellationToken).ConfigureAwait(false)).ToList();
  48. progress.Report(10);
  49. var numComplete = 0;
  50. // Create tasks for each one
  51. var tasks = packagesToInstall.Select(i => Task.Run(async () =>
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. try
  55. {
  56. await Kernel.InstallationManager.InstallPackage(i, new Progress<double> { }, cancellationToken).ConfigureAwait(false);
  57. }
  58. catch (OperationCanceledException)
  59. {
  60. // InstallPackage has it's own inner cancellation token, so only throw this if it's ours
  61. if (cancellationToken.IsCancellationRequested)
  62. {
  63. throw;
  64. }
  65. }
  66. catch (HttpException ex)
  67. {
  68. Logger.ErrorException("Error downloading {0}", ex, i.name);
  69. }
  70. catch (IOException ex)
  71. {
  72. Logger.ErrorException("Error updating {0}", ex, i.name);
  73. }
  74. // Update progress
  75. lock (progress)
  76. {
  77. numComplete++;
  78. double percent = numComplete;
  79. percent /= packagesToInstall.Count;
  80. progress.Report((90 * percent) + 10);
  81. }
  82. }));
  83. cancellationToken.ThrowIfCancellationRequested();
  84. await Task.WhenAll(tasks).ConfigureAwait(false);
  85. progress.Report(100);
  86. }
  87. /// <summary>
  88. /// Gets the name of the task
  89. /// </summary>
  90. /// <value>The name.</value>
  91. public override string Name
  92. {
  93. get { return "Check for plugin updates"; }
  94. }
  95. /// <summary>
  96. /// Gets the description.
  97. /// </summary>
  98. /// <value>The description.</value>
  99. public override string Description
  100. {
  101. get { return "Downloads and installs updates for plugins that are configured to update automatically."; }
  102. }
  103. }
  104. }