Class: Droonga::CatalogObserver
- Inherits:
-
Object
- Object
- Droonga::CatalogObserver
- Includes:
- Loggable
- Defined in:
- lib/droonga/catalog_observer.rb
Constant Summary collapse
- DEFAULT_CATALOG_PATH =
"catalog.json"- CHECK_INTERVAL =
1
Instance Attribute Summary collapse
-
#catalog ⇒ Object
readonly
Returns the value of attribute catalog.
-
#on_reload ⇒ Object
Returns the value of attribute on_reload.
Instance Method Summary collapse
- #catalog_path ⇒ Object
- #catalog_updated? ⇒ Boolean
- #ensure_latest_catalog_loaded ⇒ Object
-
#initialize ⇒ CatalogObserver
constructor
A new instance of CatalogObserver.
- #load_catalog! ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ CatalogObserver
Returns a new instance of CatalogObserver.
31 32 33 34 |
# File 'lib/droonga/catalog_observer.rb', line 31 def initialize @catalog_path = catalog_path load_catalog! end |
Instance Attribute Details
#catalog ⇒ Object (readonly)
Returns the value of attribute catalog.
28 29 30 |
# File 'lib/droonga/catalog_observer.rb', line 28 def catalog @catalog end |
#on_reload ⇒ Object
Returns the value of attribute on_reload.
29 30 31 |
# File 'lib/droonga/catalog_observer.rb', line 29 def on_reload @on_reload end |
Instance Method Details
#catalog_path ⇒ Object
61 62 63 64 |
# File 'lib/droonga/catalog_observer.rb', line 61 def catalog_path path = ENV["DROONGA_CATALOG"] || DEFAULT_CATALOG_PATH File.(path) end |
#catalog_updated? ⇒ Boolean
66 67 68 |
# File 'lib/droonga/catalog_observer.rb', line 66 def catalog_updated? File.mtime(catalog_path) > @catalog_mtime end |
#ensure_latest_catalog_loaded ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/droonga/catalog_observer.rb', line 50 def ensure_latest_catalog_loaded if catalog_updated? begin load_catalog! on_reload.call(catalog) if on_reload rescue Droonga::Error => error logger.warn("reload: fail", :path => @catalog_path, :error => error) end end end |
#load_catalog! ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/droonga/catalog_observer.rb', line 70 def load_catalog! loader = CatalogLoader.new(@catalog_path) @catalog = loader.load logger.info("loaded", :path => @catalog_path, :mtime => @catalog_mtime) ensure @catalog_mtime = File.mtime(@catalog_path) end |
#start ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/droonga/catalog_observer.rb', line 36 def start @watcher = Cool.io::TimerWatcher.new(CHECK_INTERVAL, true) observer = self @watcher.on_timer do observer.ensure_latest_catalog_loaded end loop = Coolio::Loop.default @watcher.attach(loop) end |
#stop ⇒ Object
46 47 48 |
# File 'lib/droonga/catalog_observer.rb', line 46 def stop @watcher.detach end |