Cancel a download job when deleted from web UI

This commit is contained in:
Alex Ling 2020-12-12 16:15:16 +00:00
parent 1cd90926df
commit 9dcc9665ce
3 changed files with 34 additions and 0 deletions

View File

@ -91,6 +91,7 @@ module MangaDex
end
channel.send page_job
break unless @queue.exists? job
end
end
@ -98,6 +99,9 @@ module MangaDex
page_jobs = [] of PageJob
chapter.pages.size.times do
page_job = channel.receive
break unless @queue.exists? job
Logger.debug "[#{page_job.success ? "success" : "failed"}] " \
"#{page_job.url}"
page_jobs << page_job
@ -110,6 +114,13 @@ module MangaDex
Logger.error msg
end
end
unless @queue.exists? job
Logger.debug "Download cancelled"
@downloading = false
next
end
fail_count = page_jobs.count { |j| !j.success }
Logger.debug "Download completed. " \
"#{fail_count}/#{page_jobs.size} failed"

View File

@ -66,6 +66,8 @@ class Plugin
fail_count = 0
while page = plugin.next_page
break unless @queue.exists? job
fn = process_filename page["filename"].as_s
url = page["url"].as_s
headers = HTTP::Headers.new
@ -109,6 +111,12 @@ class Plugin
end
end
unless @queue.exists? job
Logger.debug "Download cancelled"
@downloading = false
return
end
Logger.debug "Download completed. #{fail_count}/#{pages} failed"
writer.close
filename = File.join File.dirname(zip_path), File.basename(zip_path,

View File

@ -196,6 +196,21 @@ class Queue
self.delete job.id
end
def exists?(id : String)
res = false
MainFiber.run do
DB.open "sqlite3://#{@path}" do |db|
res = db.query_one "select count(*) from queue where id = (?)", id,
as: Bool
end
end
res
end
def exists?(job : Job)
self.exists? job.id
end
def delete_status(status : JobStatus)
MainFiber.run do
DB.open "sqlite3://#{@path}" do |db|