Class: BBK::App::Middlewares::Watchdog
- Defined in:
- lib/bbk/app/middlewares/watchdog.rb,
sig/bbk/app/middlewares/watchdog.rbs
Defined Under Namespace
Modules: _App, _MessageFactory
Instance Attribute Summary collapse
-
#delay ⇒ Integer
readonly
Returns the value of attribute delay.
-
#message_factory ⇒ _MessageFactory
readonly
Returns the value of attribute message_factory.
-
#pinger_thread ⇒ Thread?
readonly
Returns the value of attribute pinger_thread.
-
#publisher ⇒ Object
readonly
Returns the value of attribute publisher.
-
#reply_to ⇒ String
readonly
Returns the value of attribute reply_to.
-
#route ⇒ String
readonly
Returns the value of attribute route.
-
#timeout ⇒ Integer
readonly
Returns the value of attribute timeout.
-
#watched_delay ⇒ Integer
readonly
Returns the value of attribute watched_delay.
-
#watched_thread ⇒ Thread?
readonly
Returns the value of attribute watched_thread.
-
#watcher_delay ⇒ Object
readonly
Returns the value of attribute watcher_delay.
-
#watcher_thread ⇒ Object
readonly
Returns the value of attribute watcher_thread.
Attributes inherited from Base
Instance Method Summary collapse
- #build(app) ⇒ void
- #call(msg) ⇒ Array[BBK::App::Dispatcher::Result]
-
#initialize(publisher, route, message_factory, reply_to, delay: 20, timeout: 60, watcher_delay: 40) ⇒ Watchdog
constructor
A new instance of Watchdog.
- #start ⇒ void
- #start_ping ⇒ void
- #start_watch ⇒ void
- #stop ⇒ void
- #touch ⇒ void
Constructor Details
#initialize(publisher, route, message_factory, reply_to, delay: 20, timeout: 60, watcher_delay: 40) ⇒ Watchdog
Returns a new instance of Watchdog.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 9 def initialize(publisher, route, , reply_to, delay: 20, timeout: 60, watcher_delay: 40) @publisher = publisher @route = route @message_factory = @reply_to = reply_to @delay = delay @timeout = timeout @timestamp = Time.now.to_i @watcher_delay = watcher_delay end |
Instance Attribute Details
#delay ⇒ Integer (readonly)
Returns the value of attribute delay.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def delay @delay end |
#message_factory ⇒ _MessageFactory (readonly)
Returns the value of attribute message_factory.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def @message_factory end |
#pinger_thread ⇒ Thread? (readonly)
Returns the value of attribute pinger_thread.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def pinger_thread @pinger_thread end |
#publisher ⇒ Object (readonly)
Returns the value of attribute publisher.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def publisher @publisher end |
#reply_to ⇒ String (readonly)
Returns the value of attribute reply_to.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def reply_to @reply_to end |
#route ⇒ String (readonly)
Returns the value of attribute route.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def route @route end |
#timeout ⇒ Integer (readonly)
Returns the value of attribute timeout.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def timeout @timeout end |
#watched_delay ⇒ Integer (readonly)
Returns the value of attribute watched_delay.
20 21 22 |
# File 'sig/bbk/app/middlewares/watchdog.rbs', line 20 def watched_delay @watched_delay end |
#watched_thread ⇒ Thread? (readonly)
Returns the value of attribute watched_thread.
22 23 24 |
# File 'sig/bbk/app/middlewares/watchdog.rbs', line 22 def watched_thread @watched_thread end |
#watcher_delay ⇒ Object (readonly)
Returns the value of attribute watcher_delay.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def watcher_delay @watcher_delay end |
#watcher_thread ⇒ Object (readonly)
Returns the value of attribute watcher_thread.
6 7 8 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6 def watcher_thread @watcher_thread end |
Instance Method Details
#build(app) ⇒ void
This method returns an undefined value.
20 21 22 23 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 20 def build(app) @app = app self end |
#call(msg) ⇒ Array[BBK::App::Dispatcher::Result]
25 26 27 28 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 25 def call(msg) touch @app.call(msg) end |
#start ⇒ void
This method returns an undefined value.
30 31 32 33 34 35 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 30 def start touch start_ping start_watch self end |
#start_ping ⇒ void
This method returns an undefined value.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 44 def start_ping @pinger_thread = Thread.new(publisher, delay, route) do |publisher, delay, route| Thread.current.name = 'WatchDog::Pinger' loop do publisher.publish BBK::App::Dispatcher::Result.new( route, .build(reply_to) ) sleep delay end end end |
#start_watch ⇒ void
This method returns an undefined value.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 57 def start_watch @watcher_thread = Thread.new(timeout, watcher_delay) do |timeout, watcher_delay| Thread.current.name = 'WatchDog::Watcher' msg = "[CRITICAL] watchdog: last ping is more than #{timeout} seconds ago" sleep watcher_delay while (Time.now.to_i - @timestamp) < timeout warn msg exit!(8) end end |
#stop ⇒ void
This method returns an undefined value.
37 38 39 40 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 37 def stop @pinger_thread&.kill @watcher_thread&.kill end |
#touch ⇒ void
This method returns an undefined value.
68 69 70 |
# File 'lib/bbk/app/middlewares/watchdog.rb', line 68 def touch @timestamp = Time.now.to_i end |