using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.SRFPlay.Services;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.SRFPlay.ScheduledTasks;
///
/// Scheduled task for refreshing SRF Play content.
///
public class ContentRefreshTask : IScheduledTask
{
private readonly ILogger _logger;
private readonly ContentRefreshService _contentRefreshService;
///
/// Initializes a new instance of the class.
///
/// The logger instance.
/// The content refresh service.
public ContentRefreshTask(
ILogger logger,
ContentRefreshService contentRefreshService)
{
_logger = logger;
_contentRefreshService = contentRefreshService;
}
///
public string Name => "Refresh SRF Play Content";
///
public string Description => "Refreshes latest and trending content from SRF Play";
///
public string Category => "SRF Play";
///
public string Key => "SRFPlayContentRefresh";
///
public async Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken)
{
_logger.LogInformation("Starting SRF Play content refresh task");
progress?.Report(0);
try
{
var config = Plugin.Instance?.Configuration;
if (config == null)
{
_logger.LogWarning("Plugin configuration not available");
return;
}
if (!config.EnableLatestContent && !config.EnableTrendingContent)
{
_logger.LogInformation("Content refresh is disabled in configuration");
progress?.Report(100);
return;
}
// Refresh latest content
if (config.EnableLatestContent)
{
_logger.LogInformation("Refreshing latest content");
progress?.Report(25);
await _contentRefreshService.RefreshLatestContentAsync(cancellationToken).ConfigureAwait(false);
}
// Refresh trending content
if (config.EnableTrendingContent)
{
_logger.LogInformation("Refreshing trending content");
progress?.Report(75);
await _contentRefreshService.RefreshTrendingContentAsync(cancellationToken).ConfigureAwait(false);
}
progress?.Report(100);
_logger.LogInformation("SRF Play content refresh task completed successfully");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during SRF Play content refresh task");
throw;
}
}
///
public IEnumerable GetDefaultTriggers()
{
var config = Plugin.Instance?.Configuration;
var intervalHours = config?.ContentRefreshIntervalHours ?? 6;
// Run every X hours as configured
return new[]
{
new TaskTriggerInfo
{
Type = TaskTriggerInfo.TriggerInterval,
IntervalTicks = TimeSpan.FromHours(intervalHours).Ticks
}
};
}
}