Class: Discover::Service

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/discover.rb

Defined Under Namespace

Classes: Update, Watcher

Instance Method Summary collapse

Constructor Details

#initialize(client, name, filters = {}) ⇒ Service

Returns a new instance of Service.



173
174
175
176
177
178
179
180
181
# File 'lib/discover.rb', line 173

def initialize(client, name, filters={})
  @client = client
  @name = name
  @filters = filters
  @current = Condition.new
  @instances = {}
  @watchers = []
  async.process_updates
end

Instance Method Details

#each_leader(&block) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/discover.rb', line 192

def each_leader(&block)
  leader = self.leader
  block.call leader if leader

  each_update(false) do |update|
    if leader.nil? || (update.offline? && leader && update.address == leader.address)
      leader = self.leader
      block.call leader if leader
    end
  end
end

#each_update(include_current = true, &block) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/discover.rb', line 204

def each_update(include_current = true, &block)
  # Since updates are coming from a Proc being called in a different
  # Actor (the RPCClient), we need to suspend update notifications
  # here to avoid race conditions where we could potentially miss
  # updates between initializing the Watcher and adding it to @watchers
  watcher = pause_updates do
    watcher = Watcher.new(block)

    if include_current
      online.each { |u| watcher.notify u }
    end

    @watchers << watcher

    watcher
  end

  watcher.wait
end

#leaderObject



188
189
190
# File 'lib/discover.rb', line 188

def leader
  online.sort_by(&:created).first
end

#onlineObject



183
184
185
186
# File 'lib/discover.rb', line 183

def online
  @current.wait if @current
  @instances.values
end