Class: EventStoreSubscriptions::Subscription

Inherits:
Object
  • Object
show all
Extended by:
MakeAtomic
Includes:
WaitForFinish
Defined in:
lib/event_store_subscriptions/subscription.rb

Constant Summary collapse

FORCED_SHUTDOWN_DELAY =

seconds

60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MakeAtomic

make_atomic

Methods included from WaitForFinish

#wait_for_finish

Constructor Details

#initialize(position:, client:, setup:, statistic: SubscriptionStatistic.new) ⇒ Subscription

Returns a new instance of Subscription.

Parameters:



18
19
20
21
22
23
24
25
26
# File 'lib/event_store_subscriptions/subscription.rb', line 18

def initialize(position:, client:, setup:, statistic: SubscriptionStatistic.new)
  @position = position
  @client = client
  @setup = setup
  @state = ObjectState.new
  @statistic = statistic
  @runner = nil
  @semaphore = Mutex.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/event_store_subscriptions/subscription.rb', line 11

def client
  @client
end

#positionObject (readonly)

Returns the value of attribute position.



11
12
13
# File 'lib/event_store_subscriptions/subscription.rb', line 11

def position
  @position
end

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



11
12
13
# File 'lib/event_store_subscriptions/subscription.rb', line 11

def semaphore
  @semaphore
end

#setupObject (readonly)

Returns the value of attribute setup.



11
12
13
# File 'lib/event_store_subscriptions/subscription.rb', line 11

def setup
  @setup
end

#stateObject (readonly)

Returns the value of attribute state.



11
12
13
# File 'lib/event_store_subscriptions/subscription.rb', line 11

def state
  @state
end

#statisticObject (readonly)

Returns the value of attribute statistic.



11
12
13
# File 'lib/event_store_subscriptions/subscription.rb', line 11

def statistic
  @statistic
end

Instance Method Details

#listenEventStoreSubscriptions::Subscription

Start listening for the events

Returns:



30
31
32
# File 'lib/event_store_subscriptions/subscription.rb', line 30

make_atomic def listen
  _listen
end

#stop_listeningEventStoreSubscriptions::Subscription

Stops listening for events. This command is async - the result is not immediate. Use the #wait_for_finish method to wait until the runner has fully stopped.

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/event_store_subscriptions/subscription.rb', line 37

make_atomic def stop_listening
  return self unless runner&.alive?

  state.halting!
  Thread.new do
    stopping_at = Time.now.utc
    loop do
      # Give Subscription up to FORCED_SHUTDOWN_DELAY seconds for graceful shutdown
      runner&.exit if Time.now.utc - stopping_at > FORCED_SHUTDOWN_DELAY

      unless runner&.alive?
        state.stopped!
        self.runner = nil
        break
      end
      sleep 0.1
    end
  end
  self
end