diff --git a/src/config.cr b/src/config.cr index 0235556..44f7d6b 100644 --- a/src/config.cr +++ b/src/config.cr @@ -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 diff --git a/src/library.cr b/src/library.cr index 8d416e4..7654648 100644 --- a/src/library.cr +++ b/src/library.cr @@ -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 } diff --git a/src/mango.cr b/src/mango.cr index 4691a53..1d2d2d5 100644 --- a/src/mango.cr +++ b/src/mango.cr @@ -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