From 9dcc9665ceac7dc08ff3ba95b647d2d1a01e92d3 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Sat, 12 Dec 2020 16:15:16 +0000 Subject: [PATCH] Cancel a download job when deleted from web UI --- src/mangadex/downloader.cr | 11 +++++++++++ src/plugin/downloader.cr | 8 ++++++++ src/queue.cr | 15 +++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/mangadex/downloader.cr b/src/mangadex/downloader.cr index ff395d9..e2babb6 100644 --- a/src/mangadex/downloader.cr +++ b/src/mangadex/downloader.cr @@ -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" diff --git a/src/plugin/downloader.cr b/src/plugin/downloader.cr index 99cd52d..054698e 100644 --- a/src/plugin/downloader.cr +++ b/src/plugin/downloader.cr @@ -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, diff --git a/src/queue.cr b/src/queue.cr index 0281b61..c9f805c 100644 --- a/src/queue.cr +++ b/src/queue.cr @@ -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|