Sha256: 70bf82f3c815d977c73b54422aa9643ed9131d84bc061bfa2c054091696842fb

Contents?: true

Size: 1.9 KB

Versions: 22

Compression:

Stored size: 1.9 KB

Contents

module BBK
  module App
    module Middlewares
      class Watchdog < Base

        attr_reader :publisher, :route, :message_factory, :reply_to, :delay, :timeout,
                    :watcher_delay, :pinger_thread, :watcher_thread

        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

        def build(app)
          @app = app
          self
        end

        def call(msg)
          touch
          @app.call(msg)
        end

        def start
          touch
          start_ping
          start_watch
          self
        end

        def stop
          @pinger_thread&.kill
          @watcher_thread&.kill
        end

        protected

          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

          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

          def touch
            @timestamp = Time.now.to_i
          end



      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
bbk-app-1.1.1.273627 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.1.273338 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.1.273312 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.1.273294 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.219406 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.216998 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.204569 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.200751 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.200186 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.199675 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.199604 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.199389 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.1.0.199383 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.0.0.152254 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.0.0.141716 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.0.0.80957 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.0.0.79514 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.0.0.79241 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.0.0.78020 lib/bbk/app/middlewares/watchdog.rb
bbk-app-1.0.0.72920 lib/bbk/app/middlewares/watchdog.rb