PluginUpdateTask.cs 3.8 KB

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