Class: Rcom::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/rcom/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Task

Returns a new instance of Task.



5
6
7
8
# File 'lib/rcom/task.rb', line 5

def initialize(params)
  @node = params[:node]
  @channel = params[:channel]
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



3
4
5
# File 'lib/rcom/task.rb', line 3

def channel
  @channel
end

#nodeObject (readonly)

Returns the value of attribute node.



3
4
5
# File 'lib/rcom/task.rb', line 3

def node
  @node
end

Instance Method Details

#publish(message) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/rcom/task.rb', line 10

def publish(message)
  begin
    node.lpush(channel, message.to_msgpack)
    return true
  rescue
    return nil
  end
end

#subscribeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rcom/task.rb', line 19

def subscribe
  begin
    loop do
      ch, request = node.brpop(channel)
      message = MessagePack.unpack(
        request,
        symbolize_keys: true
      )
      yield message
    end
  rescue Interrupt => _
  end
end