- periodic scan

This commit is contained in:
Alex Ling 2020-02-16 17:20:12 +00:00
parent 26bb49fab5
commit d9129ad58f
3 changed files with 17 additions and 6 deletions

View File

@ -13,7 +13,7 @@ class Config
property db_path = File.expand_path "~/mango/mango.db", home: true
@[YAML::Field(key: "scan_interval_minutes")]
property scan_interval = 5
property scan_interval : Int32 = 5
def self.load
cfg_path = File.expand_path "~/.config/mango/config.yml", home: true

View File

@ -142,13 +142,24 @@ class TitleInfo
end
class Library
JSON.mapping dir: String, titles: Array(Title)
JSON.mapping dir: String, titles: Array(Title), scan_interval: Int32
def initialize(@dir)
def initialize(@dir, @scan_interval)
# explicitly initialize @titles to bypass the compiler check. it will
# be filled with actuall Titles in the `scan` call below
# be filled with actual Titles in the `scan` call below
@titles = [] of Title
scan
return scan if @scan_interval < 1
spawn do
loop do
start = Time.local
puts "#{start} Starting periodic scan"
scan
ms = (Time.local - start).total_milliseconds
puts "Scanned #{@titles.size} titles in #{ms}ms"
sleep @scan_interval * 60
end
end
end
def get_title(name)
@titles.find { |t| t.title == name }

View File

@ -4,7 +4,7 @@ require "./library"
require "./storage"
config = Config.load
library = Library.new config.library_path
library = Library.new config.library_path, config.scan_interval
storage = Storage.new config.db_path
server = Server.new config, library, storage