Class: Viki::Queue::Service

Inherits:
Object
  • Object
show all
Includes:
Message, Runner
Defined in:
lib/viki/queue/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Runner

#run

Methods included from Message

#bulk, #create_message, #delete_message, #update_message

Constructor Details

#initialize(opts = {}) ⇒ Service

Returns a new instance of Service.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/viki/queue/service.rb', line 8

def initialize(opts={})
  @connection = Bunny.new({
    host: Viki::Queue.host,
    port: Viki::Queue.port,
    username: Viki::Queue.username,
    password: Viki::Queue.password,
    keepalive: true,
    threaded: false,
    socket_timeout: 0,
    connect_timeout: 0})
  @connection.start
  @channel = @connection.create_channel
  @exchange = @channel.topic("general", durable: true)
  @routing = opts.fetch(:routing, 'resources')
end

Instance Attribute Details

#exchangeObject

Returns the value of attribute exchange.



6
7
8
# File 'lib/viki/queue/service.rb', line 6

def exchange
  @exchange
end

#routingObject

Returns the value of attribute routing.



6
7
8
# File 'lib/viki/queue/service.rb', line 6

def routing
  @routing
end

Instance Method Details

#delete(queue) ⇒ Object



47
48
49
# File 'lib/viki/queue/service.rb', line 47

def delete(queue)
  @channel.queue(queue, durable: true).delete()
end

#route(route) ⇒ Object



41
42
43
44
45
# File 'lib/viki/queue/service.rb', line 41

def route(route)
  return if route.nil? or route == ""
  route = route[0...-1] if route[-1] == '.'
  @routing = route
end

#stopObject



24
25
26
27
# File 'lib/viki/queue/service.rb', line 24

def stop
  @connection.stop
rescue
end

#subscribe(queue, resources) ⇒ Object



29
30
31
32
33
# File 'lib/viki/queue/service.rb', line 29

def subscribe(queue, resources)
  resources.each do |r|
    @channel.queue(queue, durable: true).bind(@exchange, :routing_key => "#{@routing}.#{r}.#")
  end
end

#unsubscribe(queue, resources) ⇒ Object



35
36
37
38
39
# File 'lib/viki/queue/service.rb', line 35

def unsubscribe(queue, resources)
  resources.each do |r|
    @channel.queue(queue, durable: true).unbind(@exchange, :routing_key => "#{@routing}.#{r}.#")
  end
end