File: slowpoke.rb

package info (click to toggle)
ruby-slowpoke 0.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 96 kB
  • sloc: ruby: 99; makefile: 7
file content (49 lines) | stat: -rw-r--r-- 1,065 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# dependencies
require "rack/timeout/base"

# modules
require "slowpoke/middleware"
require "slowpoke/railtie"
require "slowpoke/timeout"
require "slowpoke/version"

module Slowpoke
  ENV_KEY = "slowpoke.timed_out".freeze

  def self.kill
    if defined?(::PhusionPassenger)
      `passenger-config detach-process #{Process.pid}`
    elsif defined?(::Puma)
      Process.kill("TERM", Process.pid)
    else
      Process.kill("QUIT", Process.pid)
    end
  end

  def self.on_timeout(&block)
    if block_given?
      @on_timeout = block
    else
      @on_timeout
    end
  end

  on_timeout do |env|
    next if Rails.env.development? || Rails.env.test?

    Slowpoke.kill
  end
end

# remove noisy logger
Rack::Timeout.unregister_state_change_observer(:logger)

# process protection and notifications
Rack::Timeout.register_state_change_observer(:slowpoke) do |env|
  if env[Rack::Timeout::ENV_INFO_KEY].state == :timed_out
    env[Slowpoke::ENV_KEY] = true

    # TODO better payload
    ActiveSupport::Notifications.instrument("timeout.slowpoke", {})
  end
end