From 09c0ae2551b3ebb2639c773f0af114f7e43dcef4 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 30 Dec 2013 01:31:00 +0100 Subject: [PATCH] local: Add flush threshold to scanner. Instead of triggering every 1000th query, this is now configurable and also triggers the flush call to the library. --- docs/ext/local.rst | 5 +++++ mopidy/backends/local/__init__.py | 1 + mopidy/backends/local/commands.py | 33 +++++++++++-------------------- mopidy/backends/local/ext.conf | 1 + 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/docs/ext/local.rst b/docs/ext/local.rst index 5cf4b2d3..4484ab0d 100644 --- a/docs/ext/local.rst +++ b/docs/ext/local.rst @@ -58,6 +58,11 @@ Configuration values Number of milliseconds before giving up scanning a file and moving on to the next file. +.. confval:: local/scan_flush_threshold + + Number of tracks to wait before telling library it should try and store + it's progress so far. Some libraries might not respect this setting. + .. confval:: local/excluded_file_extensions File extensions to exclude when scanning the media directory. Values diff --git a/mopidy/backends/local/__init__.py b/mopidy/backends/local/__init__.py index 4031c474..6697d91d 100644 --- a/mopidy/backends/local/__init__.py +++ b/mopidy/backends/local/__init__.py @@ -28,6 +28,7 @@ class Extension(ext.Extension): schema['tag_cache_file'] = config.Deprecated() schema['scan_timeout'] = config.Integer( minimum=1000, maximum=1000*60*60) + schema['scan_flush_threshold'] = config.Integer(minimum=0) schema['excluded_file_extensions'] = config.List(optional=True) return schema diff --git a/mopidy/backends/local/commands.py b/mopidy/backends/local/commands.py index a2098078..65bfd274 100644 --- a/mopidy/backends/local/commands.py +++ b/mopidy/backends/local/commands.py @@ -25,6 +25,7 @@ class ScanCommand(commands.Command): def run(self, args, config): media_dir = config['local']['media_dir'] scan_timeout = config['local']['scan_timeout'] + flush_threshold = config['local']['scan_flush_threshold'] excluded_file_extensions = config['local']['excluded_file_extensions'] excluded_file_extensions = set( file_ext.lower() for file_ext in excluded_file_extensions) @@ -80,7 +81,9 @@ class ScanCommand(commands.Command): logger.info('Scanning...') scanner = scan.Scanner(scan_timeout) - progress = Progress(len(uris_to_update)) + count = 0 + total = len(uris_to_update) + start = time.time() for uri in sorted(uris_to_update): try: @@ -91,26 +94,14 @@ class ScanCommand(commands.Command): except exceptions.ScannerError as error: logger.warning('Failed %s: %s', uri, error) - # TODO: trigger this on batch size intervals instead and add - # flush - progress.increment() + count += 1 + if count % flush_threshold == 0 or count == total: + duration = time.time() - start + remainder = duration / count * (total - count) + logger.info('Scanned %d of %d files in %ds, ~%ds left.', + count, total, duration, remainder) + library.flush() - logger.info('Commiting changes.') library.close() + logger.info('Done scanning.') return 0 - - -# TODO: move to utils? -class Progress(object): - def __init__(self, total): - self.count = 0 - self.total = total - self.start = time.time() - - def increment(self): - self.count += 1 - if self.count % 1000 == 0 or self.count == self.total: - duration = time.time() - self.start - remainder = duration / self.count * (self.total - self.count) - logger.info('Scanned %d of %d files in %ds, ~%ds left.', - self.count, self.total, duration, remainder) diff --git a/mopidy/backends/local/ext.conf b/mopidy/backends/local/ext.conf index 5f83db05..8f1e860c 100644 --- a/mopidy/backends/local/ext.conf +++ b/mopidy/backends/local/ext.conf @@ -5,6 +5,7 @@ media_dir = $XDG_MUSIC_DIR data_dir = $XDG_DATA_DIR/mopidy/local playlists_dir = $XDG_DATA_DIR/mopidy/local/playlists scan_timeout = 1000 +scan_flush_threshold = 1000 excluded_file_extensions = .html .jpeg