fix: add cancellation token to downloader job

This commit is contained in:
Guillermo Marcel 2025-02-17 13:42:14 -03:00
parent dc34c85834
commit f16ea91e95
3 changed files with 9 additions and 9 deletions

View File

@ -20,7 +20,7 @@ public class ShinobiConnector : IDVRConnector
_options = options.Value; _options = options.Value;
} }
public async Task<List<VideoDetail>> FetchMonitorVideosBetween(DateTime from, DateTime to) public async Task<List<VideoDetail>> FetchMonitorVideosBetween(DateTime from, DateTime to, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{_options.URL}/{_options.APIKey}/videos/{_options.GroupId}/{_options.MonitorId}"; var endpoint = $"{_options.URL}/{_options.APIKey}/videos/{_options.GroupId}/{_options.MonitorId}";
endpoint += $"?start={from:yyyy-MM-ddTHH:mm:sszzz}&end={to:yyyy-MM-ddTHH:mm:sszzz}"; endpoint += $"?start={from:yyyy-MM-ddTHH:mm:sszzz}&end={to:yyyy-MM-ddTHH:mm:sszzz}";
@ -28,7 +28,7 @@ public class ShinobiConnector : IDVRConnector
_logger.LogDebug("Fetching videos details from endpoint: {Endpoint}", endpoint); _logger.LogDebug("Fetching videos details from endpoint: {Endpoint}", endpoint);
//get from the server the response with type VideoDetails //get from the server the response with type VideoDetails
var response = await _httpClient.GetFromJsonAsync<FetchVideoResponse>(endpoint); var response = await _httpClient.GetFromJsonAsync<FetchVideoResponse>(endpoint, cancellationToken);
if (response is null) if (response is null)
{ {
@ -49,7 +49,7 @@ public class ShinobiConnector : IDVRConnector
} }
public async Task DownloadMonitorVideo(VideoDetail video, string downloadFolder) public async Task DownloadMonitorVideo(VideoDetail video, string downloadFolder, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{_options.URL}{video.href}"; var endpoint = $"{_options.URL}{video.href}";
_logger.LogDebug("Fetching video from endpoint: {Endpoint}", endpoint); _logger.LogDebug("Fetching video from endpoint: {Endpoint}", endpoint);
@ -61,7 +61,7 @@ public class ShinobiConnector : IDVRConnector
try try
{ {
_logger.LogDebug("Downloading video..."); _logger.LogDebug("Downloading video...");
var videoData = await _httpClient.GetByteArrayAsync(endpoint); var videoData = await _httpClient.GetByteArrayAsync(endpoint, cancellationToken);
if(_options.RunDry) if(_options.RunDry)
{ {
@ -69,7 +69,7 @@ public class ShinobiConnector : IDVRConnector
return; return;
} }
//Write the video to the file //Write the video to the file
await File.WriteAllBytesAsync(videoPath, videoData); await File.WriteAllBytesAsync(videoPath, videoData, cancellationToken);
_logger.LogInformation("Video downloaded successfully!"); _logger.LogInformation("Video downloaded successfully!");
} }
catch (Exception ex) catch (Exception ex)

View File

@ -4,6 +4,6 @@ namespace AutoScan.Interfaces;
public interface IDVRConnector public interface IDVRConnector
{ {
Task<List<VideoDetail>> FetchMonitorVideosBetween(DateTime from, DateTime to); Task<List<VideoDetail>> FetchMonitorVideosBetween(DateTime from, DateTime to, CancellationToken cancellationToken = default);
Task DownloadMonitorVideo(VideoDetail video, string downloadFolder); Task DownloadMonitorVideo(VideoDetail video, string downloadFolder, CancellationToken cancellationToken = default);
} }

View File

@ -50,7 +50,7 @@ public class DownloaderJob : IJob
var to = new DateTime(now.Year, now.Month, now.Day, int.Parse(hours), int.Parse(minutes), 0); var to = new DateTime(now.Year, now.Month, now.Day, int.Parse(hours), int.Parse(minutes), 0);
_logger.LogInformation("Fetching videos from {From} to {To}", from, to); _logger.LogInformation("Fetching videos from {From} to {To}", from, to);
var videos = await _dvrConnector.FetchMonitorVideosBetween(from, to); var videos = await _dvrConnector.FetchMonitorVideosBetween(from, to, context.CancellationToken);
//if the amount of videos is greater than the max amount in options, log a warning //if the amount of videos is greater than the max amount in options, log a warning
if (_options.MaxAmount > 0 && videos.Count > _options.MaxAmount) if (_options.MaxAmount > 0 && videos.Count > _options.MaxAmount)
@ -65,7 +65,7 @@ public class DownloaderJob : IJob
foreach (var video in videos) foreach (var video in videos)
{ {
_logger.LogDebug("Downloading video {Filename}", video.filename); _logger.LogDebug("Downloading video {Filename}", video.filename);
await _dvrConnector.DownloadMonitorVideo(video, _options.MediaFolder); await _dvrConnector.DownloadMonitorVideo(video, _options.MediaFolder, context.CancellationToken);
} }
context.Result = new JobResult() context.Result = new JobResult()