Class: Droonga::CatalogObserver

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/droonga/catalog_observer.rb

Constant Summary collapse

DEFAULT_CATALOG_PATH =
"catalog.json"
CHECK_INTERVAL =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCatalogObserver

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

#catalogObject (readonly)

Returns the value of attribute catalog.



28
29
30
# File 'lib/droonga/catalog_observer.rb', line 28

def catalog
  @catalog
end

#on_reloadObject

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_pathObject



61
62
63
64
# File 'lib/droonga/catalog_observer.rb', line 61

def catalog_path
  path = ENV["DROONGA_CATALOG"] || DEFAULT_CATALOG_PATH
  File.expand_path(path)
end

#catalog_updated?Boolean

Returns:

  • (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_loadedObject



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

#startObject



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

#stopObject



46
47
48
# File 'lib/droonga/catalog_observer.rb', line 46

def stop
  @watcher.detach
end