diff --git a/src/library/library.cr b/src/library/library.cr index bd801ac..7c9c020 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -25,20 +25,22 @@ class Library begin Compress::Gzip::Reader.open path do |content| loaded = Library.from_yaml content + # We will have to do a full restart in these cases. Otherwise having + # two instances of the library will cause some weirdness. if loaded.dir != Config.current.library_path - # We will have to do a full restart in this case. Otherwise having - # two instances of the library will cause some weirdness. Logger.fatal "Cached library dir #{loaded.dir} does not match " \ "current library dir #{Config.current.library_path}. " \ "Deleting cache" - File.delete path - Logger.fatal "Invalid library cache deleted. Mango needs to " \ - "perform a full reset to recover from this. " \ - "Pleae restart Mango." - Logger.fatal "Exiting" - exit 1 + delete_cache_and_exit path + end + if loaded.title_ids.size > 0 && + Storage.default.count_titles == 0 + Logger.fatal "The library cache is inconsistent with the DB. " \ + "Deleting cache" + delete_cache_and_exit path end @@default = loaded + Logger.debug "Library cache loaded" end Library.default.register_jobs rescue e diff --git a/src/storage.cr b/src/storage.cr index 4c8cfe7..5622241 100644 --- a/src/storage.cr +++ b/src/storage.cr @@ -619,6 +619,20 @@ class Storage {token, expires} end + def count_titles : Int32 + count = 0 + MainFiber.run do + get_db do |db| + db.query "select count(*) from titles" do |rs| + rs.each do + count = rs.read Int32 + end + end + end + end + count + end + def close MainFiber.run do unless @db.nil? diff --git a/src/util/util.cr b/src/util/util.cr index 11d1a13..e7b1b1a 100644 --- a/src/util/util.cr +++ b/src/util/util.cr @@ -163,3 +163,12 @@ def sanitize_filename(str : String) : String .gsub(/[\177\000-\031\\:\*\?\"<>\|]/, "") sanitized.size > 0 ? sanitized : random_str end + +def delete_cache_and_exit(path : String) + File.delete path + Logger.fatal "Invalid library cache deleted. Mango needs to " \ + "perform a full reset to recover from this. " \ + "Pleae restart Mango. This is NOT a bug." + Logger.fatal "Exiting" + exit 1 +end