Skip to content

Commit 37df90e

Browse files
committed
Add #with_env_vars
1 parent 8c38efa commit 37df90e

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

lib/spring/application.rb

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,19 @@ def run
133133
end
134134
end
135135

136-
def set_env_vars(env)
136+
def with_env_vars(env)
137+
old_env = ENV.to_hash
138+
139+
# TODO move env-filtering
137140
# Delete all env vars which are unchanged from before spring started
138141
original_env.each { |k, v| ENV.delete k if ENV[k] == v }
139142

140143
# Load in the current env vars, except those which *were* changed when spring started
141144
env.each { |k, v| ENV[k] ||= v }
145+
146+
yield
147+
ensure
148+
ENV.replace(old_env.to_hash)
142149
end
143150

144151
def serve(client)
@@ -150,41 +157,42 @@ def serve(client)
150157

151158
client_args, client_env = JSON.load(client.read(client.gets.to_i)).values_at("args", "env")
152159

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)
155163

156-
connect_database
157-
setup command
164+
connect_database
165+
setup command
158166

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
163171

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")
167175

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
171178

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
179186

180-
connect_database
181-
srand
187+
connect_database
188+
srand
182189

183-
invoke_after_fork_callbacks
184-
shush_backtraces
190+
invoke_after_fork_callbacks
191+
shush_backtraces
185192

186-
command.call
187-
}
193+
command.call
194+
}
195+
end
188196

189197
disconnect_database
190198
reset_streams

test/acceptance/app_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def binstub_prelude
292292
end
293293

294294
test "config via environment variable" do
295-
File.write(application.path('config/initializers/set_foo.rb', <<-CONFIG)
295+
File.write(application.path('config/initializers/set_foo.rb'), <<-CONFIG)
296296
Rails.application.config.foo = !!ENV['FOO']
297297
CONFIG
298298

0 commit comments

Comments
 (0)