Skip to content

Need a way to restart server on important environment variable changes #420

Closed
@joshuaflanagan

Description

@joshuaflanagan

A lot of people have been bitten by the fact that spring doesn't reload application config (database.yml, config/initializers) with new environment variables. DATABASE_URL is a perfect example. There is a PR that attempts to solve part of the problem (#267), but even the comments there indicate there is still room for confusing behavior.

We need a way to specify a set of environment variables that should trigger a full reload. By default, it may just include DATABASE_URL, but you should be able to add your own environment variables to that list, in case you use them in initializers.

I tried diving into the code to create a PR, but don't have a strong enough understanding of the different environments in play. I wrote a failing test that demonstrates the problem.

test "changing specific environment variables should reload all configuration" do
  File.write(app.path('config/initializers/set_foo.rb'), <<-CONFIG)
     Rails.application.config.foo = ENV['FOO']
  CONFIG

  app.env['FOO'] = 'first'
  assert_success "bin/rails runner 'p Rails.application.config.foo'", stdout: "first"

  app.env['FOO'] = 'second'
  assert_success "bin/rails runner 'p Rails.application.config.foo'", stdout: "second"
end

As an example, I can get this to pass by making the following change:

# lib/spring/client/run.rb
def call
  if true # check if important environment variable changed
    stop_server
    cold_run
  elsif env.server_running?
    warm_run
  else
    cold_run
  end
rescue Errno::ECONNRESET
  exit 1
ensure
  server.close if @server
end

Obviously, that isn't what we want, since it reboots the server every time. But if we could detect the ENV change at that point, we could conditionally restart everything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions