module Attention

The top-level API

Default options:

{
  namespace: 'attention',                # Redis key namespace
  ttl: 60,                               # Instance heartbeat TTL in seconds
  redis_url: 'redis://localhost:6379/0', # Redis connection string
  pool_size: 5,                          # Size of the publishing Redis pool
  timeout: 5                             # Redis connection timeout
}

Constants

VERSION

Attributes

instance[R]

The server {Instance}

options[RW]

Configuration options

Public Class Methods

activate(ip: nil, port: nil) click to toggle source

Publishes this server {Instance} @param ip [String] Optionally override the IP of the server @param port [Fixnum, Numeric] Optionally specify the port of the server @return [Instance] This server instance @see Instance#publish

# File lib/attention.rb, line 46
def self.activate(ip: nil, port: nil)
  @instance ||= Instance.new ip: ip, port: port
  instance.publish
  instance
end
deactivate() click to toggle source

Unpublishes this server {Instance} @see Instance#unpublish

# File lib/attention.rb, line 54
def self.deactivate
  @instance.unpublish if @instance
end
info_for(keys) click to toggle source

Maps the Redis key +get+s into a multi operation @!visibility private

# File lib/attention.rb, line 88
def self.info_for(keys)
  [].tap do |list|
    redis.call.multi do |multi|
      keys.each do |key|
        list << multi.get(key)
      end
    end
  end
end
instance_keys() click to toggle source

Finds instance keys @!visibility private

# File lib/attention.rb, line 82
def self.instance_keys
  redis.call.keys 'instance_*'
end
instances() click to toggle source

A list of the active {Instance}s @return [Array<Hash>]

[
  { 'id' => '1', 'ip' => '127.0.0.1', 'port' => 3000 },
  { 'id' => '2', 'ip' => '127.0.0.1', 'port' => 3001 }
]
# File lib/attention.rb, line 76
def self.instances
  resolve info_for instance_keys
end
on_change(&callback) click to toggle source

Uses a {Subscriber} to listen to changes to {Instance} statuses @yield The callback triggered on {Instance} changes @yieldparam change [Hash] The change event @yieldparam instances [Array<Hash>] The list of active {Instance}s @see Instance Format of the change events @see .instances Format of the instance information

# File lib/attention.rb, line 64
def self.on_change(&callback)
  Subscriber.new('instance') do |channel, change|
    callback.call change, instances
  end
end
redis() click to toggle source

Provides access to the {RedisPool} connections @return [Redis] A Redis connection

# File lib/attention.rb, line 37
def self.redis
  RedisPool.instance
end
resolve(list) click to toggle source

Resolves the list of future values from the multi operation @!visibility private

# File lib/attention.rb, line 100
def self.resolve(list)
  list.map do |future|
    JSON.parse future.value
  end
end