From bea8556448ebaf7f54ebd268639caa7492dd0a85 Mon Sep 17 00:00:00 2001 From: kaliko Date: Thu, 30 Sep 2021 19:15:10 +0200 Subject: [PATCH] Deal with lost connection (Closes #53) --- doc/Changelog | 6 ++++-- setup.py | 2 +- sima/core.py | 10 ++++++---- sima/mpdclient.py | 24 ++++++++++++++++++------ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index 5bc357e..2639ee6 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -7,8 +7,10 @@ MPD_sima v0.18.0.dev1 simadb_cli executable is removed. * Removed obsolete --create-db and --generate-config options * Fixed crash when setting XDG_CONFIG_HOME (closes #50) - * Add abstract unix socket support for MPD connection (need at least - python-musicpd 0.6.0) + * Add abstract unix socket support for MPD connection + * Better MPD connection management. + * Need at least python-musicpd 0.7.0. + Abstract socket in v0.6.0 and socket timeout in v0.7.0 -- kaliko Wed, 28 Apr 2021 17:21:39 +0200 diff --git a/setup.py b/setup.py index d46a831..8c86cc2 100755 --- a/setup.py +++ b/setup.py @@ -42,7 +42,7 @@ setup(name='MPD_sima', keywords='MPD', long_description=DESCRIPTION, classifiers=classifiers, - install_requires=['python-musicpd>=0.4.1', 'requests>= 2.20.0'], + install_requires=['python-musicpd>=0.7.0', 'requests>= 2.20.0'], packages=find_packages(exclude=["tests"]), include_package_data=True, data_files=data_files, diff --git a/sima/core.py b/sima/core.py index 7f3631c..ed4e0e1 100644 --- a/sima/core.py +++ b/sima/core.py @@ -145,10 +145,12 @@ class Sima(Daemon): """ self.log.warning('Starting shutdown.') # Cleaning pending command - self.player.clean() - self.foreach_plugin('shutdown') - self.player.disconnect() - + try: + self.player.clean() + self.foreach_plugin('shutdown') + self.player.disconnect() + except PlayerError as err: + self.log.error('Player error during shutdown: %s', err) self.log.info('The way is shut, it was made by those who are dead. ' 'And the dead keep it…') self.log.info('bye...') diff --git a/sima/mpdclient.py b/sima/mpdclient.py index ebf39a7..d528b96 100644 --- a/sima/mpdclient.py +++ b/sima/mpdclient.py @@ -20,6 +20,7 @@ from difflib import get_close_matches from functools import wraps from logging import getLogger +from select import select # external module from musicpd import MPDClient, MPDError as PlayerError @@ -106,6 +107,7 @@ class MPD(MPDClient): def __init__(self, config): super().__init__() + self.socket_timeout = 10 self.use_mbid = True self.log = getLogger('sima') self.config = config @@ -229,12 +231,22 @@ class MPD(MPDClient): * skipped current track skipped """ curr = self.current - ret = self.idle('database', 'playlist', 'player', 'options') - if self._skipped_track(curr): - ret.append('skipped') - if 'database' in ret: - self._reset_cache() - return ret + select_timeout = 5 + while True: + self.send_idle('database', 'playlist', 'player', 'options') + _read, _, _ = select([self], [], [], select_timeout) + if _read: # tries to read response + ret = self.fetch_idle() + if self._skipped_track(curr): + ret.append('skipped') + if 'database' in ret: + self._reset_cache() + return ret + else: + try: # noidle cmd does not go through __getattr__, need to catch OSError then + self.noidle() + except OSError as err: + raise PlayerError(err) def clean(self): """Clean blocking event (idle) and pending commands -- 2.39.5