using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Plugin.SRFPlay.Services.Interfaces;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Plugin.SRFPlay.ScheduledTasks;
///
/// Scheduled task that checks and manages sport livestream recordings.
/// Runs every 2 minutes to start scheduled recordings when streams go live
/// and stop recordings when they end.
///
public class RecordingSchedulerTask : IScheduledTask
{
private readonly ILogger _logger;
private readonly IRecordingService _recordingService;
///
/// Initializes a new instance of the class.
///
/// The logger.
/// The recording service.
public RecordingSchedulerTask(
ILogger logger,
IRecordingService recordingService)
{
_logger = logger;
_recordingService = recordingService;
}
///
public string Name => "Process SRF Play Recordings";
///
public string Description => "Checks scheduled recordings and starts/stops them as needed";
///
public string Category => "SRF Play";
///
public string Key => "SRFPlayRecordingScheduler";
///
public async Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken)
{
_logger.LogDebug("Processing SRF Play recordings");
progress?.Report(0);
try
{
await _recordingService.ProcessRecordingsAsync(cancellationToken).ConfigureAwait(false);
progress?.Report(100);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing recordings");
throw;
}
}
///
public IEnumerable GetDefaultTriggers()
{
return new[]
{
new TaskTriggerInfo
{
Type = TaskTriggerInfo.TriggerInterval,
IntervalTicks = TimeSpan.FromSeconds(30).Ticks
}
};
}
}