Class: ScoutRails::Environment

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

Instance Method Summary collapse

Instance Method Details

#app_serverObject



54
55
56
57
58
59
60
61
62
# File 'lib/scout_rails/environment.rb', line 54

def app_server
  @app_server ||= if thin? then :thin
                elsif passenger? then :passenger
                elsif webrick? then :webrick
                elsif rainbows? then :rainbows
                elsif unicorn? then :unicorn
                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 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)


100
101
102
# File 'lib/scout_rails/environment.rb', line 100

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
                    end
                  when defined?(::Sinatra) && defined?(::Sinatra::Base) then :sinatra
                  else :ruby
                  end
end

#jruby?Boolean

Returns:

  • (Boolean)


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

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)


77
78
79
# File 'lib/scout_rails/environment.rb', line 77

def passenger?
  (defined?(::Passenger) && defined?(::Passenger::AbstractServer)) || defined?(::IN_PHUSION_PASSENGER)
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)


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

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
    Rails.root
  elsif framework == :sinatra
    Sinatra::Application.root
  else
    '.'
  end
end

#rubinius?Boolean

ruby checks

Returns:

  • (Boolean)


106
107
108
# File 'lib/scout_rails/environment.rb', line 106

def rubinius?
  RUBY_VERSION =~ /rubinius/i
end

#ruby_19?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/scout_rails/environment.rb', line 114

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

#sinatra?Boolean

framework checks

Returns:

  • (Boolean)


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

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

#thin?Boolean

app server related-checks

Returns:

  • (Boolean)


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

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)


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

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)


81
82
83
# File 'lib/scout_rails/environment.rb', line 81

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