SystemUpdateTask.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if (!updateInfo.IsUpdateAvailable)
  67. {
  68. Logger.Debug("No application update available.");
  69. return;
  70. }
  71. cancellationToken.ThrowIfCancellationRequested();
  72. if (!_appHost.CanSelfUpdate) return;
  73. if (ConfigurationManager.CommonConfiguration.EnableAutoUpdate)
  74. {
  75. Logger.Info("Update Revision {0} available. Updating...", updateInfo.AvailableVersion);
  76. await _appHost.UpdateApplication(updateInfo.Package, cancellationToken, progress).ConfigureAwait(false);
  77. }
  78. else
  79. {
  80. Logger.Info("A new version of " + _appHost.Name + " is available.");
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the name of the task
  85. /// </summary>
  86. /// <value>The name.</value>
  87. public string Name
  88. {
  89. get { return "Check for application updates"; }
  90. }
  91. /// <summary>
  92. /// Gets the description.
  93. /// </summary>
  94. /// <value>The description.</value>
  95. public string Description
  96. {
  97. get { return "Downloads and installs application updates."; }
  98. }
  99. /// <summary>
  100. /// Gets the category.
  101. /// </summary>
  102. /// <value>The category.</value>
  103. public string Category
  104. {
  105. get { return "Application"; }
  106. }
  107. public string Key
  108. {
  109. get { return "SystemUpdateTask"; }
  110. }
  111. }
  112. }