Class: ScoutRails::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_rails/environment.rb

Instance Method Summary collapse

Instance Method Details

#app_serverObject

This needs to be improved. Frequently, multiple app servers gem are present and which ever is checked first becomes the designated app server.

I’ve put Thin and Webrick last as they are often used in development and included in Gemfiles but less likely used in production.

Next step: (1) list out all detected app servers (2) install hooks for those that need it (passenger, rainbows, unicorn).

Believe the biggest downside is the master process for forking app servers will get a background worker. Not sure how this will impact metrics (it shouldn’t process requests).



64
65
66
67
68
69
70
71
72
# File 'lib/scout_rails/environment.rb', line 64

def app_server
  @app_server ||= if passenger? then :passenger
                elsif rainbows? then :rainbows
                elsif unicorn? then :unicorn
                elsif thin? then :thin
                elsif webrick? then :webrick
                else nil
                end
end

#envObject



4
5
6
7
8
9
10
11
# File 'lib/scout_rails/environment.rb', line 4

def env
  @env ||= case framework
           when :rails then RAILS_ENV.dup
           when :rails3_or_4 then Rails.env
           when :sinatra
             ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'
           end
end

#forking?Boolean

If forking, don’t start worker thread in the master process. Since it’s started as a Thread, it won’t survive the fork.

Returns:

  • (Boolean)


110
111
112
# File 'lib/scout_rails/environment.rb', line 110

def forking?
  passenger? or unicorn? or rainbows?
end

#frameworkObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scout_rails/environment.rb', line 13

def framework
  @framework ||= case
                  when defined?(::Rails) && defined?(ActionController)
                    if Rails::VERSION::MAJOR < 3
                      :rails
                    else
                      :rails3_or_4
                    end
                  when defined?(::Sinatra) && defined?(::Sinatra::Base) then :sinatra
                  else :ruby
                  end
end

#jruby?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/scout_rails/environment.rb', line 120

def jruby?
  defined?(JRuby)
end

#passenger?Boolean

Called via #forking? since Passenger forks. Adds an event listener to start the worker thread inside the passenger worker process. Background: www.modrails.com/documentation/Users%20guide%20Nginx.html#spawning%5Fmethods%5Fexplained

Returns:

  • (Boolean)


87
88
89
# File 'lib/scout_rails/environment.rb', line 87

def passenger?
  (defined?(::Passenger) && defined?(::Passenger::AbstractServer)) || defined?(::PhusionPassenger)
end

#processorsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/scout_rails/environment.rb', line 26

def processors
  return @processors if @processors
  unless @processors
    proc_file = '/proc/cpuinfo'
    if !File.exist?(proc_file)
      @processors = 1
    elsif `cat #{proc_file} | grep 'model name' | wc -l` =~ /(\d+)/
      @processors = $1.to_i
    end
    if @processors < 1
      @processors = 1
    end 
  end
  @processors
end

#rainbows?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/scout_rails/environment.rb', line 95

def rainbows?
  if defined?(::Rainbows) && defined?(::Rainbows::HttpServer)
    ObjectSpace.each_object(::Rainbows::HttpServer) { |x| return true }
  end
end

#rootObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/scout_rails/environment.rb', line 42

def root
  if framework == :rails
    RAILS_ROOT.to_s
  elsif framework == :rails3_or_4
    Rails.root
  elsif framework == :sinatra
    Sinatra::Application.root
  else
    '.'
  end
end

#rubinius?Boolean

ruby checks

Returns:

  • (Boolean)


116
117
118
# File 'lib/scout_rails/environment.rb', line 116

def rubinius?
  RUBY_VERSION =~ /rubinius/i
end

#ruby_19?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/scout_rails/environment.rb', line 124

def ruby_19?
  defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION.match(/^1\.9/)
end

#sinatra?Boolean

framework checks

Returns:

  • (Boolean)


130
131
132
# File 'lib/scout_rails/environment.rb', line 130

def sinatra?
  defined?(Sinatra::Application)
end

#thin?Boolean

app server related-checks

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/scout_rails/environment.rb', line 76

def thin?
  if defined?(::Thin) && defined?(::Thin::Server)
    # Ensure Thin is actually initialized. It could just be required and not running.
    ObjectSpace.each_object(Thin::Server) { |x| return true }
    false
  end
end

#unicorn?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
# File 'lib/scout_rails/environment.rb', line 101

def unicorn?
  if defined?(::Unicorn) && defined?(::Unicorn::HttpServer)
    # Ensure Unicorn is actually initialized. It could just be required and not running.
    ObjectSpace.each_object(::Unicorn::HttpServer) { |x| return true }
  end
end

#webrick?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/scout_rails/environment.rb', line 91

def webrick?
  defined?(::WEBrick) && defined?(::WEBrick::VERSION)
end