Move pop to the Downloader classes

This commit is contained in:
Alex Ling 2020-07-21 17:20:03 +00:00
parent 2a36804e8d
commit 92f5a90629
3 changed files with 35 additions and 19 deletions

View File

@ -18,7 +18,7 @@ module MangaDex
sleep 1.second sleep 1.second
next if @stopped || @downloading next if @stopped || @downloading
begin begin
job = @queue.pop job = pop
next if job.nil? next if job.nil?
download job download job
rescue e rescue e
@ -28,6 +28,21 @@ module MangaDex
end end
end end
def pop : Queue::Job?
job = nil
DB.open "sqlite3://#{@queue.path}" do |db|
begin
db.query_one "select * from queue where id not like '%-%' and " \
"(status = 0 or status = 1) order by time limit 1" \
do |res|
job = Queue::Job.from_query_result res
end
rescue
end
end
job
end
private def download(job : Queue::Job) private def download(job : Queue::Job)
@downloading = true @downloading = true
@queue.set_status Queue::JobStatus::Downloading, job @queue.set_status Queue::JobStatus::Downloading, job

View File

@ -5,5 +5,20 @@ class Plugin
def initialize def initialize
super super
end end
def pop : Queue::Job?
job = nil
DB.open "sqlite3://#{@queue.path}" do |db|
begin
db.query_one "select * from queue where id like '%-%' and " \
"(status = 0 or status = 1) order by time limit 1" \
do |res|
job = Queue::Job.from_query_result res
end
rescue
end
end
job
end
end end
end end

View File

@ -2,7 +2,7 @@ require "sqlite3"
require "./util/*" require "./util/*"
class Queue class Queue
class Downloader abstract class Downloader
property stopped = false property stopped = false
@library_path : String = Config.current.library_path @library_path : String = Config.current.library_path
@downloading = false @downloading = false
@ -11,6 +11,8 @@ class Queue
@queue = Queue.default @queue = Queue.default
@queue << self @queue << self
end end
abstract def pop : Job?
end end
class PageJob class PageJob
@ -88,7 +90,7 @@ class Queue
end end
end end
@path : String getter path : String
@downloaders = [] of Downloader @downloaders = [] of Downloader
@paused = false @paused = false
@ -122,22 +124,6 @@ class Queue
end end
end end
# Returns the earliest job in queue or nil if the job cannot be parsed.
# Returns nil if queue is empty
def pop
job = nil
DB.open "sqlite3://#{@path}" do |db|
begin
db.query_one "select * from queue where status = 0 " \
"or status = 1 order by time limit 1" do |res|
job = Job.from_query_result res
end
rescue
end
end
job
end
# Push an array of jobs into the queue, and return the number of jobs # Push an array of jobs into the queue, and return the number of jobs
# inserted. Any job already exists in the queue will be ignored. # inserted. Any job already exists in the queue will be ignored.
def push(jobs : Array(Job)) def push(jobs : Array(Job))