mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-04 20:05:29 -04:00
Move pop
to the Downloader
classes
This commit is contained in:
parent
2a36804e8d
commit
92f5a90629
@ -18,7 +18,7 @@ module MangaDex
|
||||
sleep 1.second
|
||||
next if @stopped || @downloading
|
||||
begin
|
||||
job = @queue.pop
|
||||
job = pop
|
||||
next if job.nil?
|
||||
download job
|
||||
rescue e
|
||||
@ -28,6 +28,21 @@ module MangaDex
|
||||
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)
|
||||
@downloading = true
|
||||
@queue.set_status Queue::JobStatus::Downloading, job
|
||||
|
@ -5,5 +5,20 @@ class Plugin
|
||||
def initialize
|
||||
super
|
||||
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
|
||||
|
22
src/queue.cr
22
src/queue.cr
@ -2,7 +2,7 @@ require "sqlite3"
|
||||
require "./util/*"
|
||||
|
||||
class Queue
|
||||
class Downloader
|
||||
abstract class Downloader
|
||||
property stopped = false
|
||||
@library_path : String = Config.current.library_path
|
||||
@downloading = false
|
||||
@ -11,6 +11,8 @@ class Queue
|
||||
@queue = Queue.default
|
||||
@queue << self
|
||||
end
|
||||
|
||||
abstract def pop : Job?
|
||||
end
|
||||
|
||||
class PageJob
|
||||
@ -88,7 +90,7 @@ class Queue
|
||||
end
|
||||
end
|
||||
|
||||
@path : String
|
||||
getter path : String
|
||||
@downloaders = [] of Downloader
|
||||
@paused = false
|
||||
|
||||
@ -122,22 +124,6 @@ class Queue
|
||||
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
|
||||
# inserted. Any job already exists in the queue will be ignored.
|
||||
def push(jobs : Array(Job))
|
||||
|
Loading…
x
Reference in New Issue
Block a user