Compare with DB when loading library cache (fixes #256)

This commit is contained in:
Alex Ling 2022-01-24 14:18:52 +00:00
parent 750fbbb8fe
commit 77df418390
3 changed files with 33 additions and 8 deletions

View File

@ -25,20 +25,22 @@ class Library
begin begin
Compress::Gzip::Reader.open path do |content| Compress::Gzip::Reader.open path do |content|
loaded = Library.from_yaml 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 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 " \ Logger.fatal "Cached library dir #{loaded.dir} does not match " \
"current library dir #{Config.current.library_path}. " \ "current library dir #{Config.current.library_path}. " \
"Deleting cache" "Deleting cache"
File.delete path delete_cache_and_exit path
Logger.fatal "Invalid library cache deleted. Mango needs to " \ end
"perform a full reset to recover from this. " \ if loaded.title_ids.size > 0 &&
"Pleae restart Mango." Storage.default.count_titles == 0
Logger.fatal "Exiting" Logger.fatal "The library cache is inconsistent with the DB. " \
exit 1 "Deleting cache"
delete_cache_and_exit path
end end
@@default = loaded @@default = loaded
Logger.debug "Library cache loaded"
end end
Library.default.register_jobs Library.default.register_jobs
rescue e rescue e

View File

@ -619,6 +619,20 @@ class Storage
{token, expires} {token, expires}
end 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 def close
MainFiber.run do MainFiber.run do
unless @db.nil? unless @db.nil?

View File

@ -163,3 +163,12 @@ def sanitize_filename(str : String) : String
.gsub(/[\177\000-\031\\:\*\?\"<>\|]/, "") .gsub(/[\177\000-\031\\:\*\?\"<>\|]/, "")
sanitized.size > 0 ? sanitized : random_str sanitized.size > 0 ? sanitized : random_str
end 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