mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-25 22:37:27 -04:00
New: Rebuilt Completed/Failed download handling from scratch
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.Download;
|
||||
using NzbDrone.Core.Download.TrackedDownloads;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
using NzbDrone.Core.Qualities;
|
||||
using NzbDrone.Core.Tv;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.Queue
|
||||
{
|
||||
@@ -21,7 +21,7 @@ namespace NzbDrone.Core.Queue
|
||||
public String Status { get; set; }
|
||||
public String TrackedDownloadStatus { get; set; }
|
||||
public List<TrackedDownloadStatusMessage> StatusMessages { get; set; }
|
||||
public RemoteEpisode RemoteEpisode { get; set; }
|
||||
public String TrackingId { get; set; }
|
||||
public RemoteEpisode RemoteEpisode { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using NzbDrone.Common.TPL;
|
||||
using NzbDrone.Core.Lifecycle;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace NzbDrone.Core.Queue
|
||||
{
|
||||
public class QueueScheduler : IHandle<ApplicationStartedEvent>,
|
||||
IHandle<ApplicationShutdownRequested>
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly Logger _logger;
|
||||
private static readonly Timer Timer = new Timer();
|
||||
private static CancellationTokenSource _cancellationTokenSource;
|
||||
|
||||
public QueueScheduler(IEventAggregator eventAggregator, Logger logger)
|
||||
{
|
||||
_eventAggregator = eventAggregator;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private void CheckQueue()
|
||||
{
|
||||
try
|
||||
{
|
||||
Timer.Enabled = false;
|
||||
_eventAggregator.PublishEvent(new UpdateQueueEvent());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!_cancellationTokenSource.IsCancellationRequested)
|
||||
{
|
||||
Timer.Enabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Handle(ApplicationStartedEvent message)
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
Timer.Interval = 1000 * 30;
|
||||
Timer.Elapsed += (o, args) => Task.Factory.StartNew(CheckQueue, _cancellationTokenSource.Token)
|
||||
.LogExceptions();
|
||||
|
||||
Timer.Start();
|
||||
}
|
||||
|
||||
public void Handle(ApplicationShutdownRequested message)
|
||||
{
|
||||
_logger.Info("Shutting down queue scheduler");
|
||||
_cancellationTokenSource.Cancel(true);
|
||||
Timer.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Core.Download;
|
||||
using NzbDrone.Core.Download.TrackedDownloads;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
|
||||
namespace NzbDrone.Core.Queue
|
||||
{
|
||||
@@ -11,64 +12,63 @@ namespace NzbDrone.Core.Queue
|
||||
Queue Find(int id);
|
||||
}
|
||||
|
||||
public class QueueService : IQueueService
|
||||
public class QueueService : IQueueService, IHandle<TrackedDownloadRefreshedEvent>
|
||||
{
|
||||
private readonly IDownloadTrackingService _downloadTrackingService;
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private static List<Queue> _queue = new List<Queue>();
|
||||
|
||||
public QueueService(IDownloadTrackingService downloadTrackingService)
|
||||
public QueueService(IEventAggregator eventAggregator)
|
||||
{
|
||||
_downloadTrackingService = downloadTrackingService;
|
||||
_eventAggregator = eventAggregator;
|
||||
}
|
||||
|
||||
public List<Queue> GetQueue()
|
||||
{
|
||||
var queueItems = _downloadTrackingService.GetQueuedDownloads()
|
||||
.OrderBy(v => v.DownloadItem.RemainingTime)
|
||||
.ToList();
|
||||
|
||||
return MapQueue(queueItems);
|
||||
return _queue;
|
||||
}
|
||||
|
||||
public Queue Find(int id)
|
||||
{
|
||||
return GetQueue().SingleOrDefault(q => q.Id == id);
|
||||
return _queue.SingleOrDefault(q => q.Id == id);
|
||||
}
|
||||
|
||||
private List<Queue> MapQueue(IEnumerable<TrackedDownload> trackedDownloads)
|
||||
public void Handle(TrackedDownloadRefreshedEvent message)
|
||||
{
|
||||
var queued = new List<Queue>();
|
||||
_queue = message.TrackedDownloads.OrderBy(c => c.DownloadItem.RemainingTime).SelectMany(MapQueue)
|
||||
.ToList();
|
||||
|
||||
foreach (var trackedDownload in trackedDownloads)
|
||||
_eventAggregator.PublishEvent(new QueueUpdatedEvent());
|
||||
}
|
||||
|
||||
private static IEnumerable<Queue> MapQueue(TrackedDownload trackedDownload)
|
||||
{
|
||||
foreach (var episode in trackedDownload.RemoteEpisode.Episodes)
|
||||
{
|
||||
foreach (var episode in trackedDownload.RemoteEpisode.Episodes)
|
||||
var queue = new Queue
|
||||
{
|
||||
var queue = new Queue
|
||||
{
|
||||
Id = episode.Id ^ (trackedDownload.DownloadItem.DownloadClientId.GetHashCode() << 16),
|
||||
Series = trackedDownload.RemoteEpisode.Series,
|
||||
Episode = episode,
|
||||
Quality = trackedDownload.RemoteEpisode.ParsedEpisodeInfo.Quality,
|
||||
Title = trackedDownload.DownloadItem.Title,
|
||||
Size = trackedDownload.DownloadItem.TotalSize,
|
||||
Sizeleft = trackedDownload.DownloadItem.RemainingSize,
|
||||
Timeleft = trackedDownload.DownloadItem.RemainingTime,
|
||||
Status = trackedDownload.DownloadItem.Status.ToString(),
|
||||
RemoteEpisode = trackedDownload.RemoteEpisode,
|
||||
TrackedDownloadStatus = trackedDownload.Status.ToString(),
|
||||
StatusMessages = trackedDownload.StatusMessages,
|
||||
TrackingId = trackedDownload.TrackingId
|
||||
};
|
||||
Id = episode.Id ^ (trackedDownload.DownloadItem.DownloadId.GetHashCode() << 16),
|
||||
Series = trackedDownload.RemoteEpisode.Series,
|
||||
Episode = episode,
|
||||
Quality = trackedDownload.RemoteEpisode.ParsedEpisodeInfo.Quality,
|
||||
Title = trackedDownload.DownloadItem.Title,
|
||||
Size = trackedDownload.DownloadItem.TotalSize,
|
||||
Sizeleft = trackedDownload.DownloadItem.RemainingSize,
|
||||
Timeleft = trackedDownload.DownloadItem.RemainingTime,
|
||||
Status = trackedDownload.DownloadItem.Status.ToString(),
|
||||
TrackedDownloadStatus = trackedDownload.Status.ToString(),
|
||||
StatusMessages = trackedDownload.StatusMessages.ToList(),
|
||||
RemoteEpisode = trackedDownload.RemoteEpisode,
|
||||
TrackingId = trackedDownload.TrackingId
|
||||
};
|
||||
|
||||
if (queue.Timeleft.HasValue)
|
||||
{
|
||||
queue.EstimatedCompletionTime = DateTime.UtcNow.Add(queue.Timeleft.Value);
|
||||
}
|
||||
|
||||
queued.Add(queue);
|
||||
if (queue.Timeleft.HasValue)
|
||||
{
|
||||
queue.EstimatedCompletionTime = DateTime.UtcNow.Add(queue.Timeleft.Value);
|
||||
}
|
||||
|
||||
yield return queue;
|
||||
}
|
||||
|
||||
return queued;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace NzbDrone.Core.Queue
|
||||
{
|
||||
public class UpdateQueueEvent : IEvent
|
||||
public class QueueUpdatedEvent : IEvent
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user