diff --git a/src/calibre/library/caches.py b/src/calibre/library/caches.py index 0b5a922209..714579ec77 100644 --- a/src/calibre/library/caches.py +++ b/src/calibre/library/caches.py @@ -36,14 +36,14 @@ class MetadataBackup(Thread): # {{{ def run(self): while self.keep_running: try: - id_ = self.db.dirtied_queue.get(True, 5) + id_ = self.db.dirtied_queue.get() except Empty: continue except: # Happens during interpreter shutdown break if self.dump_func([id_]) is None: - # An exception occured in dump_func, retry once + # An exception occurred in dump_func, retry once prints('Failed to backup metadata for id:', id_, 'once') time.sleep(2) if not self.dump_func([id_]): @@ -84,9 +84,12 @@ class CoverCache(Thread): # {{{ def run(self): while self.keep_running: try: - id_ = self.load_queue.get(True, 1) + id_ = self.load_queue.get() except Empty: continue + except: + #Happens during interpreter shutdown + break try: img = self._image_for_id(id_) except: diff --git a/src/calibre/library/cli.py b/src/calibre/library/cli.py index cd4e472807..6ff17b0781 100644 --- a/src/calibre/library/cli.py +++ b/src/calibre/library/cli.py @@ -32,8 +32,9 @@ def send_message(msg=''): t.conn.send('refreshdb:'+msg) t.conn.close() - - +def write_dirtied(db): + prints('Backing up metadata') + db.dump_metadata() def get_parser(usage): parser = OptionParser(usage) @@ -259,6 +260,7 @@ def do_add(db, paths, one_book_per_directory, recurse, add_duplicates): print >>sys.stderr, '\t', title+':' print >>sys.stderr, '\t\t ', path + write_dirtied(db) send_message() finally: sys.stdout = orig @@ -299,6 +301,7 @@ def do_add_empty(db, title, authors, isbn): if isbn: mi.isbn = isbn db.import_book(mi, []) + write_dirtied() send_message() def command_add(args, dbpath): @@ -452,6 +455,7 @@ def do_set_metadata(db, id, stream): db.set_metadata(id, mi) db.clean() do_show_metadata(db, id, False) + write_dirtied() send_message() def set_metadata_option_parser(): diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index dc320eb011..bdaa643d83 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -557,7 +557,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): def metadata_for_field(self, key): return self.field_metadata[key] - def dump_metadata(self, book_ids, remove_from_dirtied=True, commit=True): + def dump_metadata(self, book_ids=None, remove_from_dirtied=True, commit=True): + 'Write metadata for each record to an individual OPF file' + if book_ids is None: + book_ids = [x[0] for x in self.conn.get( + 'SELECT book FROM metadata_dirtied', all=True)] for book_id in book_ids: if not self.data.has_id(book_id): continue