Earlier I switched one of the Litespeed web server virtual hosts from production mode to staging mode and after gracefully restarting rails was receiving a 503 Service Unavailable error. Initially I thought that maybe Litespeed had to be restarted completely instead of just a graceful restart but that produced the same results. Below I describe the errors including errors in the Litespeed logs as well as how to resolve the issue which ended up being a silly mistake on my part.
503 Service Unavailable Error After Switching Rails Site To Staging Mode:
After receiving the above 503 service unavailable error after switching the rails environment mode I looked in the Litespeed logs to be pointed in the right direction. The below text was obtained from the Litespeed logs when attempting to connect to the rails site that was switched from a production environment to a staging environment.
Litespeed Stderr Log After Rails Site Switched To Staging Environment Mode:
- 2010-04-14 09:49:24.474 [STDERR] /home/web/site2/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:218:in `establish_connection': staging database is not configured (ActiveRecord::AdapterNotSpecified)
- from /home/web/site2/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:209:in `establish_connection'
- from ./config/../vendor/rails/railties/lib/initializer.rb:332:in `initialize_database'
- from ./config/../vendor/rails/railties/lib/initializer.rb:120:in `process'
- from ./config/../vendor/rails/railties/lib/initializer.rb:93:in `send'
- from ./config/../vendor/rails/railties/lib/initializer.rb:93:in `run'
- from ./config/environment.rb:16
- from /var/lsws/fcgi-bin/RailsRunner.rb:68:in `require'
- from /var/lsws/fcgi-bin/RailsRunner.rb:68
As seen in the above error the issue is database related. In this scenario we are using PostgreSQL for the database however the issue was not database specific and instead was a silly mistake of not including staging as an option in the database.yml file. Below I show the initial database.yml without the staging environment variable configured and then the updated database.yml file with the staging environment variable configured.
Ruby Project Without Staging Environment Set In database.yml File:
- development:
- adapter: postgresql
- database: dbname
- username: dbuser
- password: dbpass
- host: localhost
- production:
- adapter: postgresql
- database: dbname
- username: dbuser
- password: dbpass
- host: localhost
Ruby Project With Staging Environment Set In database.yml File:
- development:
- adapter: postgresql
- database: dbname
- username: dbuser
- password: dbpass
- host: localhost
- production:
- adapter: postgresql
- database: dbname
- username: dbuser
- password: dbpass
- host: localhost
- staging:
- adapter: postgresql
- database: dbname
- username: dbuser
- password: dbpass
- host: localhost
This is why it is always good to include all three possible Rails environment modes in your database.yml file from the start. Just in case you change your Ruby/Rails project to run in a different environment mode such as development, staging, or production. So after adding the staging configuration to the database.yml file the site started working without issue.