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.



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.



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!



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”.



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.



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.



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.



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

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