[banshee] LastFM: Clean the queue after a scrobbling failure (bgo#667499)



commit 0d5feefcf75496fe5bb95cc60d8bfd80f7e91334
Author: Phil Trimble <PhilTrimble gmail com>
Date:   Sun Jun 3 19:27:41 2012 -0500

    LastFM: Clean the queue after a scrobbling failure (bgo#667499)
    
    If invalid tracks are present in the audioscrobbler queue we receive
    an error when submitting to LastFM. Since we are not cleaning the
    queue upon a failure we continue to attempt to resubmit indefinitely.
    
    Adds a step to remove invalid tracks from the queue upon receiving
    a failure so this no longer occurs. Also adds a simple logging
    statement when invalid data is detected in a track being added to
    the LastFM queue to help diagnose the root cause of the submission
    failures.
    
    Signed-off-by: Alexander Kojevnikov <alexk gnome org>

 .../Banshee.Lastfm.Audioscrobbler/Queue.cs         |   40 +++++++++++++++++++-
 .../Lastfm/Lastfm/AudioscrobblerConnection.cs      |   11 ++++-
 src/Libraries/Lastfm/Lastfm/IQueue.cs              |    1 +
 3 files changed, 49 insertions(+), 3 deletions(-)
---
diff --git a/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs b/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
index a259cd4..0973f95 100644
--- a/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
+++ b/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
@@ -49,6 +49,8 @@ namespace Banshee.Lastfm.Audioscrobbler
 {
     class Queue : IQueue
     {
+        private readonly TimeSpan MAXIMUM_TRACK_STARTTIME_IN_FUTURE = TimeSpan.FromDays (180);
+
         internal class QueuedTrack
         {
             private static DateTime epoch = DateTimeUtil.LocalUnixEpoch.ToUniversalTime ();
@@ -121,6 +123,12 @@ namespace Banshee.Lastfm.Audioscrobbler
                 get { return track_auth; }
             }
 
+            public override string ToString ()
+            {
+                return String.Format ("{0} - {1} (on {2} - track {3}) <duration: {4}sec, start time: {5}sec>",
+                                      Artist, Title, Album, TrackNumber, Duration, StartTime);
+            }
+
             string artist;
             string album;
             string title;
@@ -290,7 +298,16 @@ namespace Banshee.Lastfm.Audioscrobbler
         {
             TrackInfo t = (track as TrackInfo);
             if (t != null) {
-                queue.Add (new QueuedTrack (t, started_at));
+                QueuedTrack new_queued_track = new QueuedTrack (t, started_at);
+
+                //FIXME Just log invalid tracks until we determine the root cause
+                if (IsInvalidQueuedTrack (new_queued_track)) {
+                    Log.WarningFormat ("Invalid data detected while adding to audioscrobbler queue for " +
+                                       "track '{0}', original start time: '{1}'", new_queued_track, started_at);
+                }
+
+                queue.Add (new_queued_track);
+
                 dirty = true;
                 RaiseTrackAdded (this, new EventArgs ());
             }
@@ -312,5 +329,26 @@ namespace Banshee.Lastfm.Audioscrobbler
             if (handler != null)
                 handler (o, args);
         }
+
+        public void RemoveInvalidTracks ()
+        {
+            int removed_track_count = queue.RemoveAll (IsInvalidQueuedTrack);
+            if (removed_track_count > 0) {
+                Log.WarningFormat ("{0} invalid track(s) removed from audioscrobbler queue",
+                                   removed_track_count);
+                dirty = true;
+                Save ();
+            }
+        }
+
+        private bool IsInvalidQueuedTrack (QueuedTrack track)
+        {
+            DateTime trackStartTime = DateTimeUtil.FromTimeT (track.StartTime);
+
+            return (String.IsNullOrEmpty (track.Artist) ||
+                    String.IsNullOrEmpty (track.Title) ||
+                    trackStartTime < DateTimeUtil.LocalUnixEpoch ||
+                    trackStartTime > DateTime.Now.Add (MAXIMUM_TRACK_STARTTIME_IN_FUTURE));
+        }
     }
 }
diff --git a/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs b/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
index 9a3ee66..2596262 100644
--- a/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
+++ b/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
@@ -332,9 +332,16 @@ namespace Lastfm
                     last_upload_failed_logged = now;
                 }
 
-                // retransmit the queue on the next interval
                 hard_failures++;
-                state = State.NeedTransmit;
+
+                queue.RemoveInvalidTracks ();
+
+                // if there are still valid tracks in the queue then retransmit on the next interval
+                if (queue.Count > 0) {
+                    state = State.NeedTransmit;
+                } else {
+                    state = State.Idle;
+                }
             }
             else if (line.StartsWith ("BADSESSION")) {
                 if (now - last_upload_failed_logged > TimeSpan.FromMinutes(FAILURE_LOG_MINUTES)) {
diff --git a/src/Libraries/Lastfm/Lastfm/IQueue.cs b/src/Libraries/Lastfm/Lastfm/IQueue.cs
index 58d2975..9b1afaf 100644
--- a/src/Libraries/Lastfm/Lastfm/IQueue.cs
+++ b/src/Libraries/Lastfm/Lastfm/IQueue.cs
@@ -45,5 +45,6 @@ namespace Lastfm
 
         void Add (object track, DateTime started);
         void RemoveRange (int first, int count);
+        void RemoveInvalidTracks ();
     }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]