bentglasstube / Net-MPD

5 stars 1 forks source link

connection is not reconnecting on idle #7

Open carnager opened 7 years ago

carnager commented 7 years ago
#!/usr/bin/env perl
#
use Net::MPD;
my $mpd = Net::MPD->connect($ENV{MPD_HOST} // $mpd_host // 'localhost');
sleep 121;
$mpd->add('flac/Twisted Roots/1993 Turn to Stone/06-Trax.flac')

result:

perl foo.pl
Error reading line from socket at foo.pl line 9.

Documentation says:

close

close the connection. This is pretty worthless as the library will just reconnect for the next command.

So I tried to add a $mpd->close(); line before the sleep, which gives a reading socket error on the close command, which ends the script instantly without ever going to the sleep.

Then I put the close command into a try block. Which resulted in the same behavior as the original script above.

Finally I closed the socket itself after the close command. The final script looks like this now:

#!/usr/bin/env perl
#
use Net::MPD;
use Try::Tiny;

my $mpd = Net::MPD->connect($ENV{MPD_HOST} // $mpd_host // 'localhost');
try { $mpd->close(); };
$mpd->{socket}->close;
sleep 121;
$mpd->add('flac/Twisted Roots/1993 Turn to Stone/06-Trax.flac');

Which finally had the desired result.

So actually there are 2 bugs here:

  1. $mpd->close(); does not work without a try block and bails out.
  2. $mpd->close(); does not close the actual socket.

Also I think that both above issues should be done automatically without the user having to close down the socket manually before idle times.

bentglasstube commented 7 years ago

I agree this doesn't work too well currently. I think if you try again after the failed connection it will reconnect but it's not a great workflow at present.

-- Alan Berndt

On Aug 12, 2017 05:49, "Rasmus Steinke" notifications@github.com wrote:

!/usr/bin/env perl

# use Net::MPD;

my $mpd = Net::MPD->connect($ENV{MPD_HOST} // $mpd_host // 'localhost');

sleep 121;

$mpd->add('flac/Twisted Roots/1993 Turn to Stone/06-Trax.flac')

result:

perl foo.pl Error reading line from socket at foo.pl line 9.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/bentglasstube/Net-MPD/issues/7, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbaJc5JhofckrOl1njRGs4YwJNNWrqoks5sXZ9NgaJpZM4O1Z_e .