Example #1
0
        public async Task ProcessScrobble(TrackScrobble scrobble, IScrobbler scrobbler, CancellationToken cancellationToken)
        {
            await EnsureLoaded(cancellationToken);

            scrobblesQueue.Enqueue(scrobble);
            await SaveScrobblesQueue(cancellationToken);

            while (scrobblesQueue.Any())
            {
                var currentScrobble = scrobblesQueue.Peek();

                try
                {
                    await scrobbler.Scrobble(currentScrobble, cancellationToken);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    logger.LogWarning($"Scrobble failed: {currentScrobble}. Error: {e.Message}. Scrobbles queue size: {scrobblesQueue.Count}");
                    break;
                }

                scrobblesQueue.Dequeue();
            }

            await SaveScrobblesQueue(cancellationToken);
        }
        public async Task RegisterPlaybackFinish(SongModel song, CancellationToken cancellationToken)
        {
            var playbackDateTime = clock.Now;
            await songsService.AddSongPlayback(song, playbackDateTime, cancellationToken);

            var scrobble = new TrackScrobble
            {
                Track = GetTrackFromSong(song),
                PlayStartTimestamp = playbackDateTime - song.Duration,
                ChosenByUser       = true,
            };

            await scrobbler.Scrobble(scrobble, cancellationToken);
        }