Class: RServiceBus::SubscriptionStorage_Redis

Inherits:
SubscriptionStorage show all
Defined in:
lib/rservicebus/SubscriptionStorage/Redis.rb

Overview

Implementation of Subscription Storage to Redis

Instance Method Summary collapse

Constructor Details

#initialize(appName, uri) ⇒ SubscriptionStorage_Redis

Constructor

Parameters:

  • appName (String)

    Name of the application, which is used as a Namespace

  • uri (String)

    a location for the resource to which we will attach, eg redis://127.0.0.1/foo



14
15
16
17
18
# File 'lib/rservicebus/SubscriptionStorage/Redis.rb', line 14

def initialize( appName, uri )
	super(appName, uri)
       port = uri.port.nil? ? 6379 : uri.port
	@redis = Redis.new( :host=>uri.host, :port=>port )
end

Instance Method Details

#add(eventName, queueName) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rservicebus/SubscriptionStorage/Redis.rb', line 43

def add( eventName, queueName )
	content = @redis.get( @appName + '.Subscriptions')
	if content.nil? then
		subscriptions = Hash.new
	else
		subscriptions = YAML::load(content)
	end

	if subscriptions[eventName].nil? then
		subscriptions[eventName] = Array.new
	end
	
	subscriptions[eventName] << queueName
	subscriptions[eventName] = subscriptions[eventName].uniq

	@redis.set( @appName + '.Subscriptions', YAML::dump(subscriptions ) )

	return subscriptions
end

#getAllObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rservicebus/SubscriptionStorage/Redis.rb', line 20

def getAll
	RServiceBus.log 'Load subscriptions'
	begin
		content = @redis.get( @appName + '.Subscriptions')
		if content.nil? then
			subscriptions = Hash.new
		else
			subscriptions = YAML::load(content)
		end
		return subscriptions
	rescue Exception => e
		puts 'Error connecting to redis'
		if e.message == 'Redis::CannotConnectError' ||
				e.message == 'Redis::ECONNREFUSED' then
			puts '***Most likely, redis is not running. Start redis, and try running this again.'
		else
			puts e.message
			puts e.backtrace
		end
		abort()
	end
end

#remove(eventName, queueName) ⇒ Object



63
64
65
# File 'lib/rservicebus/SubscriptionStorage/Redis.rb', line 63

def remove( eventName, queueName )
	raise 'Method, remove, needs to be implemented for this subscription storage'
end