Class: BBK::App::Middlewares::SelfKiller

Inherits:
Object
  • Object
show all
Defined in:
lib/bbk/app/middlewares/self_killer.rb

Constant Summary collapse

SELF_KILLER_LOG_INTERVAL =
300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher, delay: 10 * 60, threshold: 10_000, logger: ::Logger.new(STDOUT)) ⇒ SelfKiller

Returns a new instance of SelfKiller.



11
12
13
14
15
16
17
18
19
20
# File 'lib/bbk/app/middlewares/self_killer.rb', line 11

def initialize(dispatcher, delay: 10 * 60, threshold: 10_000, logger: ::Logger.new(STDOUT))
  @dispatcher = dispatcher
  @threshold = threshold
  @count = 0
  @stop_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + delay
  @stopping = false
  @logger = logger
  @logger.info "[SelfKiller] Initializing: #{@count}/#{@threshold}"
  reset_log_timer
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



9
10
11
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9

def count
  @count
end

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



9
10
11
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9

def dispatcher
  @dispatcher
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



9
10
11
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9

def stop_time
  @stop_time
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



9
10
11
# File 'lib/bbk/app/middlewares/self_killer.rb', line 9

def threshold
  @threshold
end

Instance Method Details

#build(app) ⇒ Object



22
23
24
25
# File 'lib/bbk/app/middlewares/self_killer.rb', line 22

def build(app)
  @app = app
  self
end

#call(msg) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/bbk/app/middlewares/self_killer.rb', line 27

def call(msg)
  @count += 1
  if time_exceed?(@log_timer)
    @logger.info "[SelfKiller] Threshold status: #{@count}/#{@threshold}, delayed: #{!time_exceed?}"
    reset_log_timer
  end
  close_dispatcher if stop?

  @app.call(msg)
end