We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Centralize spawn options sanitization into a single function.
Hongli Lai (Phusion) (author)
Sun Nov 23 05:30:02 -0800 2008
commit  948dd851941ae09301e2068a8e4987ec873b9d48
tree    42b59d922a997858e5c3bcad607de233cab9c39b
parent  fdc2ab77d3581346a62ae2604af5769df1321345
...
42
43
44
45
46
47
48
49
 
50
51
52
...
42
43
44
 
 
 
 
 
45
46
47
48
0
@@ -42,11 +42,7 @@ class ApplicationSpawner
0
   # exit() during startup.
0
   # - SystemCallError, IOError, SocketError: Something went wrong.
0
   def spawn_application(app_root, options = {})
0
- options = {
0
- "lower_privilege" => true,
0
- "lowest_user" => "nobody",
0
- "environment" => "production"
0
- }.merge(options)
0
+ options = sanitize_spawn_options(options)
0
     
0
     a, b = UNIXSocket.pair
0
     pid = safe_fork(self.class.to_s, true) do
...
87
88
89
90
91
92
93
94
 
95
96
97
...
87
88
89
 
 
 
 
 
90
91
92
93
0
@@ -87,11 +87,7 @@ class ApplicationSpawner < AbstractServer
0
     rescue InvalidPath
0
       raise
0
     end
0
- @options = {
0
- "lower_privilege" => true,
0
- "lowest_user" => "nobody",
0
- "environment" => "production"
0
- }.merge(options)
0
+ @options = sanitize_spawn_options(options)
0
     @lower_privilege = @options["lower_privilege"]
0
     @lowest_user = @options["lowest_user"]
0
     @environment = @options["environment"]
...
150
151
152
153
154
155
156
157
 
158
159
160
...
150
151
152
 
 
 
 
 
153
154
155
156
0
@@ -150,11 +150,7 @@ class FrameworkSpawner < AbstractServer
0
   def spawn_application(app_root, options = {})
0
     app_root = normalize_path(app_root)
0
     assert_valid_app_root(app_root)
0
- options = {
0
- "lower_privilege" => true,
0
- "lowest_user" => "nobody",
0
- "environment" => "production"
0
- }.merge(options)
0
+ options = sanitize_spawn_options(options)
0
     options["app_root"] = app_root
0
     
0
     exception_to_propagate = nil
...
122
123
124
125
126
127
128
129
130
131
132
...
138
139
140
141
142
143
144
145
146
147
148
149
150
 
151
152
153
...
287
288
289
290
291
292
293
294
295
 
296
297
298
...
122
123
124
 
 
 
 
 
125
126
127
...
133
134
135
 
 
 
 
 
 
 
 
 
 
136
137
138
139
...
273
274
275
 
 
 
 
 
 
276
277
278
279
0
@@ -122,11 +122,6 @@ class SpawnManager < AbstractServer
0
   # A timeout of 0 means that the spawner server should never idle timeout. A timeout of
0
   # -1 means that the default timeout value should be used. The default value is -1.
0
   #
0
- # ['memory_limit']
0
- # The maximum amount of memory that the application instance may use. If the limit has
0
- # been reached, the application instance will exit gracefully.
0
- # The default value is 0, which means that there is no limit.
0
- #
0
   # <b>Exceptions:</b>
0
   # - InvalidPath: +app_root+ doesn't appear to be a valid Ruby on Rails application root.
0
   # - VersionNotFound: The Ruby on Rails framework version that the given application requires
0
@@ -138,16 +133,7 @@ class SpawnManager < AbstractServer
0
     if !options["app_root"]
0
       raise ArgumentError, "The 'app_root' option must be given."
0
     end
0
- options = {
0
- "lower_privilege" => true,
0
- "lowest_user" => "nobody",
0
- "environment" => "production",
0
- "app_type" => "rails",
0
- "spawn_method" => "smart-lv2",
0
- "framework_spawner_timeout" => -1,
0
- "app_spawner_timeout" => -1,
0
- "memory_limit" => 0
0
- }.merge(options)
0
+ options = sanitize_spawn_options(options)
0
     
0
     if options["app_type"] == "rails"
0
       if !defined?(Railz::FrameworkSpawner)
0
@@ -287,12 +273,7 @@ private
0
   end
0
   
0
   def handle_spawn_application(*options)
0
- options = Hash[*options]
0
- options["lower_privilege"] = options["lower_privilege"] == "true"
0
- options["framework_spawner_timeout"] = options["framework_spawner_timeout"].to_i
0
- options["app_spawner_timeout"] = options["app_spawner_timeout"].to_i
0
- options["memory_limit"] = options["memory_limit"].to_i
0
-
0
+ options = sanitize_spawn_options(Hash[*options])
0
     app = nil
0
     app_root = options["app_root"]
0
     app_type = options["app_type"]
...
348
349
350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351
352
353
...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
0
@@ -348,6 +348,23 @@ protected
0
     end
0
   end
0
   
0
+ def sanitize_spawn_options(options)
0
+ defaults = {
0
+ "lower_privilege" => true,
0
+ "lowest_user" => "nobody",
0
+ "environment" => "production",
0
+ "app_type" => "rails",
0
+ "spawn_method" => "smart-lv2",
0
+ "framework_spawner_timeout" => -1,
0
+ "app_spawner_timeout" => -1
0
+ }
0
+ options = defaults.merge(options)
1
+ options["lower_privilege"] = options["lower_privilege"] == "true"
0
+ options["framework_spawner_timeout"] = options["framework_spawner_timeout"].to_i
0
+ options["app_spawner_timeout"] = options["app_spawner_timeout"].to_i
0
+ return options
0
+ end
0
+
0
   # Returns the directory in which to store Phusion Passenger-specific
0
   # temporary files. If +create+ is true, then this method creates the
0
   # directory if it doesn't exist.

Comments