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}`);
});
};
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 = () => {
if (loading) return;
loading = true;
@ -74,7 +92,10 @@ const load = () => {
})
.done(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 ';
if (obj.status === 'Completed')
cls += 'uk-label-success';

View File

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

View File

@ -129,7 +129,10 @@ class APIRouter < Router
get "/api/admin/mangadex/queue" do |env|
jobs = @context.queue.get_all
send_json env, jobs.to_json
send_json env, {
"jobs" => jobs,
"paused" => @context.queue.paused?
}.to_json
end
post "/api/admin/mangadex/queue/delete/:id" do |env|
@ -145,19 +148,6 @@ class APIRouter < Router
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|
begin
id = env.params.url["id"]
@ -171,9 +161,22 @@ class APIRouter < Router
end
end
post "/api/admin/mangadex/queue/retry" do |env|
post "/api/admin/mangadex/queue/:action" do |env|
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
rescue e
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="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="toggle()" id="pause-resume-btn" hidden></button>
</div>
<div id="config" class="uk-margin">
<label><input id="auto-refresh" class="uk-checkbox" type="checkbox" checked> Auto Refresh</label>