Class: Honker::CoreWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/honker.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_path, extension_path, backend, watcher_poll_interval_ms) ⇒ CoreWatcher

Returns a new instance of CoreWatcher.

Raises:

  • (ArgumentError)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/honker.rb', line 116

def initialize(db_path, extension_path, backend, watcher_poll_interval_ms)
  @lib = Fiddle.dlopen(extension_path)
  @open = Fiddle::Function.new(
    @lib["honker_watcher_open_v2"],
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG_LONG, Fiddle::TYPE_VOIDP, Fiddle::TYPE_SIZE_T],
    Fiddle::TYPE_VOIDP,
  )
  @wait = Fiddle::Function.new(
    @lib["honker_watcher_wait"],
    [Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG_LONG],
    Fiddle::TYPE_INT,
  )
  @close = Fiddle::Function.new(
    @lib["honker_watcher_close"],
    [Fiddle::TYPE_VOIDP],
    Fiddle::TYPE_VOID,
  )
  err = "\0" * 1024
  @handle = @open.call(
    db_path.to_s,
    backend.to_s,
    watcher_poll_interval_ms || 1,
    err,
    err.bytesize,
  )
  return unless @handle.to_i.zero?

  raise ArgumentError, err.delete_suffix("\0").split("\0", 2).first
end

Instance Method Details

#closeObject



154
155
156
157
158
159
# File 'lib/honker.rb', line 154

def close
  return if @handle.nil? || @handle.to_i.zero?

  @close.call(@handle)
  @handle = nil
end

#wait(timeout_s) ⇒ Object

Raises:



146
147
148
149
150
151
152
# File 'lib/honker.rb', line 146

def wait(timeout_s)
  code = @wait.call(@handle, (timeout_s * 1000).ceil)
  return true if code == 1
  return false if code == 0

  raise Error, "honker update watcher closed or died"
end