Passionate about technologies, innovations.

Use Unicorn as Rails 3 Development Server

In order to simulate production setup, for example, allow multiple request process running simultaneously, we want to serve content using Unicorn in development.

If you are using thin server, it already has a built-in command: rails server thin.

Here's a quick guide of running Unicorn instead of default Rails WEBrick server under development mode. You will be able to execute rake server to start a development server with standard Rails development log output in STDOUT.

Gemfile

Fisrt make sure that unicorn is included in Rails Gemfile, also make sure running bundle update to update gem dependencies.

Create a Rake Task

Create a new rake task file under Rails directory lib/tasks with a sensible name, for example dev_server.rake. Unicorn ships unicorncommand for Rails specifically, it's simple to write a server task:

400: Invalid request

Refine Terminal Logs

Starting the server by running rake server or simply rake s at this stage, you will notice that terminal only contains unicorn logs, request logs are missing. This is because Rails writes log to a file instead of STDOUT by default. Adding the following lines in config/environments/development.rb to produce request logs in terminal:

400: Invalid request

Multiple IO Logs

Right now Rails only support assigning one logger in configuration. If the desire is to log Unicorn and Rails log to both STDOUT and file, a custom mulitple stream writer is required. Here's how you can achieve it. The following example shows an improved development.rb configuraiton working with a custom MultipleIO writer class.

400: Invalid request

References

Comments