Class: Tincan::Sender

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

Overview

An object whose purpose is to send messages to a given series of Redis message queues for those receiving them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tincan::Receiver

Creates and return a sender object, ready to send. You can pass in either a hash or a block; the block takes priority.

Parameters:

  • options (Hash) (defaults to: {})

    A list of keys/values to assign to this instance.



17
18
19
20
21
22
23
24
25
26
# File 'lib/tincan/sender.rb', line 17

def initialize(options = {})
  if block_given?
    yield(self)
  else
    @config = options
    ivars =  %i(redis_host redis_port namespace)
    ivars.each { |n| send("#{n}=".to_sym, @config[n]) }
  end
  self.redis_port ||= 6379
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/tincan/sender.rb', line 8

def config
  @config
end

#namespaceObject

Returns the value of attribute namespace.



9
10
11
# File 'lib/tincan/sender.rb', line 9

def namespace
  @namespace
end

#redis_hostObject

Returns the value of attribute redis_host.



9
10
11
# File 'lib/tincan/sender.rb', line 9

def redis_host
  @redis_host
end

#redis_portObject

Returns the value of attribute redis_port.



9
10
11
# File 'lib/tincan/sender.rb', line 9

def redis_port
  @redis_port
end

Instance Method Details

#flush_all_queues_for_object(object_name) ⇒ Boolean

Tells Redis to delete any pending messages for registered receivers, by essentially deleting the message key.

Returns:

  • (Boolean)

    True, just because.



57
58
59
60
61
62
# File 'lib/tincan/sender.rb', line 57

def flush_all_queues_for_object(object_name)
  keys_for_receivers(object_name).each do |key|
    redis_client.del(key)
  end
  true
end

#identifier_for_message(message) ⇒ Integer

Generates an identifier to be used for a message. It’s unique!

Parameters:

  • message (Tincan::Message)

    The message for which to generate a unique identifier.

Returns:

  • (Integer)

    A unique identifier number for the message.



86
87
88
# File 'lib/tincan/sender.rb', line 86

def identifier_for_message(message)
  message.message_id
end

#keys_for_receivers(object_name, exclude: []) ⇒ Array

Asks Redis for the set of all active receivers and generates string keys for all of them. Formatted like “namespace:object:client:messages”.

Parameters:

  • object_name (String)

    The name of the object, used in formulating which receiver list keys to return.

  • exclude (Array) (defaults to: [])

    An optional array of client names to exclude from the returned list of receiver list keys.

Returns:

  • (Array)

    An array of keys identifying all receiver pointer lists.



45
46
47
48
49
50
51
52
# File 'lib/tincan/sender.rb', line 45

def keys_for_receivers(object_name, exclude: [])
  exclude ||= []
  receiver_list_key = key_for_elements(object_name, 'receivers')
  receivers = redis_client.smembers(receiver_list_key)
  receivers.reject { |r| exclude.include?(r) }.map do |receiver|
    key_for_elements(object_name, receiver, 'messages')
  end
end

#primary_key_for_message(message) ⇒ String

Generates a key to be used as the primary destination key in Redis.

Parameters:

Returns:

  • (String)

    A properly-formatted key to be used with Redis.



93
94
95
96
# File 'lib/tincan/sender.rb', line 93

def primary_key_for_message(message)
  identifier = identifier_for_message(message)
  key_for_elements(message.object_name.downcase, 'messages', identifier)
end

#publish(message, exclude: []) ⇒ Boolean

Bundles up an object in a message object and publishes it to the Redis host.

Parameters:

  • message (Tincan::Message)

    The message to publish.

  • exclude (Array) (defaults to: [])

    An optional array of client names to exclude from getting the published message.

Returns:

  • (Boolean)

    true if the operation was a success.



72
73
74
75
76
77
78
# File 'lib/tincan/sender.rb', line 72

def publish(message, exclude: [])
  identifier = identifier_for_message(message)
  redis_client.set(primary_key_for_message(message), message.to_json)
  keys = keys_for_receivers(message.object_name.downcase, exclude: exclude)
  keys.each { |key| redis_client.rpush(key, identifier) }
  true
end

#redis_clientRedis

The instance of a Redis communicator that can publish messages.

Returns:

  • (Redis)

    The Redis client used by this object.



32
33
34
# File 'lib/tincan/sender.rb', line 32

def redis_client
  @redis_client ||= ::Redis.new(host: redis_host, port: redis_port)
end