From a1caa789fea92d5c9d02885b9c524f12c6199be9 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 2 Jul 2011 01:56:58 -0700 Subject: [PATCH] Instant progress notifications. Now use comet instead of pooling. --- .../Notification/ProgressNotification.cs | 2 +- .../Providers/Jobs/EpisodeSearchJob.cs | 2 + .../Providers/NotificationProvider.cs | 2 +- .../Controllers/NotificationController.cs | 37 +++++++++++-- NzbDrone.Web/Scripts/Notification.js | 53 +++++++------------ 5 files changed, 57 insertions(+), 39 deletions(-) diff --git a/NzbDrone.Core/Model/Notification/ProgressNotification.cs b/NzbDrone.Core/Model/Notification/ProgressNotification.cs index a95aa9610..80e8bc64a 100644 --- a/NzbDrone.Core/Model/Notification/ProgressNotification.cs +++ b/NzbDrone.Core/Model/Notification/ProgressNotification.cs @@ -27,7 +27,7 @@ namespace NzbDrone.Core.Model.Notification /// Gets or sets the title for this notification. /// /// The title. - public String Title { get; set; } + public String Title { get; private set; } /// /// Gets or sets the current status of this task. this field could be use to show the currently processing item in a long running task. diff --git a/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs b/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs index 45330756a..d011acc28 100644 --- a/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs +++ b/NzbDrone.Core/Providers/Jobs/EpisodeSearchJob.cs @@ -51,6 +51,8 @@ namespace NzbDrone.Core.Providers.Jobs Logger.Error("Unable to find an episode {0} in database", targetId); return; } + notification.CurrentMessage = "Searching for " + episode; + var series = episode.Series; diff --git a/NzbDrone.Core/Providers/NotificationProvider.cs b/NzbDrone.Core/Providers/NotificationProvider.cs index aebd6b114..00ee08ada 100644 --- a/NzbDrone.Core/Providers/NotificationProvider.cs +++ b/NzbDrone.Core/Providers/NotificationProvider.cs @@ -20,7 +20,7 @@ namespace NzbDrone.Core.Providers get { return new List(_basicNotifications.Values); } } - public virtual List GetProgressNotifications + public virtual List ProgressNotifications { get { diff --git a/NzbDrone.Web/Controllers/NotificationController.cs b/NzbDrone.Web/Controllers/NotificationController.cs index 25d9cac2a..186b34ee8 100644 --- a/NzbDrone.Web/Controllers/NotificationController.cs +++ b/NzbDrone.Web/Controllers/NotificationController.cs @@ -1,4 +1,7 @@ -using System.Web.Mvc; +using System; +using System.Diagnostics; +using System.Threading; +using System.Web.Mvc; using MvcMiniProfiler; using NzbDrone.Core.Providers; @@ -32,8 +35,8 @@ namespace NzbDrone.Web.Controllers else { - if (_notifications.GetProgressNotifications.Count != 0) - message = _notifications.GetProgressNotifications[0].CurrentMessage; + if (_notifications.ProgressNotifications.Count != 0) + message = _notifications.ProgressNotifications[0].CurrentMessage; } @@ -44,5 +47,33 @@ namespace NzbDrone.Web.Controllers return Json(message, JsonRequestBehavior.AllowGet); } + + + [HttpGet] + public JsonResult Comet(string message) + { + var requestTimer = Stopwatch.StartNew(); + + MiniProfiler.Stop(true); + + var currentMessage = GetCurrentMessage(); + + while (message == currentMessage && requestTimer.Elapsed.TotalSeconds < 10) + { + Thread.Sleep(250); + currentMessage = GetCurrentMessage(); + } + + return Json(currentMessage, JsonRequestBehavior.AllowGet); + } + + private string GetCurrentMessage() + { + if (_notifications.ProgressNotifications.Count != 0) + return _notifications.ProgressNotifications[0].CurrentMessage; + + + return string.Empty; + } } } \ No newline at end of file diff --git a/NzbDrone.Web/Scripts/Notification.js b/NzbDrone.Web/Scripts/Notification.js index 5d300500a..77c88262e 100644 --- a/NzbDrone.Web/Scripts/Notification.js +++ b/NzbDrone.Web/Scripts/Notification.js @@ -1,30 +1,27 @@ -/// - $(document).ready(function () - { - var speed = 0; +$(window).load(function () { + var speed = 700; var isShown = false; - refreshNotifications(); + var currentMessage = ""; + + $.doTimeout(200, refreshNotifications); + - var timer = window.setInterval(function () { - speed = 1000; - refreshNotifications(); - }, 2000); function refreshNotifications() { - $.ajax({ - url: '/Notification', - success: notificationCallback - }); + $.get('/notification/Comet', { message: currentMessage }, notificationCallback); } function notificationCallback(data) { + currentMessage = data; if (data === "") { - CloseMsg(); + closeMsg(); } else { - DisplayMsg(data); + displayMsg(data); } + + refreshNotifications(); } //SetupNotifications(); @@ -32,32 +29,20 @@ - function DisplayMsg(sMsg) { + function displayMsg(sMsg) { //set the message text - - - //$("#msgText").text(sMsg); - $("#msgText").showHtml(sMsg, 200); - + $("#msgText").showHtml(sMsg, 150); if (!isShown) { - isShown = true; - if (speed === 0) { - $('#msgBox').show(); - } - else { - $('#msgBox').show("slide", { direction: "right" }, speed); - } - + $('#msgBox').show("slide", { direction: "right" }, speed / 2); } + + isShown = true; } - function CloseMsg() { + function closeMsg() { //hide the message - if (isShown) { - $('#msgBox').hide("slide", { direction: "right" }, speed); - } - + $('#msgBox').hide("slide", { direction: "right" }, speed); isShown = false; } });