Files
jellyfin-jellyfin-1/Emby.Server.Implementations/ScheduledTasks/Tasks/PluginUpdateTask.cs

125 lines
3.9 KiB
C#
Raw Normal View History

2019-01-31 03:11:43 +09:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-11-14 14:30:34 -07:00
using System.Net.Http;
2019-01-31 03:11:43 +09:00
using System.Threading;
using System.Threading.Tasks;
2019-10-08 20:51:11 +02:00
using MediaBrowser.Common.Updates;
2020-12-06 23:48:54 +00:00
using MediaBrowser.Model.Globalization;
2019-01-31 03:11:43 +09:00
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
/// <summary>
/// Plugin Update Task.
/// </summary>
public class PluginUpdateTask : IScheduledTask, IConfigurableScheduledTask
2019-01-31 03:11:43 +09:00
{
2025-04-30 09:29:13 +02:00
private readonly ILogger<PluginUpdateTask> _logger;
private readonly IInstallationManager _installationManager;
private readonly ILocalizationManager _localization;
2019-01-31 03:11:43 +09:00
/// <summary>
2025-04-30 09:29:13 +02:00
/// Initializes a new instance of the <see cref="PluginUpdateTask" /> class.
2019-01-31 03:11:43 +09:00
/// </summary>
2025-04-30 09:29:13 +02:00
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
/// <param name="installationManager">Instance of the <see cref="IInstallationManager"/> interface.</param>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
public PluginUpdateTask(ILogger<PluginUpdateTask> logger, IInstallationManager installationManager, ILocalizationManager localization)
2019-01-31 03:11:43 +09:00
{
2025-04-30 09:29:13 +02:00
_logger = logger;
_installationManager = installationManager;
_localization = localization;
}
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Name => _localization.GetLocalizedString("TaskUpdatePlugins");
2020-08-31 22:20:19 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Description => _localization.GetLocalizedString("TaskUpdatePluginsDescription");
2020-08-31 22:20:19 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Category => _localization.GetLocalizedString("TasksApplicationCategory");
2020-08-31 22:20:19 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Key => "PluginUpdates";
2020-08-31 22:20:19 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public bool IsHidden => false;
2020-08-31 22:20:19 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public bool IsEnabled => true;
2020-08-31 22:20:19 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public bool IsLogged => true;
2020-08-31 22:20:19 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
yield return new TaskTriggerInfo
2019-01-31 03:11:43 +09:00
{
2025-04-30 09:29:13 +02:00
Type = TaskTriggerInfoType.StartupTrigger
};
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
yield return new TaskTriggerInfo
2019-01-31 03:11:43 +09:00
{
2025-04-30 09:29:13 +02:00
Type = TaskTriggerInfoType.IntervalTrigger,
IntervalTicks = TimeSpan.FromHours(24).Ticks
};
}
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
progress.Report(0);
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
var packageFetchTask = _installationManager.GetAvailablePluginUpdates(cancellationToken);
var packagesToInstall = (await packageFetchTask.ConfigureAwait(false)).ToList();
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
progress.Report(10);
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
var numComplete = 0;
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
foreach (var package in packagesToInstall)
{
cancellationToken.ThrowIfCancellationRequested();
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
try
{
await _installationManager.InstallPackage(package, cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// InstallPackage has its own inner cancellation token, so only throw this if it's ours
if (cancellationToken.IsCancellationRequested)
2019-01-31 03:11:43 +09:00
{
2025-04-30 09:29:13 +02:00
throw;
2019-01-31 03:11:43 +09:00
}
}
2025-04-30 09:29:13 +02:00
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Error downloading {Name}", package.Name);
}
catch (IOException ex)
{
_logger.LogError(ex, "Error updating {Name}", package.Name);
}
catch (InvalidDataException ex)
{
_logger.LogError(ex, "Error updating {Name}", package.Name);
}
2019-01-31 03:11:43 +09:00
2025-04-30 09:29:13 +02:00
// Update progress
lock (progress)
{
progress.Report((90.0 * ++numComplete / packagesToInstall.Count) + 10);
}
2019-01-31 03:11:43 +09:00
}
2025-04-30 09:29:13 +02:00
progress.Report(100);
2019-01-31 03:11:43 +09:00
}
}