Ignore thumbnail progress in cache (fixes #270)

This commit is contained in:
Alex Ling 2022-01-26 09:10:47 +00:00
parent fea6c04c4f
commit 9b111b0ee8
2 changed files with 38 additions and 17 deletions

View File

@ -1,9 +1,38 @@
class Library class Library
struct ThumbnailContext
property current : Int32, total : Int32
def initialize
@current = 0
@total = 0
end
def progress
if total == 0
0
else
current / total
end
end
def reset
@current = 0
@total = 0
end
def increment
@current += 1
end
end
include YAML::Serializable include YAML::Serializable
getter dir : String, title_ids : Array(String), getter dir : String, title_ids : Array(String),
title_hash : Hash(String, Title) title_hash : Hash(String, Title)
@[YAML::Field(ignore: true)]
getter thumbnail_ctx = ThumbnailContext.new
use_default use_default
def save_instance def save_instance
@ -55,9 +84,6 @@ class Library
@title_ids = [] of String @title_ids = [] of String
@title_hash = {} of String => Title @title_hash = {} of String => Title
@entries_count = 0
@thumbnails_count = 0
register_jobs register_jobs
end end
@ -278,34 +304,29 @@ class Library
.shuffle! .shuffle!
end end
def thumbnail_generation_progress
return 0 if @entries_count == 0
@thumbnails_count / @entries_count
end
def generate_thumbnails def generate_thumbnails
if @thumbnails_count > 0 if thumbnail_ctx.current > 0
Logger.debug "Thumbnail generation in progress" Logger.debug "Thumbnail generation in progress"
return return
end end
Logger.info "Starting thumbnail generation" Logger.info "Starting thumbnail generation"
entries = deep_titles.flat_map(&.deep_entries).reject &.err_msg entries = deep_titles.flat_map(&.deep_entries).reject &.err_msg
@entries_count = entries.size thumbnail_ctx.total = entries.size
@thumbnails_count = 0 thumbnail_ctx.current = 0
# Report generation progress regularly # Report generation progress regularly
spawn do spawn do
loop do loop do
unless @thumbnails_count == 0 unless thumbnail_ctx.current == 0
Logger.debug "Thumbnail generation progress: " \ Logger.debug "Thumbnail generation progress: " \
"#{(thumbnail_generation_progress * 100).round 1}%" "#{(thumbnail_ctx.progress * 100).round 1}%"
end end
# Generation is completed. We reset the count to 0 to allow subsequent # Generation is completed. We reset the count to 0 to allow subsequent
# calls to the function, and break from the loop to stop the progress # calls to the function, and break from the loop to stop the progress
# report fiber # report fiber
if thumbnail_generation_progress.to_i == 1 if thumbnail_ctx.progress.to_i == 1
@thumbnails_count = 0 thumbnail_ctx.reset
break break
end end
sleep 10.seconds sleep 10.seconds
@ -319,7 +340,7 @@ class Library
# and CPU # and CPU
sleep 1.seconds sleep 1.seconds
end end
@thumbnails_count += 1 thumbnail_ctx.increment
end end
Logger.info "Thumbnail generation finished" Logger.info "Thumbnail generation finished"
end end

View File

@ -240,7 +240,7 @@ struct APIRouter
} }
get "/api/admin/thumbnail_progress" do |env| get "/api/admin/thumbnail_progress" do |env|
send_json env, { send_json env, {
"progress" => Library.default.thumbnail_generation_progress, "progress" => Library.default.thumbnail_ctx.progress,
}.to_json }.to_json
end end