Class: ScoutRailsProxy::Environment

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

Instance Method Summary collapse

Instance Method Details

#app_serverObject



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

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

#envObject



4
5
6
7
8
9
10
11
# File 'lib/scout_rails_proxy/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)


101
102
103
# File 'lib/scout_rails_proxy/environment.rb', line 101

def forking?
  passenger? or unicorn?
end

#frameworkObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scout_rails_proxy/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)


111
112
113
# File 'lib/scout_rails_proxy/environment.rb', line 111

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)


84
85
86
# File 'lib/scout_rails_proxy/environment.rb', line 84

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_proxy/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

#rootObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/scout_rails_proxy/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)


107
108
109
# File 'lib/scout_rails_proxy/environment.rb', line 107

def rubinius?
  RUBY_VERSION =~ /rubinius/i
end

#sinatra?Boolean

framework checks

Returns:

  • (Boolean)


117
118
119
# File 'lib/scout_rails_proxy/environment.rb', line 117

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

#thin?Boolean

app server related-checks

Returns:

  • (Boolean)


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

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)


73
74
75
76
77
78
79
# File 'lib/scout_rails_proxy/environment.rb', line 73

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 }
    false
  end
end

#webrick?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/scout_rails_proxy/environment.rb', line 88

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