Class: EventStoreSubscriptions::WatchDog
- Inherits:
-
Object
- Object
- EventStoreSubscriptions::WatchDog
- 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
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(collection, restart_terminator: nil) ⇒ WatchDog
constructor
A new instance of WatchDog.
-
#unwatch ⇒ EventStoreSubscriptions::WatchDog
Stop watching over the given subscriptions collection.
-
#watch ⇒ EventStoreSubscriptions::WatchDog
Start watching over the given Subscriptions collection.
Methods included from WaitForFinish
Constructor Details
#initialize(collection, restart_terminator: nil) ⇒ WatchDog
Returns a new instance of WatchDog.
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
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
21 22 23 |
# File 'lib/event_store_subscriptions/watch_dog.rb', line 21 def collection @collection end |
#state ⇒ Object (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
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
#unwatch ⇒ EventStoreSubscriptions::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
```
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 |
#watch ⇒ EventStoreSubscriptions::WatchDog
Start watching over the given Subscriptions collection
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 |