Add pause/resume download button to the download manager

This commit is contained in:
Alex Ling 2020-03-02 16:30:05 +00:00
parent fecb96c91b
commit 30af64e9ca
4 changed files with 63 additions and 29 deletions

View File

@ -63,6 +63,24 @@ const refresh = (id) => {
alert('danger', `Failed to restart download job. Error: [${jqXHR.status}] ${jqXHR.statusText}`); alert('danger', `Failed to restart download job. Error: [${jqXHR.status}] ${jqXHR.statusText}`);
}); });
}; };
const toggle = () => {
$('#pause-resume-btn').attr('disabled', '');
const paused = $('#pause-resume-btn').text() === 'Resume download';
const action = paused ? 'resume' : 'pause';
const url = `/api/admin/mangadex/queue/${action}`;
$.ajax({
type: 'POST',
url: url,
dataType: 'json'
})
.fail((jqXHR, status) => {
alert('danger', `Failed to ${action} download queue. Error: [${jqXHR.status}] ${jqXHR.statusText}`);
})
.always(() => {
load();
$('#pause-resume-btn').removeAttr('disabled');
});
};
const load = () => { const load = () => {
if (loading) return; if (loading) return;
loading = true; loading = true;
@ -74,7 +92,10 @@ const load = () => {
}) })
.done(data => { .done(data => {
console.log(data); console.log(data);
const rows = data.map(obj => { const btnText = data.paused ? "Resume download" : "Pause download";
$('#pause-resume-btn').text(btnText);
$('#pause-resume-btn').removeAttr('hidden');
const rows = data.jobs.map(obj => {
var cls = 'uk-label '; var cls = 'uk-label ';
if (obj.status === 'Completed') if (obj.status === 'Completed')
cls += 'uk-label-success'; cls += 'uk-label-success';

View File

@ -80,7 +80,10 @@ module MangaDex
end end
end end
end end
class Queue class Queue
property downloader : Downloader?
def initialize(@path : String) def initialize(@path : String)
dir = File.dirname path dir = File.dirname path
unless Dir.exists? dir unless Dir.exists? dir
@ -243,13 +246,27 @@ module MangaDex
"\n", msg, job.id "\n", msg, job.id
end end
end end
def pause
@downloader.not_nil!.stopped = true
end
def resume
@downloader.not_nil!.stopped = false
end
def paused?
@downloader.not_nil!.stopped
end
end end
class Downloader class Downloader
@stopped = false property stopped = false
def initialize(@queue : Queue, @api : API, @library_path : String, def initialize(@queue : Queue, @api : API, @library_path : String,
@wait_seconds : Int32, @retries : Int32) @wait_seconds : Int32, @retries : Int32)
@queue.downloader = self
spawn do spawn do
loop do loop do
sleep 1.second sleep 1.second
@ -265,16 +282,8 @@ module MangaDex
end end
end end
def stop
@stopped = true
end
def resume
@stopped = false
end
private def download(job : Job) private def download(job : Job)
self.stop @stop = true
@queue.set_status JobStatus::Downloading, job @queue.set_status JobStatus::Downloading, job
begin begin
chapter = @api.get_chapter(job.id) chapter = @api.get_chapter(job.id)
@ -284,7 +293,7 @@ module MangaDex
unless e.message.nil? unless e.message.nil?
@queue.add_message e.message.not_nil!, job @queue.add_message e.message.not_nil!, job
end end
self.resume @stop = false
return return
end end
@queue.set_pages chapter.pages.size, job @queue.set_pages chapter.pages.size, job
@ -347,7 +356,7 @@ module MangaDex
else else
@queue.set_status JobStatus::MissingPages, job @queue.set_status JobStatus::MissingPages, job
end end
self.resume @stop = false
end end
end end

View File

@ -129,7 +129,10 @@ class APIRouter < Router
get "/api/admin/mangadex/queue" do |env| get "/api/admin/mangadex/queue" do |env|
jobs = @context.queue.get_all jobs = @context.queue.get_all
send_json env, jobs.to_json send_json env, {
"jobs" => jobs,
"paused" => @context.queue.paused?
}.to_json
end end
post "/api/admin/mangadex/queue/delete/:id" do |env| post "/api/admin/mangadex/queue/delete/:id" do |env|
@ -145,19 +148,6 @@ class APIRouter < Router
end end
end end
# Delete all completed tasks from the queue
post "/api/admin/mangadex/queue/delete" do |env|
begin
@context.queue.delete_status MangaDex::JobStatus::Completed
send_json env, {"success" => true}.to_json
rescue e
send_json env, {
"success" => false,
"error" => e.message
}.to_json
end
end
post "/api/admin/mangadex/queue/retry/:id" do |env| post "/api/admin/mangadex/queue/retry/:id" do |env|
begin begin
id = env.params.url["id"] id = env.params.url["id"]
@ -171,9 +161,22 @@ class APIRouter < Router
end end
end end
post "/api/admin/mangadex/queue/retry" do |env| post "/api/admin/mangadex/queue/:action" do |env|
begin begin
@context.queue.reset action = env.params.url["action"]
case action
when "delete"
@context.queue.delete_status MangaDex::JobStatus::Completed
when "retry"
@context.queue.reset
when "pause"
@context.queue.pause
when "resume"
@context.queue.resume
else
raise "Unknown queue action #{action}"
end
send_json env, {"success" => true}.to_json send_json env, {"success" => true}.to_json
rescue e rescue e
send_json env, { send_json env, {

View File

@ -3,6 +3,7 @@
<button class="uk-button uk-button-default" onclick="remove()">Delete Completed Tasks</button> <button class="uk-button uk-button-default" onclick="remove()">Delete Completed Tasks</button>
<button class="uk-button uk-button-default" onclick="refresh()">Retry Failed Tasks</button> <button class="uk-button uk-button-default" onclick="refresh()">Retry Failed Tasks</button>
<button class="uk-button uk-button-default" onclick="load()">Refresh Queue</button> <button class="uk-button uk-button-default" onclick="load()">Refresh Queue</button>
<button class="uk-button uk-button-default" onclick="toggle()" id="pause-resume-btn" hidden></button>
</div> </div>
<div id="config" class="uk-margin"> <div id="config" class="uk-margin">
<label><input id="auto-refresh" class="uk-checkbox" type="checkbox" checked> Auto Refresh</label> <label><input id="auto-refresh" class="uk-checkbox" type="checkbox" checked> Auto Refresh</label>