From 92f5a9062917511d8e1d3a9db51fc187950d283b Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Tue, 21 Jul 2020 17:20:03 +0000 Subject: [PATCH] Move `pop` to the `Downloader` classes --- src/mangadex/downloader.cr | 17 ++++++++++++++++- src/plugin/downloader.cr | 15 +++++++++++++++ src/queue.cr | 22 ++++------------------ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/mangadex/downloader.cr b/src/mangadex/downloader.cr index 3541d51..8bfe338 100644 --- a/src/mangadex/downloader.cr +++ b/src/mangadex/downloader.cr @@ -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 diff --git a/src/plugin/downloader.cr b/src/plugin/downloader.cr index 25dca6d..3488e78 100644 --- a/src/plugin/downloader.cr +++ b/src/plugin/downloader.cr @@ -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 diff --git a/src/queue.cr b/src/queue.cr index a4dbcb2..b7e8679 100644 --- a/src/queue.cr +++ b/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))