]> git.openstreetmap.org Git - rails.git/blob - public/dispatch.fcgi
Make the report_error function support any http status code for added flexibility.
[rails.git] / public / dispatch.fcgi
1 #!/usr/bin/ruby1.8
2 #
3 # You may specify the path to the FastCGI crash log (a log of unhandled
4 # exceptions which forced the FastCGI instance to exit, great for debugging)
5 # and the number of requests to process before running garbage collection.
6 #
7 # By default, the FastCGI crash log is RAILS_ROOT/log/fastcgi.crash.log
8 # and the GC period is nil (turned off).  A reasonable number of requests
9 # could range from 10-100 depending on the memory footprint of your app.
10 #
11 # Example:
12 #   # Default log path, normal GC behavior.
13 #   RailsFCGIHandler.process!
14 #
15 #   # Default log path, 50 requests between GC.
16 #   RailsFCGIHandler.process! nil, 50
17 #
18 #   # Custom log path, normal GC behavior.
19 #   RailsFCGIHandler.process! '/var/log/myapp_fcgi_crash.log'
20 #
21 require File.dirname(__FILE__) + "/../config/environment"
22 require 'fcgi_handler'
23
24 class OpenStreetMapFCGIHandler < RailsFCGIHandler
25 protected
26   def process_request(cgi)
27     # Call superclass to process the request
28     super
29
30     # Restart if we've hit our memory limit
31     if resident_size > 512
32       run_gc!
33
34       if resident_size > 512
35         dispatcher_log :info, "restarting due to memory limit"
36         restart!
37       end
38     end
39   end
40
41   def resident_size
42     # Read statm to get process sizes. Format is
43     #   Size RSS Shared Text Lib Data
44     fields = File.open("/proc/self/statm") do |file|
45       fields = file.gets.split(" ")
46     end
47
48     # Return resident size in megabytes
49     return fields[1].to_i / 256
50   end
51
52 end
53
54 OpenStreetMapFCGIHandler.process! nil, 10