Method: Spring::Application#serve

Defined in:
lib/spring/application.rb

#serve(client) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/spring/application.rb', line 169

def serve(client)
  log "got client"
  manager.puts

  @clients[client] = true

  _stdout, stderr, _stdin = streams = 3.times.map { client.recv_io }
  [STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) }

  if preloaded?
    client.puts(0) # preload success
  else
    begin
      preload
      client.puts(0) # preload success
    rescue Exception
      log "preload failed"
      client.puts(1) # preload failure
      raise
    end
  end

  args, env = JSON.load(client.read(client.gets.to_i)).values_at("args", "env")
  command   = Spring.command(args.shift)

  connect_database
  setup command

  if Rails.application.reloaders.any?(&:updated?)
    Rails.application.reloader.reload!
  end

  pid = fork {
    # Make sure to close other clients otherwise their graceful termination
    # will be impossible due to reference from this fork.
    @clients.each_key { |c| c.close if c != client }

    Process.setsid
    IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") }
    trap("TERM", "DEFAULT")

    unless Spring.quiet
      STDERR.puts "Running via Spring preloader in process #{Process.pid}"

      if Rails.env.production?
        STDERR.puts "WARNING: Spring is running in production. To fix "         \
                    "this make sure the spring gem is only present "            \
                    "in `development` and `test` groups in your Gemfile "       \
                    "and make sure you always use "                             \
                    "`bundle install --without development test` in production"
      end
    end

    ARGV.replace(args)
    $0 = command.exec_name

    # Delete all env vars which are unchanged from before Spring started
    original_env.each { |k, v| ENV.delete k if ENV[k] == v }

    # Load in the current env vars, except those which *were* changed when Spring started
    env.each { |k, v| ENV[k] ||= v }

    connect_database
    srand

    invoke_after_fork_callbacks
    shush_backtraces

    command.call
  }

  disconnect_database

  log "forked #{pid}"
  manager.puts pid

  wait pid, streams, client
rescue Exception => e
  log "exception: #{e}"
  manager.puts unless pid

  if streams && !e.is_a?(SystemExit)
    print_exception(stderr, e)
    streams.each(&:close)
  end

  client.puts(1) if pid
  client.close
ensure
  # Redirect STDOUT and STDERR to prevent from keeping the original FDs
  # (i.e. to prevent `spring rake -T | grep db` from hanging forever),
  # even when exception is raised before forking (i.e. preloading).
  reset_streams
end