Spring Documentation

repository·main·Indexed 25 days ago

https://github.com/rails/spring

A Rails application preloader designed to speed up development by keeping the application running in the background. It reduces overhead for commands such as tests, migrations, and rake tasks. Includes guides on installation, binstub generation, process management, and configuration via ~/.spring.rb and config/spring.rb.

Tokens
3.8K
Snippets
11
Records
40
Agent score
83%

What's inside Spring

  1. Run Spring commands with different environments

    main

    Spring automatically detects environment changes. If you run a command with a different environment (e.g., using RAILS_ENV), Spring will boot that specific environment.

    For rake tasks, they run in the development environment by default, but you can change this using the RAILS_ENV environment variable.

  2. Install and Setup Spring

    main

    Spring is a Rails application preloader that keeps your application running in the background to speed up development tasks like running tests, rake tasks, or migrations.

    To set it up:

    1. Add spring to your Gemfile in the :development group.
    2. Run bundle install.
    3. 'Springify' your bin/ directory by running bundle exec spring binstub --all. This generates a bin/spring executable and modifies existing executables to hook into Spring.

    Note: Using gem "spring", git: "..." is not supported.

    gem "spring", group: :development
    $ bundle install
    $ bundle exec spring binstub --all
  3. Prevent Spring installation in production

    main

    Spring should not be installed in production environments. To ensure it is excluded during deployment, configure Bundler to skip the development and test groups before running bundle install on your production machine.

    $ bundle config set without 'development test'
    $ bundle install
  4. Use Spring without adding it to the Gemfile

    main

    You can use Spring without checking it into your source repository by installing it as a system gem and prefixing your commands with spring instead of using binstubs.

    Note: Using Spring binstubs without adding Spring to the Gemfile is not supported.

  5. How the Spring client executes commands

    main

    The Spring client manages the execution of commands within a preloaded environment. When you run a command via Spring, it follows these behaviors:

    1. Warm Run: If a Spring server is already running, the client connects to the existing server and executes the command within the preloaded process.
    2. Cold Run: If no server is running (or if the connection fails), the client boots a new server, waits for it to become available, and then executes the command.
    3. Command Discovery: If a command was installed (e.g., a new gem providing a Spring command) after the server started, the client will automatically stop the existing server and perform a cold_run to ensure the new command is available.
    4. Version Mismatch: If the client version and the server version do not match, the client attempts to restart the server to resolve the mismatch.
    5. Signal Forwarding: The client forwards specific signals to the application process, including INT, QUIT, USR1, USR2, INFO, and WINCH.
  6. Set the application root for non-standard setups

    main

    If you are working on a project with a non-standard setup (such as a Rails engine), you must manually specify the application root in a config/spring.rb file relative to the engine's root directory.

    Spring.application_root = './test/dummy'
  7. Configure Spring Reloading

    main

    Spring relies on Rails' application reloading mechanism. To ensure it works correctly, you must ensure config.enable_reloading is set to true in your environment configurations (e.g., config/environments/test.rb).

    For Rails versions before 7.1: You must set cache_classes to false instead.

  8. Enable quiet mode to hide Spring messages

    main

    To disable the "Running via Spring preloader" message, you can either:

    1. Set Spring.quiet = true in ~/.spring.rb or config/spring.rb.
    2. Set the SPRING_QUIET environment variable before executing Spring commands.
  9. Configure Spring loading order and custom settings

    main

    Spring loads configuration from two main files:

    1. ~/.spring.rb: Loaded before Bundler. Use this to require spring-commands-* gems that you want available in all projects without adding them to every project's Gemfile.
    2. config/spring.rb: Loaded after Bundler. Use this for project-specific settings.

    Additionally, config/spring_client.rb is loaded before Bundler and before a server process starts; it can be used to add new top-level commands.

  10. Debug Spring by running the server explicitly

    main

    If you need to see detailed information about Spring's activity, run the server explicitly in a separate terminal. Logging output will be printed to stdout, or you can redirect it to a file using the SPRING_LOG environment variable.

    $ spring server
  11. Use Spring.after_fork to run code after forking

    main

    To run code after Spring has forked the process but before the actual command is executed (e.g., for connecting to external services, performing cleanup, or setting up dynamic configuration), use the Spring.after_fork callback. You can register multiple callbacks by calling the method multiple times.

    Spring.after_fork do
      # run arbitrary code
    end