Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Async refresh
Browse files Browse the repository at this point in the history
Move refresh of google music content to an extra thread.
This fixes two problems:

1. Very long startup times on small systems, like the raspberry pi
2. Remote changes to the content, like new/changed playlists
  • Loading branch information
felixb committed Jul 30, 2014
1 parent d4e13c9 commit 9406ef7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions mopidy_gmusic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def get_config_schema(self):
schema['password'] = config.Secret()
schema['deviceid'] = config.String(optional=True)
schema['all_access'] = config.Boolean(optional=True)
schema['refresh_playlists'] = config.Integer(minimum=-1, optional=True)
return schema

def setup(self, registry):
Expand Down
32 changes: 30 additions & 2 deletions mopidy_gmusic/actor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from __future__ import unicode_literals

import logging
import time

from threading import Timer

from mopidy import backend

import pykka
Expand All @@ -9,11 +14,14 @@
from .playlists import GMusicPlaylistsProvider
from .session import GMusicSession

logger = logging.getLogger(__name__)


class GMusicBackend(pykka.ThreadingActor, backend.Backend):
def __init__(self, config, audio):
super(GMusicBackend, self).__init__()

self._refresh_timer = None
self.config = config

self.library = GMusicLibraryProvider(backend=self)
Expand All @@ -28,8 +36,28 @@ def on_start(self):
self.config['gmusic']['password'],
self.config['gmusic']['deviceid'])
self.library.set_all_access(self.config['gmusic']['all_access'])
self.library.refresh()
self.playlists.refresh()
# wait a few seconds to let mopidy settle
# then refresh google music content asynchronously
self._sched_refresh(5.0)

def on_stop(self):
if self._refresh_timer:
self._refresh_timer.cancel()
self._refresh_timer = None
self.session.logout()

def _refresh_content(self):
logger.debug('Start refreshing gmusic content')
t0 = round(time.time())
self.library.refresh()
self.playlists.refresh()
# schedule next refresh
refresh_playlists = self.config['gmusic']['refresh_playlists']
if refresh_playlists > 0:
self._sched_refresh(refresh_playlists * 60.0)
t1 = round(time.time())
logger.debug('Finished refreshing gmusic content, took %ds', t1 - t0)

def _sched_refresh(self, seconds):
self._refresh_timer = Timer(seconds, self._refresh_content)
self._refresh_timer.start()
1 change: 1 addition & 0 deletions mopidy_gmusic/ext.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ username =
password =
deviceid =
all_access = false
refresh_playlists = 60

0 comments on commit 9406ef7

Please sign in to comment.