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.


165
166
167
168
169
170
171
172
173
# File 'lib/discover.rb', line 165

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


184
185
186
187
188
189
190
191
192
193
194
# File 'lib/discover.rb', line 184

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


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/discover.rb', line 196

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


180
181
182
# File 'lib/discover.rb', line 180

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

#onlineObject


175
176
177
178
# File 'lib/discover.rb', line 175

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