Thank you for your donation!


Problem: Spotify connect keeps dropping
#21
If librespot has crashed you should post the issue in the librespot Git so the developers can work with you to possibly find out what's going on.
https://github.com/librespot-org/librespot/issues
Reply
#22
(12-06-2018, 04:08 AM)Tim Curtis Wrote: If librespot has crashed you should post the issue in the librespot Git so the developers can work with you to possibly find out what's going on.
https://github.com/librespot-org/librespot/issues
Hi Tim,

The crashing / random stop working problem is a known issue, and the devs have a hard time getting it solved. It have something to do with the connection link between librespot and Spotify servers. (" session handling logic ?")

https://github.com/librespot-org/librespot/issues/134

You can "bandaid" the issue with a watchdog/crontab job that checks for status of the service - not a pretty solution but it seems to work. As librespot is not run as a standalone service as raspotify is, but rather a part of rc-local, I cant with my limited linux experience get systemctl to show status of librespot, and actually rewrite the script made by oboote in issue 134, see previous post.  

The script should be something like "if Spotify renderer is enabled and if librespot process is missing, then restart librespot with parameters in playerlib" - I'm not quite sure how case 'spotifysvc' implements. But it seems to me its actually possible to get it work, (with a set of skills way past my level). 

If someone could help I would be grateful; as the chance of getting the script working by myself is just as good as the Vatican City wins next soccer world championship. 
Reply
#23
I encoutered this issue with librespot 2 months ago and I have given up trying to get it running.
I am using the solution provided by Rafa, see http://moodeaudio.org/forum/showthread.php?tid=14
which works like a charm for months now, it even survived the updates from 4.2 to 4.3
The only litte issure i have here is that the webradio doesnt stop playing when i connect to spotify
Reply
#24
(12-06-2018, 12:54 PM)abrakadabra2k Wrote: I encoutered this issue with librespot 2 months ago and I have given up trying to get it running.
I am using the solution provided by Rafa, see http://moodeaudio.org/forum/showthread.php?tid=14
which works like a charm for months now, it even survived the updates from 4.2 to 4.3
The only litte issure i have here is that the webradio doesnt stop playing when i connect to spotify

Well I stick with Tim's implementation of librespot for now - I just LOVE that the webradio stops playing when I start playing via Airplay/Connect/BT, and resumes after… that's just amazing and outweighs the random connection drops.

I just need to restart the service from webgui or reboot the pi once or twice a week when it stops working.
Reply
#25
The librespot project is actively being worked on so eventually the bug will be fixed which is why at the moment I'm not planning to invest time to code some sort of watchdog for it into moOde.

But if someone wants to give it a try then here is some useful information.

librespot is started by moOde in /var/www/inc/playerlib.php function startSpotify(). This function reads the values in the sql table cfg_spotify and then composes the command string. A "restart" script would need to be written in PHP and include playerlib.php. Then it just calls startSpotify().

The watchdog part would be added to the existing /var/www/command/watchdog.sh script. The code block would be similar to the MPD restart block.
Reply
#26
(12-06-2018, 02:18 PM)Tim Curtis Wrote: The librespot project is actively being worked on so eventually the bug will be fixed which is why at the moment I'm not planning to invest time to code some sort of watchdog for it into moOde.

But if someone wants to give it a try then here is some useful information.

librespot is started by moOde in /var/www/inc/playerlib.php function startSpotify(). This function reads the values in the sql table cfg_spotify and then composes the command string. A "restart" script would need to be written in PHP and include playerlib.php. Then it just calls startSpotify().

The watchdog part would be added to the existing /var/www/command/watchdog.sh script. The code block would be similar to the MPD restart block.

Thanks for this! Combining the information in here ^^ and in some other thread, I've got myself a nice watchdog/restart for the librespot issues. You can use this procedure:

1. login as user pi

2. Create a file in /home/pi called restartspotify.php

Code:
sudo nano /home/pi/restartspotify.php

with this contents:

PHP Code:
#!/usr/bin/php
<?php


set_include_path
('/var/www/inc');
require_once 
'playerlib.php';

playerSession('open''' ,'');
session_write_close();

sysCmd('killall librespot');
sysCmd('/var/www/vol.sh -restore'); // r44d
// reset to inactive
playerSession('write''spotactive''0');
$GLOBALS['spotactive'] = '0';
sendEngCmd('spotactive0');
startSpotify();

?>


3. set the script as executable:

Code:
sudo chmod +x /home/pi/restartspotify.php

4. replace the already existing file /var/www/command/watchdog.sh

Code:
sudo nano /var/www/command/watchdog.sh

with this version (which adds a check for the existence of the librespot process and calls the script above to start it again)

PHP Code:
#!/bin/bash
#
# moOde audio player (C) 2014 Tim Curtis
# http://moodeaudio.org
#
# tsunamp player ui (C) 2013 Andrea Coiutti & Simone De Gregori
# http://www.tsunamp.com
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 2018-01-26 TC moOde 4.0
#

FPMLIMIT=40
FPMCNT
=$(pgrep --"php-fpm: pool www")
MPDACTIVE=$(pgrep --x mpd)
LIBRESPOTACTIVE=$(pgrep --x librespot)

while 
true; do
        # PHP
        if (( FPMCNT FPMLIMIT )); then
                TIMESTAMP
=$(date +'%Y%m%d %H%M%S')
                LOGMSG=" watchdog: PHP restart (fpm child limit > "$FPMLIMIT")"
                echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
                systemctl restart php7.0
-fpm
        fi

        
# MPD
        if [[ $MPDACTIVE ]]; then
                TIMESTAMP
=$(date +'%Y%m%d %H%M%S')
                LOGMSG=" watchdog: MPD restart (check syslog for MPD errors)"
                echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
                systemctl start mpd
        fi

        
#LIBRESPOT

        if [[ $LIBRESPOTACTIVE ]]; then
                TIMESTAMP
=$(date +'%Y%m%d %H%M%S')
                LOGMSG=" watchdog: LIBRESPOT restart (check syslog for MPD errors)"
                echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
                
/home/pi/restartspotify.php
        fi


        sleep 6
        FPMCNT
=$(pgrep --"php-fpm: pool www")
        MPDACTIVE=$(pgrep --x mpd)
        LIBRESPOTACTIVE=$(pgrep --x librespot)

done > /dev/null 2>&
 

(for reference, compared with the original I have added these lines: 28, 47-54, 60)
 

5. Reboot and that's it!

(note: so far I only tested it by killall librespot and the process was respawned few seconds after - the watchdog checks every 6 seconds).
Reply
#27
(03-22-2019, 08:11 PM)adixor Wrote:
(12-06-2018, 02:18 PM)Tim Curtis Wrote: The librespot project is actively being worked on so eventually the bug will be fixed which is why at the moment I'm not planning to invest time to code some sort of watchdog for it into moOde.

But if someone wants to give it a try then here is some useful information.

librespot is started by moOde in /var/www/inc/playerlib.php function startSpotify(). This function reads the values in the sql table cfg_spotify and then composes the command string. A "restart" script would need to be written in PHP and include playerlib.php. Then it just calls startSpotify().

The watchdog part would be added to the existing /var/www/command/watchdog.sh script. The code block would be similar to the MPD restart block.

Thanks for this! Combining the information in here ^^ and in some other thread, I've got myself a nice watchdog/restart for the librespot issues. You can use this procedure:

1. login as user pi

2. Create a file in /home/pi called restartspotify.php

Code:
sudo nano /home/pi/restartspotify.php

with this contents:

PHP Code:
#!/usr/bin/php
<?php


set_include_path
('/var/www/inc');
require_once 
'playerlib.php';

playerSession('open''' ,'');
session_write_close();

sysCmd('killall librespot');
sysCmd('/var/www/vol.sh -restore'); // r44d
// reset to inactive
playerSession('write''spotactive''0');
$GLOBALS['spotactive'] = '0';
sendEngCmd('spotactive0');
startSpotify();

?>


3. set the script as executable:

Code:
sudo chmod +x /home/pi/restartspotify.php

4. replace the already existing file /var/www/command/watchdog.sh

Code:
sudo nano /var/www/command/watchdog.sh

with this version (which adds a check for the existence of the librespot process and calls the script above to start it again)

PHP Code:
#!/bin/bash
#
# moOde audio player (C) 2014 Tim Curtis
# http://moodeaudio.org
#
# tsunamp player ui (C) 2013 Andrea Coiutti & Simone De Gregori
# http://www.tsunamp.com
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 2018-01-26 TC moOde 4.0
#

FPMLIMIT=40
FPMCNT
=$(pgrep --"php-fpm: pool www")
MPDACTIVE=$(pgrep --x mpd)
LIBRESPOTACTIVE=$(pgrep --x librespot)

while 
true; do
        # PHP
        if (( FPMCNT FPMLIMIT )); then
                TIMESTAMP
=$(date +'%Y%m%d %H%M%S')
                LOGMSG=" watchdog: PHP restart (fpm child limit > "$FPMLIMIT")"
                echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
                systemctl restart php7.0
-fpm
        fi

        
# MPD
        if [[ $MPDACTIVE ]]; then
                TIMESTAMP
=$(date +'%Y%m%d %H%M%S')
                LOGMSG=" watchdog: MPD restart (check syslog for MPD errors)"
                echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
                systemctl start mpd
        fi

        
#LIBRESPOT

        if [[ $LIBRESPOTACTIVE ]]; then
                TIMESTAMP
=$(date +'%Y%m%d %H%M%S')
                LOGMSG=" watchdog: LIBRESPOT restart (check syslog for MPD errors)"
                echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
                
/home/pi/restartspotify.php
        fi


        sleep 6
        FPMCNT
=$(pgrep --"php-fpm: pool www")
        MPDACTIVE=$(pgrep --x mpd)
        LIBRESPOTACTIVE=$(pgrep --x librespot)

done > /dev/null 2>&
 

(for reference, compared with the original I have added these lines: 28, 47-54, 60)
 

5. Reboot and that's it!

(note: so far I only tested it by killall librespot and the process was respawned few seconds after - the watchdog checks every 6 seconds).

Thank you for this!

Big Grin
Reply
#28
I'm curious, whats your guess as to how often this failure mode happens with librespot?

it would be trivial to integrate @adixor's nice code into moOde 5.
Reply
#29
(03-31-2019, 06:34 PM)Tim Curtis Wrote: I'm curious, whats your guess as to how often this failure mode happens with librespot?

it would be trivial to integrate @adixor's nice code into moOde 5.

Hi Tim,

How often? A really good question. I have 2 almost identical rpi3s, running on 2 different networks / ISP. Librespot fails whenever the direct link to Spotify servers is lost.
The failures happens between once a day (usually at night - probably during network maintenance) and sometimes only every second week. I think the longest it have been running is between 2 and 3 weeks without any hickups.
Reply
#30
Hi,

I see. That sounds like a bug in librespot. It shouldn't crash or exit due to interruption in connection, rather it should print a message and periodically try to reconnect.

I can compensate for that by implementing the watchdog code from earlier post but it needs to be modified to work correctly. See below.

/var/www/command/restart-spot.php
Code:
#!/usr/bin/php
<?php
/**
* moOde audio player (C) 2014 Tim Curtis
* http://moodeaudio.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* 2019-MM-DD TC moOde 5.0
*
*/

set_include_path('/var/www/inc');
require_once 'playerlib.php';

// clear active state
session_id(playerSession('getsessionid'));
session_start();
playerSession('write', 'spotactive', '0');
session_write_close();

// dismiss active screen
sendEngCmd('spotactive0');
$GLOBALS['spotactive'] = '0';

// restore MPD volume and start librespot
sysCmd('/var/www/vol.sh -restore');
startSpotify();

/var/www/command/watchdog.sh
Code:
#!/bin/bash
#
# moOde audio player (C) 2014 Tim Curtis
# http://moodeaudio.org
#
# tsunamp player ui (C) 2013 Andrea Coiutti & Simone De Gregori
# http://www.tsunamp.com
#
# This Program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This Program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 2019-MM-DD TC moOde 5.0
#

FPMLIMIT=40
FPMCNT=$(pgrep -c -f "php-fpm: pool www")
MPDACTIVE=$(pgrep -c -x mpd)
SPOTACTIVE=$(pgrep -c -x librespot)
SQLDB=/var/local/www/db/moode-sqlite3.db

while true; do

    # PHP
    if (( FPMCNT > FPMLIMIT )); then
        TIMESTAMP=$(date +'%Y%m%d %H%M%S')
        LOGMSG=" watchdog: PHP restarted (fpm child limit > "$FPMLIMIT")"
        echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
        systemctl restart php7.0-fpm
    fi

    # MPD
    if [[ $MPDACTIVE = 0 ]]; then
        TIMESTAMP=$(date +'%Y%m%d %H%M%S')
        LOGMSG=" watchdog: MPD restarted (check syslog for errors)"
        echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
        systemctl start mpd
    fi

    # LIBRESPOT
    RESULT=$(sqlite3 $SQLDB "select value from cfg_system where param='spotifysvc'")
    if [[ $RESULT = "1" ]]; then
        if [[ $SPOTACTIVE = 0 ]]; then
            TIMESTAMP=$(date +'%Y%m%d %H%M%S')
            LOGMSG=" watchdog: LIBRESPOT restarted (check syslog for errors)"
            echo $TIMESTAMP$LOGMSG >> /var/log/moode.log
            /var/www/command/restart-spot.php
        fi
    fi
        
    sleep 6
    FPMCNT=$(pgrep -c -f "php-fpm: pool www")
    MPDACTIVE=$(pgrep -c -x mpd)
    SPOTACTIVE=$(pgrep -c -x librespot)

done > /dev/null 2>&1 &

-Tim
Reply


Forum Jump: