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

@@ -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))