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

#attach(loop) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/droonga/catalog_observer.rb', line 53

def attach(loop)
  watcher = Cool.io::TimerWatcher.new(CHECK_INTERVAL, true)
  observer = self
  watcher.on_timer do
    observer.ensure_latest_catalog_loaded
  end
  loop.attach(watcher)
end

#catalog_pathObject



73
74
75
76
# File 'lib/droonga/catalog_observer.rb', line 73

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

#catalog_updated?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/droonga/catalog_observer.rb', line 78

def catalog_updated?
  File.mtime(catalog_path) > @catalog_mtime
end

#ensure_latest_catalog_loadedObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/droonga/catalog_observer.rb', line 62

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



82
83
84
85
86
87
88
# File 'lib/droonga/catalog_observer.rb', line 82

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
45
46
# File 'lib/droonga/catalog_observer.rb', line 36

def start
  @loop = Cool.io::Loop.new
  attach(@loop)
  @thread = Thread.new do
    begin
      @loop.run
    rescue => error
      logger.exception("error in catalog observing thread", error)
    end
  end
end

#stopObject



48
49
50
51
# File 'lib/droonga/catalog_observer.rb', line 48

def stop
  @loop.stop
  @thread.join
end