Class: BBK::App::Middlewares::Watchdog

Inherits:
Base
  • Object
show all
Defined in:
lib/bbk/app/middlewares/watchdog.rb,
sig/bbk/app/middlewares/watchdog.rbs

Defined Under Namespace

Modules: _App, _MessageFactory

Instance Attribute Summary collapse

Attributes inherited from Base

#app

Instance Method Summary collapse

Constructor Details

#initialize(publisher, route, message_factory, reply_to, delay: 20, timeout: 60, watcher_delay: 40) ⇒ Watchdog

Returns a new instance of Watchdog.

Parameters:

  • (Object)
  • (String)
  • (_MessageFactory)
  • (String)
  • delay: (Integer) (defaults to: 20)
  • timeout: (Integer) (defaults to: 60)
  • watcher_delay: (Integer) (defaults to: 40)


9
10
11
12
13
14
15
16
17
18
# File 'lib/bbk/app/middlewares/watchdog.rb', line 9

def initialize(publisher, route, message_factory, reply_to, delay: 20, timeout: 60, watcher_delay: 40)
  @publisher = publisher
  @route = route
  @message_factory = message_factory
  @reply_to = reply_to
  @delay = delay
  @timeout = timeout
  @timestamp = Time.now.to_i
  @watcher_delay = watcher_delay
end

Instance Attribute Details

#delayInteger (readonly)

Returns the value of attribute delay.

Returns:

  • (Integer)


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.

Returns:



6
7
8
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6

def message_factory
  @message_factory
end

#pinger_threadThread? (readonly)

Returns the value of attribute pinger_thread.

Returns:

  • (Thread, nil)


6
7
8
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6

def pinger_thread
  @pinger_thread
end

#publisherObject (readonly)

Returns the value of attribute publisher.

Returns:

  • (Object)


6
7
8
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6

def publisher
  @publisher
end

#reply_toString (readonly)

Returns the value of attribute reply_to.

Returns:

  • (String)


6
7
8
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6

def reply_to
  @reply_to
end

#routeString (readonly)

Returns the value of attribute route.

Returns:

  • (String)


6
7
8
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6

def route
  @route
end

#timeoutInteger (readonly)

Returns the value of attribute timeout.

Returns:

  • (Integer)


6
7
8
# File 'lib/bbk/app/middlewares/watchdog.rb', line 6

def timeout
  @timeout
end

#watched_delayInteger (readonly)

Returns the value of attribute watched_delay.

Returns:

  • (Integer)


20
21
22
# File 'sig/bbk/app/middlewares/watchdog.rbs', line 20

def watched_delay
  @watched_delay
end

#watched_threadThread? (readonly)

Returns the value of attribute watched_thread.

Returns:

  • (Thread, nil)


22
23
24
# File 'sig/bbk/app/middlewares/watchdog.rbs', line 22

def watched_thread
  @watched_thread
end

#watcher_delayObject (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_threadObject (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.

Parameters:



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

#startvoid

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_pingvoid

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,
        message_factory.build(reply_to)
      )
      sleep delay
    end
  end
end

#start_watchvoid

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

#stopvoid

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

#touchvoid

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