Class: EventStoreSubscriptions::WatchDog

Inherits:
Object
  • Object
show all
Includes:
WaitForFinish
Defined in:
lib/event_store_subscriptions/watch_dog.rb

Overview

Watches over the given subscriptions collection and restarts dead subscriptions. It is useful in cases when your subscription’s handler raises error. Its usage is optional.

Constant Summary collapse

CHECK_INTERVAL =

seconds. How often to scan subscriptions

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WaitForFinish

#wait_for_finish

Constructor Details

#initialize(collection, restart_terminator: nil) ⇒ WatchDog

Returns a new instance of WatchDog.

Parameters:

  • collection (EventStoreSubscriptions::Subscriptions)
  • restart_terminator (Proc, nil) (defaults to: nil)

    define a terminator that would halt Subscription restart process if the result of it execution is truthy. Subscription instance will be passed as a first argument into it, and, based on it, you should decide whether to process the restart or not.



29
30
31
32
33
34
# File 'lib/event_store_subscriptions/watch_dog.rb', line 29

def initialize(collection, restart_terminator: nil)
  @collection = collection
  @state = ObjectState.new
  @runner = nil
  @restart_terminator = restart_terminator
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



21
22
23
# File 'lib/event_store_subscriptions/watch_dog.rb', line 21

def collection
  @collection
end

#stateObject (readonly)

Returns the value of attribute state.



21
22
23
# File 'lib/event_store_subscriptions/watch_dog.rb', line 21

def state
  @state
end

Class Method Details

.watch(collection, restart_terminator: nil) ⇒ EventStoreSubscriptions::WatchDog

Parameters:

Returns:



15
16
17
# File 'lib/event_store_subscriptions/watch_dog.rb', line 15

def watch(collection, restart_terminator: nil)
  new(collection, restart_terminator: restart_terminator).watch
end

Instance Method Details

#unwatchEventStoreSubscriptions::WatchDog

Stop watching over the given subscriptions collection. This command is async - the result is not immediate. Use the #wait_for_finish method in order to wait until the runner has fully stopped . Example:

```ruby
watch_dog.unwatch.wait_for_finish
```

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/event_store_subscriptions/watch_dog.rb', line 68

def unwatch
  return self unless runner&.alive?

  state.halting!
  Thread.new do
    loop do
      # If runner sleeps between runs we can safely shut it down. Even if the edge case happens,
      # when a runner's status changes between its check and `runner.exit`, it is still ok, it
      # would be shut down anyway because of the guard condition `break unless state.running?`
      runner.exit if runner&.status == 'sleep'
      unless runner&.alive?
        state.stopped!
        self.runner = nil
        break
      end
      sleep 0.1
    end
  end
  self
end

#watchEventStoreSubscriptions::WatchDog

Start watching over the given Subscriptions collection

Returns:



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

def watch
  self.runner ||=
    begin
      state.running!
      Thread.new do
        loop do
          sleep CHECK_INTERVAL
          break unless state.running?

          collection.subscriptions.each do |sub|
            break unless state.running?

            restart_subscription(sub) if sub.state.dead?
          end
        end
      rescue StandardError => e
        state.dead!
        raise
      end
    end
  self
end