Class: Pubilion::Worker::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/pubilion/worker/subscriber.rb

Overview

Subscriber on Pubilion

Constant Summary collapse

SHUTDOWN_WAIT =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, handler) ⇒ Subscriber

Returns a new instance of Subscriber.



13
14
15
16
# File 'lib/pubilion/worker/subscriber.rb', line 13

def initialize(config, handler)
  @config = config
  @handler = handler
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/pubilion/worker/subscriber.rb', line 11

def config
  @config
end

#handlerObject (readonly)

Returns the value of attribute handler.



11
12
13
# File 'lib/pubilion/worker/subscriber.rb', line 11

def handler
  @handler
end

Instance Method Details

#alive?Boolean

Check if subscriber is alive.

Returns:

  • (Boolean)


37
38
39
# File 'lib/pubilion/worker/subscriber.rb', line 37

def alive?
  @subscriber.started? && !@subscriber.stopped?
end

#runObject

Start streaming pull subscribe.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pubilion/worker/subscriber.rb', line 19

def run
  @subscriber = subscription.listen do |message|
    handler.on_message(message)
  end

  @subscriber.on_error do |error|
    handler.on_error(error)
  end

  @subscriber.start
end

#shutdownObject

Stop streaming pull subscribe.



32
33
34
# File 'lib/pubilion/worker/subscriber.rb', line 32

def shutdown
  @subscriber&.stop&.wait!(SHUTDOWN_WAIT)
end

#stopped?Boolean

Check if subscriber is stopped.

Returns:

  • (Boolean)


42
43
44
# File 'lib/pubilion/worker/subscriber.rb', line 42

def stopped?
  @subscriber.stopped?
end