@@ -133,12 +133,19 @@ def run
133
133
end
134
134
end
135
135
136
- def set_env_vars ( env )
136
+ def with_env_vars ( env )
137
+ old_env = ENV . to_hash
138
+
139
+ # TODO move env-filtering
137
140
# Delete all env vars which are unchanged from before spring started
138
141
original_env . each { |k , v | ENV . delete k if ENV [ k ] == v }
139
142
140
143
# Load in the current env vars, except those which *were* changed when spring started
141
144
env . each { |k , v | ENV [ k ] ||= v }
145
+
146
+ yield
147
+ ensure
148
+ ENV . replace ( old_env . to_hash )
142
149
end
143
150
144
151
def serve ( client )
@@ -150,41 +157,42 @@ def serve(client)
150
157
151
158
client_args , client_env = JSON . load ( client . read ( client . gets . to_i ) ) . values_at ( "args" , "env" )
152
159
153
- preload unless preloaded?
154
- command = Spring . command ( client_args . shift )
160
+ with_env_vars ( client_env ) do
161
+ preload unless preloaded?
162
+ command = Spring . command ( client_args . shift )
155
163
156
- connect_database
157
- setup command
164
+ connect_database
165
+ setup command
158
166
159
- if Rails . application . reloaders . any? ( &:updated? )
160
- ActionDispatch ::Reloader . cleanup!
161
- ActionDispatch ::Reloader . prepare!
162
- end
167
+ if Rails . application . reloaders . any? ( &:updated? )
168
+ ActionDispatch ::Reloader . cleanup!
169
+ ActionDispatch ::Reloader . prepare!
170
+ end
163
171
164
- pid = fork {
165
- IGNORE_SIGNALS . each { |sig | trap ( sig , "DEFAULT" ) }
166
- trap ( "TERM" , "DEFAULT" )
172
+ pid = fork {
173
+ IGNORE_SIGNALS . each { |sig | trap ( sig , "DEFAULT" ) }
174
+ trap ( "TERM" , "DEFAULT" )
167
175
168
- ARGV . replace ( client_args )
169
- $0 = command . process_title
170
- set_env_vars ( client_env )
176
+ ARGV . replace ( client_args )
177
+ $0 = command . process_title
171
178
172
- # requiring is faster, so if config.cache_classes was true in
173
- # the environment's config file, then we can respect that from
174
- # here on as we no longer need constant reloading.
175
- if @original_cache_classes
176
- ActiveSupport ::Dependencies . mechanism = :require
177
- Rails . application . config . cache_classes = true
178
- end
179
+ # requiring is faster, so if config.cache_classes was true in
180
+ # the environment's config file, then we can respect that from
181
+ # here on as we no longer need constant reloading.
182
+ if @original_cache_classes
183
+ ActiveSupport ::Dependencies . mechanism = :require
184
+ Rails . application . config . cache_classes = true
185
+ end
179
186
180
- connect_database
181
- srand
187
+ connect_database
188
+ srand
182
189
183
- invoke_after_fork_callbacks
184
- shush_backtraces
190
+ invoke_after_fork_callbacks
191
+ shush_backtraces
185
192
186
- command . call
187
- }
193
+ command . call
194
+ }
195
+ end
188
196
189
197
disconnect_database
190
198
reset_streams
0 commit comments