<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
  <id>http://michaelvanrooijen.com/</id>
  <title>Michael van Rooijen</title>
  <updated>2011-07-22T22:00:00Z</updated>
  <link rel="alternate" href="http://michaelvanrooijen.com/" />
  
  <author>
    <name>Michael van Rooijen</name>
    <uri>http://twitter.com/meskyanichi</uri>
  </author>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/michaelvanrooijen" /><feedburner:info uri="michaelvanrooijen" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <id>tag:michaelvanrooijen.com,2011-07-23:/articles/2011/07/23-deploying-your-static-nanoc-sites-to-herokus-hosted-platform-with-nanoc-heroku/</id>
    <title type="html">Deploying your static nanoc sites to Heroku's hosted platform with Nanoc::Heroku</title>
    <published>2011-07-22T22:00:00Z</published>
    <updated>2011-07-22T22:00:00Z</updated>
    <link rel="alternate" href="http://feedproxy.google.com/~r/michaelvanrooijen/~3/ZjErIvotReY/" />
    <content type="html">&lt;p&gt;So you&amp;#8217;ve created a new application and would like to launch it. It is a Ruby application, but you created the front pages using &lt;a href='http://nanoc.stoneship.org/'&gt;nanoc&lt;/a&gt;, a static site generator. Your Ruby application will be deployed to Heroku&amp;#8217;s platform, but where will your static site run? A separate VPS? A shared host?&lt;/p&gt;

&lt;p&gt;I personally dislike the idea of having to deploy parts of my application (the front pages, the application itself) to different servers/platforms. Shared hosting sucks, so let&amp;#8217;s assume we get a VPS to host our static front pages. First of all, adding a new VPS costs money. You have to then set up a web server, and ensure it stays up. You have to keep the server up to date with security patches, and if things go wrong, then you&amp;#8217;ll have to solve these issues yourself. All that for a simple static website.&lt;/p&gt;

&lt;p&gt;I enjoy deploying applications to &lt;a href='http://heroku.com/'&gt;Heroku&lt;/a&gt;. It&amp;#8217;s easy, it&amp;#8217;s fast, it&amp;#8217;s performant, it&amp;#8217;s secure, it&amp;#8217;s managed and you go from &amp;#8220;nothing&amp;#8221; to &amp;#8220;deployed&amp;#8221; in seconds. Wouldn&amp;#8217;t it be nice if we could quickly create a static site using a great site development tool, and (optionally) leverage other tools like haml, compass, coffeescript, then render the html, and deploy it to Heroku, for free?&lt;/p&gt;

&lt;h2 id='nanocheroku'&gt;Nanoc::Heroku&lt;/h2&gt;

&lt;p&gt;Heroku currently supports Ruby, Node and Clojure, but not (directly) raw HTML sites. However, that doesn&amp;#8217;t mean that we can&amp;#8217;t use a little bit of Ruby and Rack to get around it.&lt;/p&gt;

&lt;p&gt;&lt;a href='https://github.com/meskyanichi/nanoc-heroku'&gt;Nanoc::Heroku&lt;/a&gt; is a little bare-bones nanoc application/structure that I set up. It is not a gem, you simply clone it from the repository and immediately get to work with a few commands. Let me highlight a few interesting points:&lt;/p&gt;

&lt;p&gt;It has out-of-the-box support for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HAML / SASS&lt;/li&gt;

&lt;li&gt;Compass&lt;/li&gt;

&lt;li&gt;CoffeeScript&lt;/li&gt;

&lt;li&gt;Markdown&lt;/li&gt;

&lt;li&gt;Builder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It auto-compiles your site when adding/updating files using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Guard&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It contains rake tasks for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimizing Images of format: JPG and PNG&lt;/li&gt;

&lt;li&gt;Compressing Stylesheets and Javascripts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It deploys to Heroku with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rake deploy&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;after changing a constant in the &lt;code&gt;Rakefile&lt;/code&gt; and running &lt;code&gt;heroku create mysite --stack cedar&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id='getting_started'&gt;Getting started&lt;/h2&gt;

&lt;p&gt;To get up and running quickly, all you have to do is clone the git repository, remove the origin remote, install the bundled gems, start guard and the nanoc server. Here&amp;#8217;s how:&lt;/p&gt;

&lt;p&gt;Clone the repository, remove remote, install dependencies:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  git clone git&lt;span class='sy'&gt;:/&lt;/span&gt;/github.com/meskyanichi/nanoc-heroku.git my_site_name
  cd my_site_name &amp;amp;&amp;amp; git remote rm origin &amp;amp;&amp;amp; bundle
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the current shell, run the following to do the initial compiling and start the server afterwards:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  nanoc compile &amp;amp;&amp;amp; nanoc view
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And finally, open a second shell tab and run the following as well:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  guard
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Visit &lt;code&gt;http://localhost:3000/&lt;/code&gt; and your site should be up and running. Every time you add/update a file inside the &lt;code&gt;./content&lt;/code&gt; directory, Guard will run &lt;code&gt;nanoc compile&lt;/code&gt; to deliver a fresh state of the latest changes, rendered in &lt;code&gt;./output&lt;/code&gt;. These will be immediately visible on &lt;code&gt;http://localhost:3000/&lt;/code&gt; after a browser refresh.&lt;/p&gt;

&lt;p&gt;Refer to the &lt;a href='http://nanoc.stoneship.org/docs/'&gt;nanoc documentation&lt;/a&gt; for more in-depth information about the static site generator itself.&lt;/p&gt;

&lt;h2 id='deploying_to_heroku'&gt;Deploying to Heroku&lt;/h2&gt;

&lt;p&gt;First of all, let&amp;#8217;s create the Heroku application. It must be running on the Celadon Cedar stack. Note: The Heroku gem updates very frequently, so it&amp;#8217;s a good idea to run &lt;code&gt;gem install heroku&lt;/code&gt; once in a while to ensure you&amp;#8217;re on the latest version.&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  gem install heroku
  heroku create mysite --stack cedar
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will add the &lt;code&gt;heroku&lt;/code&gt; remote to your git repository. Next, open up your &lt;code&gt;Rakefile&lt;/code&gt;, it contains a constant called &lt;code&gt;BASE_URL&lt;/code&gt;. The value of the &lt;code&gt;BASE_URL&lt;/code&gt; should be your domain url. For example: &lt;code&gt;http://github.com&lt;/code&gt;. This is required because when you run the deployment task, it will update the &lt;code&gt;config.yaml&lt;/code&gt; to ensure it compiles with the proper URL&amp;#8217;s in your website, so your website&amp;#8217;s link to link to &lt;code&gt;http://localhost:3000/&lt;/code&gt; urls.&lt;/p&gt;

&lt;p&gt;Now, before we deploy we need to install the image optimization utilities, and the Java runtime for the CSS/JS compressor. On OSX using Homebrew, you can install the image optimization utilities by running:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  brew install jpegoptim optipng
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;On Linux (Ubuntu) with &lt;code&gt;apt-get&lt;/code&gt; or &lt;code&gt;aptitude&lt;/code&gt;, the equivalent is:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  apt-get install jpegoptim optipng
  aptitude install jpegoptim optipng
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;master&lt;/code&gt; branch will always be used to deploy your website. You could tweak the Rakefile to change this behavior. Ensure that you are working in a clean working directory (that there is nothing to commit). Once you&amp;#8217;re ready to deploy the master branch to Heroku, simply issue the following command:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  rake deploy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will create a new branch called &lt;code&gt;deployment&lt;/code&gt;, and checkout to it. It will then take the value of the BASE_URL constant (which is defined in the Rakefile) and apply it to the &lt;code&gt;config.yaml&lt;/code&gt; file and will re-compile the whole website. It will remove the &lt;code&gt;output&lt;/code&gt; entry from your &lt;code&gt;.gitignore&lt;/code&gt; as well so it can commit the raw files. After that it&amp;#8217;ll commit these changes to the &lt;code&gt;deployment&lt;/code&gt; branch and push it to Heroku. Once deployed, it will checkout back to the &lt;code&gt;master&lt;/code&gt; branch and remove the &lt;code&gt;deployment&lt;/code&gt; branch again. You are now back at the state you were before you ran &lt;code&gt;rake deploy&lt;/code&gt;, except that you actually deployed your website to Heroku.&lt;/p&gt;

&lt;p&gt;You don&amp;#8217;t have to worry about this procedure though. Just sit on a clean master branch and run the Rake task. If the master branch isn&amp;#8217;t clean, it will tell you upon initializing the Rake task and abort the operation.&lt;/p&gt;

&lt;p&gt;And That&amp;#8217;s it! Your static site should now be running on Heroku! Ensure the &lt;code&gt;web&lt;/code&gt; process is running. You can check the process list by running &lt;code&gt;heroku ps&lt;/code&gt;. If it isn&amp;#8217;t listed, be sure to run &lt;code&gt;heroku ps:scale web=1&lt;/code&gt;. When you see that there is one process running, run &lt;code&gt;heroku open&lt;/code&gt; to open up your browser to view the application.&lt;/p&gt;

&lt;p&gt;You can of course use this for any static site, not just for front pages of your applications. You could use it for a blog, or other things. So far I have already moved this blog, as well as the &lt;a href='http://hirefireapp.com/'&gt;HireFire&lt;/a&gt; front-page (both of which are using Nanoc::Heroku) to Heroku, and I&amp;#8217;ve been very satisfied with the migration!&lt;/p&gt;</content>
    <summary type="html">So you created a static website using nanoc, the static site generator. But to where could you deploy it? The most obvious choice would be a VPS. However, using Nanoc::Heroku you can deploy to your static website to Heroku's performant and managed platform for free. Keep reading to find out how.</summary>
  <feedburner:origLink>http://michaelvanrooijen.com/articles/2011/07/23-deploying-your-static-nanoc-sites-to-herokus-hosted-platform-with-nanoc-heroku/</feedburner:origLink></entry>
  <entry>
    <id>tag:michaelvanrooijen.com,2011-06-19:/articles/2011/06/19-automate-your-applications-development-environment-with-guard/</id>
    <title type="html">Automate your application's development environment with Guard</title>
    <published>2011-06-18T22:00:00Z</published>
    <updated>2011-06-18T22:00:00Z</updated>
    <link rel="alternate" href="http://feedproxy.google.com/~r/michaelvanrooijen/~3/BhWXWxZdKTg/" />
    <content type="html">&lt;p&gt;Do you find yourself repeating various actions over and over again in your development environment? Examples of such actions are restarting your application server for your Rails app every time you update a files that are loaded in memory and require a server restart for the changes to take effect. Maybe you are using Nanoc, a static site generator and would like it to automatically recompile your site every time a file is saved. Or maybe you are using a testing library such as RSpec and want to automatically run your tests every time you save a Ruby file.&lt;/p&gt;

&lt;p&gt;There are lots of things that could be automated. Some tools come with built-in utilities to take care of this (think of autotest, nanoc autocompile, etc), but they will all require their own separate terminal window.&lt;/p&gt;

&lt;h2 id='enter_guard'&gt;Enter Guard&lt;/h2&gt;

&lt;p&gt;&lt;a href='https://github.com/guard/guard'&gt;Guard&lt;/a&gt; is a RubyGem, written by &lt;a href='https://github.com/thibaudgg'&gt;Thibaud Guillaume-Gentil&lt;/a&gt; (&lt;a href='http://twitter.com/#!/thibaudgg'&gt;@thibaudgg&lt;/a&gt;), that you could use to easily automate parts of your application&amp;#8217;s development environment.&lt;/p&gt;

&lt;p&gt;To show you what it can do, here&amp;#8217;s an example: Let&amp;#8217;s say we have a Ruby on Rails application, using Bundler to manage gem dependencies and RSpec as our testing library. Each of these bring with them repetitive actions. So let&amp;#8217;s automate them.&lt;/p&gt;

&lt;p&gt;We could use the following gems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Guard::Rails&lt;/li&gt;

&lt;li&gt;Guard::Bundler&lt;/li&gt;

&lt;li&gt;Guard::RSpec&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These gems are basically Guard &amp;#8220;modules&amp;#8221;. Simply add them to your Gemfile. Now create a &lt;code&gt;Guardfile&lt;/code&gt; in your application&amp;#8217;s root directory and add the following.&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  guard &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;rails&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class='r'&gt;do&lt;/span&gt;
    watch(&lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;Gemfile.lock&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;)
    watch(&lt;span class='rx'&gt;&lt;span class='dl'&gt;%r{&lt;/span&gt;&lt;span class='k'&gt;^(config|lib)/.*&lt;/span&gt;&lt;span class='dl'&gt;}&lt;/span&gt;&lt;/span&gt;)
  &lt;span class='r'&gt;end&lt;/span&gt;

  guard &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;rspec&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class='r'&gt;do&lt;/span&gt;
    watch(&lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;spec/spec_helper.rb&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;) { &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; }
    watch(&lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;config/routes.rb&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;) { &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec/routing&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; }
    watch(&lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;app/controllers/application_controller.rb&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;) {
      &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec/controllers&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
    }
    watch(&lt;span class='rx'&gt;&lt;span class='dl'&gt;%r{&lt;/span&gt;&lt;span class='k'&gt;^spec/.+_spec&lt;/span&gt;&lt;span class='ch'&gt;\.&lt;/span&gt;&lt;span class='k'&gt;rb&lt;/span&gt;&lt;span class='dl'&gt;}&lt;/span&gt;&lt;/span&gt;)
    watch(&lt;span class='rx'&gt;&lt;span class='dl'&gt;%r{&lt;/span&gt;&lt;span class='k'&gt;^app/(.+)&lt;/span&gt;&lt;span class='ch'&gt;\.&lt;/span&gt;&lt;span class='k'&gt;rb&lt;/span&gt;&lt;span class='dl'&gt;}&lt;/span&gt;&lt;/span&gt;) { |m| &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec/&lt;/span&gt;&lt;span class='il'&gt;&lt;span class='idl'&gt;#{&lt;/span&gt;m[&lt;span class='i'&gt;1&lt;/span&gt;]&lt;span class='idl'&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class='k'&gt;_spec.rb&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; }
    watch(&lt;span class='rx'&gt;&lt;span class='dl'&gt;%r{&lt;/span&gt;&lt;span class='k'&gt;^lib/(.+)&lt;/span&gt;&lt;span class='ch'&gt;\.&lt;/span&gt;&lt;span class='k'&gt;rb&lt;/span&gt;&lt;span class='dl'&gt;}&lt;/span&gt;&lt;/span&gt;) { |m| &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec/lib/&lt;/span&gt;&lt;span class='il'&gt;&lt;span class='idl'&gt;#{&lt;/span&gt;m[&lt;span class='i'&gt;1&lt;/span&gt;]&lt;span class='idl'&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class='k'&gt;_spec.rb&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; }
    watch(&lt;span class='rx'&gt;&lt;span class='dl'&gt;%r{&lt;/span&gt;&lt;span class='k'&gt;^app/controllers/(.+)_(controller)&lt;/span&gt;&lt;span class='ch'&gt;\.&lt;/span&gt;&lt;span class='k'&gt;rb&lt;/span&gt;&lt;span class='dl'&gt;}&lt;/span&gt;&lt;/span&gt;) { |m|
      [&lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec/routing/&lt;/span&gt;&lt;span class='il'&gt;&lt;span class='idl'&gt;#{&lt;/span&gt;m[&lt;span class='i'&gt;1&lt;/span&gt;]&lt;span class='idl'&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class='k'&gt;_routing_spec.rb&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
      &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec/&lt;/span&gt;&lt;span class='il'&gt;&lt;span class='idl'&gt;#{&lt;/span&gt;m[&lt;span class='i'&gt;2&lt;/span&gt;]&lt;span class='idl'&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class='k'&gt;s/&lt;/span&gt;&lt;span class='il'&gt;&lt;span class='idl'&gt;#{&lt;/span&gt;m[&lt;span class='i'&gt;1&lt;/span&gt;]&lt;span class='idl'&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class='k'&gt;_&lt;/span&gt;&lt;span class='il'&gt;&lt;span class='idl'&gt;#{&lt;/span&gt;m[&lt;span class='i'&gt;2&lt;/span&gt;]&lt;span class='idl'&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class='k'&gt;_spec.rb&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
      &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;spec/acceptance/&lt;/span&gt;&lt;span class='il'&gt;&lt;span class='idl'&gt;#{&lt;/span&gt;m[&lt;span class='i'&gt;1&lt;/span&gt;]&lt;span class='idl'&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class='k'&gt;_spec.rb&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;]
    }
  &lt;span class='r'&gt;end&lt;/span&gt;

  guard &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;bundler&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class='r'&gt;do&lt;/span&gt;
    watch(&lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;Gemfile&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class='r'&gt;end&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;#8217;s go over what the above Guardfile + Guard Gems will do for us.&lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;Guard::Rails&lt;/strong&gt;, if a file in the &lt;code&gt;config&lt;/code&gt; or &lt;code&gt;lib&lt;/code&gt; directories are updated, or the &lt;code&gt;Gemfile.lock&lt;/code&gt; file gets updated, the rails app server will automatically be restarted. Normally, you would have to open the shell manually and restart the process yourself. With Guard::Rails, this will be done for you.&lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;Guard::RSpec&lt;/strong&gt;, when you save for example a controller file, only that specific controller&amp;#8217;s spec file will be run, rather than running all the tests. The same goes for models. You can customize this behavior to suit your needs. Another cool feature, which is particularly interesting when you&amp;#8217;re working on gems that need to support multiple Ruby versions, is that you can pass in a hash of options to the &lt;code&gt;guard &amp;#39;rspec&amp;#39;&lt;/code&gt; method in the &lt;code&gt;Guardfile&lt;/code&gt;. For example if you want to run the tests against MRI 1.9.2, 1.8.7, REE 1.8.7, JRuby and Rubinius, and you are using RVM, you could simply define the following:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  guard &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;rspec&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class='sy'&gt;:rvm&lt;/span&gt; =&amp;gt; [&lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;1.9.2&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;1.8.7&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;ree&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;jruby&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;rbx&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;] &lt;span class='r'&gt;do&lt;/span&gt;
    ...
  &lt;span class='r'&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now every time Guard runs your tests, it&amp;#8217;ll run them against the all defined Ruby versions and implementations.&lt;/p&gt;

&lt;p&gt;With &lt;strong&gt;Guard::Bundler&lt;/strong&gt;, it simply watches for changes in the &lt;code&gt;Gemfile&lt;/code&gt;, and will re-bundle it when changes are made. Rather than having to manually go in to the terminal again to run &lt;code&gt;bundle install&lt;/code&gt;, this will be done for you. Also, notice that the Guard::Rails has this line defined: &lt;code&gt;watch(&amp;#39;Gemfile.lock&amp;#39;)&lt;/code&gt;. When Guard::Bundler runs the &lt;code&gt;bundle install&lt;/code&gt; command, it&amp;#8217;ll update the &lt;code&gt;Gemfile.lock&lt;/code&gt; file, which means that dependencies have changed and that the Rails server will also be restarted automatically for you by Guard::Rails. Again, no need to go in to your terminal to restart it manually.&lt;/p&gt;

&lt;p&gt;Another useful feature is that it supports growl notifications (or libnotify on Linux). So you could have a small notification pop up on your screen when tests run, or when the Rails server has been restarted, without having to check the shell.&lt;/p&gt;

&lt;p&gt;Guard is very modular, and there are &lt;a href='https://github.com/guard/guard/wiki/List-of-available-Guards'&gt;a bunch of modules&lt;/a&gt; available right out of the box that automate particular parts of your development environment. It is also easy to write your own Guard modules, refer to &lt;a href='https://github.com/guard/guard'&gt;Guard&amp;#8217;s README on GitHub&lt;/a&gt; for more information.&lt;/p&gt;</content>
    <summary type="html">As applications become more complex, you might find yourself repeating particular actions over and over again in your development environment. Learn how you can automate such actions using the Guard RubyGem.</summary>
  <feedburner:origLink>http://michaelvanrooijen.com/articles/2011/06/19-automate-your-applications-development-environment-with-guard/</feedburner:origLink></entry>
  <entry>
    <id>tag:michaelvanrooijen.com,2011-06-08:/articles/2011/06/08-managing-and-monitoring-your-ruby-application-with-foreman-and-upstart/</id>
    <title type="html">Managing and monitoring your Ruby application with Foreman and Upstart</title>
    <published>2011-06-07T22:00:00Z</published>
    <updated>2011-06-07T22:00:00Z</updated>
    <link rel="alternate" href="http://feedproxy.google.com/~r/michaelvanrooijen/~3/l0Gvw8kj6us/" />
    <content type="html">&lt;p&gt;In my &lt;a href='http://michaelvanrooijen.com/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/'&gt;last post&lt;/a&gt; I briefly mentioned a gem called &lt;a href='https://github.com/ddollar/foreman'&gt;Foreman&lt;/a&gt;. Foreman is a gem that allows you to easily organize and horizontally scale your various processes in a centralized manner. For example, in a typical Ruby on Rails application you might have a few different processes: your application server (Thin, Unicorn, etc), a worker (Resque, Delayed Job, etc), a pubsub server (Faye, Juggernaut, etc). Whatever you&amp;#8217;re using, you should be monitoring it to ensure that whatever you spin up, stays up.&lt;/p&gt;

&lt;p&gt;There are various tools available to do this sort of thing. In Ruby you have &lt;a href='https://github.com/mojombo/god'&gt;God&lt;/a&gt; and &lt;a href='https://github.com/arya/bluepill'&gt;Bluepill&lt;/a&gt;, in Python you have &lt;a href='http://supervisord.org/'&gt;Supervisord&lt;/a&gt;, in C you have &lt;a href='http://mmonit.com/monit/'&gt;Monit&lt;/a&gt;, and there are plenty more. The one I want to talk about today however is called &lt;a href='http://upstart.ubuntu.com/'&gt;Upstart&lt;/a&gt;, because Foreman makes it easy to &amp;#8220;export to&amp;#8221; (setup) Upstart in your production environment. It even makes your development environment a lot more pleasant to work with if you have more than one process to run, which is likely the case.&lt;/p&gt;

&lt;p&gt;The reason I started using Foreman is because I often have applications that consist of 3 or more processes. This means that I have to keep 3 or more shell tabs open at all times, and also have to switch between them to see each of their output. With Foreman, this is no longer the case.&lt;/p&gt;

&lt;h2 id='getting_started_in_development'&gt;Getting started in development&lt;/h2&gt;

&lt;p&gt;Foreman is very simple to set up in development. All you have to do is create a file called &lt;code&gt;Procfile&lt;/code&gt; in the root of your application and define your processes inside of it. Here&amp;#8217;s an example Ruby on Rails application using Thin as its app server, Resque as its worker process and Faye as its pubsub server.&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  web:     bundle exec thin start -p $PORT
  worker:  bundle exec rake resque:work
  pubsub:  bundle exec thin start -p $PORT -R ./faye.ru
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With this in place we are now able to start using Foreman. As you can see we&amp;#8217;re executing every command from within the bundled environment using Bundler. This is always a good idea as it isolated each environment and avoids gem version conflicts.&lt;/p&gt;

&lt;p&gt;Installing Foreman is as easy as running &lt;code&gt;gem install foreman&lt;/code&gt;. With Foreman installed, we can now start all our processes with a single command from our application&amp;#8217;s root.&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  foreman start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You&amp;#8217;ll see that Foreman starts up all the processes defined in the &lt;code&gt;Procfile&lt;/code&gt;, and also give each of the processes a different color in the console for readability. It looks something like this:&lt;/p&gt;

&lt;p&gt;&lt;img alt='Foreman Example' src='http://michaelvanrooijen.com/images/articles/foreman.png' /&gt;&lt;/p&gt;

&lt;p&gt;One of the key features of Foreman is the ability to add concurrency to each of the processes by initializing Foreman with additional parameters. In development this might not be too interesting, but in production it sure is.&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  foreman start -c web=4 worker=2 pubsub=1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This would spin up 4 Thin instances, 2 Resque instances and 1 Faye instance. By default Foreman will use port 5000 as a starting point. Then it increments by 1 for the corresponding process, and by 100 for each separate process. In the above code snippet that would mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Thin&lt;/strong&gt; runs on ports 5000 5001 5002 5003&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Resque&lt;/strong&gt; runs on ports 5100 5101&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Faye&lt;/strong&gt; runs on port 5200&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can change the starting port using the &lt;code&gt;-p&lt;/code&gt; option. It must be a 1000-value (1000, 2000, 3000, etc). Refer to Foreman&amp;#8217;s the man page for more information.&lt;/p&gt;

&lt;p&gt;From now on when you want to start up your development environment, all you have to do is to &lt;code&gt;cd in/to/your/app&lt;/code&gt; and run &lt;code&gt;foreman start&lt;/code&gt;. This is really nice, especially if each process has a couple of arguments you would normally need to remember and type in every time to start up your whole environment.&lt;/p&gt;

&lt;h2 id='converting_procfile_to_upstart_configuration_files'&gt;Converting Procfile to Upstart configuration files&lt;/h2&gt;

&lt;p&gt;Our application has now be deployed to our server, but we need to initialize the processes and keep them monitored. Some Linux distributions come with Upstart pre-installed. If yours does not, you can install it from source by downloading it from the official website.&lt;/p&gt;

&lt;p&gt;Assuming your machine does have Upstart installed, we need to write configuration files to the init directory. On Ubuntu, by default, this would mean writing the configuration files to &lt;code&gt;/etc/init&lt;/code&gt;. It also requires you to have root access. If you&amp;#8217;re using Foreman, you&amp;#8217;ve actually already indirectly written the configuration files: your &lt;code&gt;Procfile&lt;/code&gt; in your application&amp;#8217;s root. Now we need to export (convert) that Procfile to Upstart configuration files.&lt;/p&gt;

&lt;p&gt;You can do so by running the following command:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  foreman export upstart /etc/init -a myapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You should then see the following if all went well:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  [foreman export] writing: /etc/init/myapp.conf
  [foreman export] writing: /etc/init/myapp-web.conf
  [foreman export] writing: /etc/init/myapp-web-1.conf
  [foreman export] writing: /etc/init/myapp-worker.conf
  [foreman export] writing: /etc/init/myapp-worker-1.conf
  [foreman export] writing: /etc/init/myapp-pubsub.conf
  [foreman export] writing: /etc/init/myapp-pubsub-1.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you&amp;#8217;re running this command as a non-root user, then you must prefix the command with &lt;code&gt;sudo&lt;/code&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  sudo foreman export upstart /etc/init -a myapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If it says it cannot find the &lt;code&gt;foreman&lt;/code&gt; command, and you&amp;#8217;re using &lt;a href='https://rvm.beginrescueend.com/'&gt;RVM&lt;/a&gt; (Ruby Version Manager), be sure to use &lt;code&gt;rvmsudo&lt;/code&gt; instead of just &lt;code&gt;sudo&lt;/code&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  rvmsudo foreman export upstart /etc/init -a myapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you want to have more concurrency for some processes, you must specify it during export as well:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  foreman export upstart /etc/init -a myapp -c web=3 worker=2 pubsub=1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Be sure to export it for the same user that&amp;#8217;s deploying your application and has write access to your application path. If your deployments user name is &amp;#8220;deployer&amp;#8221;, you should specify it like so:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  foreman export upstart /etc/init -a myapp -u deployer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you don&amp;#8217;t set it to the deployment user, it might be unable to properly start up your application. You can overwrite the configuration files at any time by re-running the &lt;code&gt;foreman export&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s a full example of I&amp;#8217;m using for one of my applications:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  rvmsudo foreman export upstart /etc/init -a myapp -u deployer -c web=3 faye=1 worker=1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id='start_monitoring_all_your_processes'&gt;Start monitoring all your processes&lt;/h2&gt;

&lt;p&gt;Now that the configuration files are in place, all that&amp;#8217;s left is to start the monitor and it&amp;#8217;ll ensure your processes are always up and running, and restore them from crashes if necessary. To tell Upstart to monitor our processes, simply run the following command:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  sudo start myapp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;myapp&lt;/code&gt; is the name of your application you set by specifying the &lt;code&gt;-a&lt;/code&gt; option in the &lt;code&gt;foreman export&lt;/code&gt; command. If you do not specify the &lt;code&gt;-a&lt;/code&gt; option, it&amp;#8217;ll name your application after the name of the root directory where your application resides.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s it. Now all your processes should be up and running! Try to kill off a process using &lt;code&gt;kill &amp;lt;pid&amp;gt;&lt;/code&gt; and you&amp;#8217;ll see that it&amp;#8217;ll be instantly respawned by the Upstart utility!&lt;/p&gt;

&lt;p&gt;If you want Upstart to automatically start up and monitor &amp;#8220;myapp&amp;#8221; when you restart your server, you can add this line at the top of your &lt;code&gt;/etc/init/myapp.conf&lt;/code&gt; file&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  start on runlevel [2345]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With this in place, each time you reboot your server, it&amp;#8217;ll automatically spin up and monitor your application processes by itself so you don&amp;#8217;t have to manually SSH in to do this.&lt;/p&gt;

&lt;p&gt;Additionally you can also bind services other than your application-specific processes to Upstart via your &lt;code&gt;Procfile&lt;/code&gt;, for example: nginx, mysql, postgresql, mongodb, redis, memcached, and so forth. You could also let Upstart handle that for you to ensure each of these is also always up and running, and starts up when your system boots. For example, let&amp;#8217;s say we have Unicorn as our app server, Resque as our worker library, NGINX as our web server and MongoDB as our database. We would set up our &lt;code&gt;Procfile&lt;/code&gt; to look something like this:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  web:    bundle exec unicorn -p $PORT -E production
  worker: bundle exec rake resque:work -e production
  mongo:  sudo /etc/mongodb/bin/mongod --journal
  nginx:  sudo /usr/local/sbin/nginx -c /etc/nginx/conf/nginx.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With that you can then re-export that to Upstart and run &lt;code&gt;restart myapp&lt;/code&gt;. Upstart will now also start up and monitor your MongoDB and NGINX instances. Some of the commands that become available when exporting to Upstart are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;start myapp&lt;/li&gt;

&lt;li&gt;stop myapp&lt;/li&gt;

&lt;li&gt;restart myapp&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These will start/stop/restart all processes (web, worker, mongo, nginx). If you only want to restart all the web (Thin, Unicorn) processes, because you just pushed changes to your application and need to restart the app instances for them to take effect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;restart myapp-web&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These commands are available from anywhere on your machine, you don&amp;#8217;t need to be in the root of your application or anything, but you&amp;#8217;ll have to execute them as the root user, or with &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As you can see, Foreman beautifully integrates in both your development, as well as your production environment. If you&amp;#8217;ve set it up in your development environment, which should take less than a minute, you&amp;#8217;re pretty much good to go when you deploy to production. I actually use 2 Procfiles: &lt;code&gt;Procfile&lt;/code&gt; and &lt;code&gt;Procfile.production&lt;/code&gt;. This is because I want to specify different flags for my production environment, such as &lt;code&gt;-e production&lt;/code&gt;. You can tell Foreman to load a different Procfile, than the default.&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  foreman start                           # defaults to ./Procfile
  foreman start -f ./Procfile.production  # uses ./Procfile.production
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id='conclusion'&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;&lt;a href='https://github.com/ddollar/foreman'&gt;Foreman&lt;/a&gt; and &lt;a href='http://upstart.ubuntu.com/'&gt;Upstart&lt;/a&gt; can really save a lot of time, both in development, as well as in production. It&amp;#8217;s a simple, reliable and awesome solution. It takes about a minute or less to set up locally for in development, and even less in production since all you have to do is export your Procfile and tell Upstart to run it. You have the joy of booting up your development environment with a single command to get to work quickly without having to remember all the command options/flags, and you can export your Procfile to Upstart in production and have it take care of keeping your processes up and running.&lt;/p&gt;</content>
    <summary type="html">Every application you deploy should be monitored and managed by some type of process monitor to ensure your processes are up and running at all times. See how this is done using the Foreman RubyGem and the Upstart utility.</summary>
  <feedburner:origLink>http://michaelvanrooijen.com/articles/2011/06/08-managing-and-monitoring-your-ruby-application-with-foreman-and-upstart/</feedburner:origLink></entry>
  <entry>
    <id>tag:michaelvanrooijen.com,2011-06-01:/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/</id>
    <title type="html">More concurrency on a single Heroku dyno with the new Celadon Cedar stack</title>
    <published>2011-05-31T22:00:00Z</published>
    <updated>2011-05-31T22:00:00Z</updated>
    <link rel="alternate" href="http://feedproxy.google.com/~r/michaelvanrooijen/~3/HRt09oSr2Xs/" />
    <content type="html">&lt;p&gt;&lt;a href='http://heroku.com/'&gt;Heroku&lt;/a&gt; recently released the public beta of their new stack called &lt;a href='http://devcenter.heroku.com/articles/cedar'&gt;Celadon Cedar&lt;/a&gt;. Check out their &lt;a href='http://blog.heroku.com/archives/2011/5/31/celadon_cedar/'&gt;blog post&lt;/a&gt; for more information. There are a few minor adjustments to how their pricing model work. Free plans are not affected and it generally benefits the consumer. I am truly excited to see this because I&amp;#8217;ve been using &lt;a href='https://github.com/ddollar/foreman'&gt;Foreman&lt;/a&gt;, a gem by David Dollar, which allows you to nicely organize, view and manage your processes. I&amp;#8217;ll be doing an article on Foreman in the future. Heroku&amp;#8217;s Cedar stack takes the same approach as Foreman, and you can (and should) actually use Foreman in development. If it works there, it probably works on Heroku&amp;#8217;s new Cedar stack as well.&lt;/p&gt;

&lt;p&gt;The newly mentioned capabilities of the Cedar platform however, are not the only thing I&amp;#8217;m excited about. When someone told me about this stack prior to the release of the public beta, and showed me a &lt;code&gt;Procfile&lt;/code&gt;, the first thing I asked was:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;#8220;Can I ride a Unicorn?&amp;#8221; - And his answer was: &amp;#8220;Yes&amp;#8221;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;img alt='unicorn ruby app server' src='http://michaelvanrooijen.com/images/articles/unicorn.png' /&gt;&lt;/p&gt;

&lt;h2 id='unicorn'&gt;Unicorn&lt;/h2&gt;

&lt;p&gt;I&amp;#8217;m sure most of you know, or at least have heard about Unicorn. If you&amp;#8217;re a Ruby on Rails developer, you&amp;#8217;ve probably seen the Unicorn gem in your Gemfile. But, for those that don&amp;#8217;t know, or never tried it out before, I highly encourage you to check out &lt;a href='https://github.com/blog/517-unicorn'&gt;this post&lt;/a&gt; sometime, by Chris Wanstrath over at Github.&lt;/p&gt;

&lt;p&gt;What I&amp;#8217;d like to point out right now for this article is that Unicorn has a &amp;#8220;master&amp;#8221; process, and has one or more &amp;#8220;worker&amp;#8221; processes. The &amp;#8220;master&amp;#8221; process itself doesn&amp;#8217;t see any requests, but instead, it manages the worker processes, which are its child processes. They get forked from the master process. The kernel handles load balancing, and you only connect via a single unix socket or port using an upstream in, for example, NGINX. Unlike with for example Mongrel and Thin, where you upstream to multiple ports.&lt;/p&gt;

&lt;p&gt;In any case, what&amp;#8217;s important to understand for now is the fact that there&amp;#8217;s a master process which loads in your application&amp;#8217;s environment, and forks off child processes to serve your application.&lt;/p&gt;

&lt;h2 id='cedar_and_unicorn__more_concurrency_per_heroku_dyno'&gt;Cedar and Unicorn - More concurrency per Heroku dyno&lt;/h2&gt;

&lt;p&gt;At this point you may already know where I&amp;#8217;m going with this. Let&amp;#8217;s create a basic Rails application.&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  rails new cedar-on-unicorn
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Generate a simple controller which won&amp;#8217;t interact with the database&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  rails generate controller home index
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Open up your &lt;code&gt;Gemfile&lt;/code&gt; and ensure you have the following defined&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  gem &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;rails&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;3.0.7&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;
  gem &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;unicorn&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And now comes the interesting part, create a unicorn config file in &lt;code&gt;Rails.root/config/unicorn.rb&lt;/code&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  worker_processes &lt;span class='i'&gt;4&lt;/span&gt; &lt;span class='c'&gt;# amount of unicorn workers to spin up&lt;/span&gt;
  timeout &lt;span class='i'&gt;30&lt;/span&gt;         &lt;span class='c'&gt;# restarts workers that hang for 30 seconds&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And a Procfile for Heroku&amp;#8217;s new Cedar stack in &lt;code&gt;Rails.root/Procfile&lt;/code&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Bundle it up&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  bundle install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Initialize a new Git repository, commit it, and create your new Heroku Cedar Stack-based environment and push to it&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  git init
  git add . &amp;amp;&amp;amp; git commit -m &lt;span class='s'&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;span class='k'&gt;concurrecy&lt;/span&gt;&lt;span class='dl'&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
  heroku create --stack cedar
  git push heroku master
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, visit &lt;strong&gt;http://myapp.heroku.com/home/index&lt;/strong&gt; and you should be up and running.. with 4 unicorn worker instances, on a single Dyno! Prior to Celadon Cedar, you could only deploy Thin application servers, which don&amp;#8217;t have this kind of process management. Thin doesn&amp;#8217;t have a master process which forks children, so this wasn&amp;#8217;t possible. If you wanted more concurrency / instances, you had to spin up more Heroku dynos.&lt;/p&gt;

&lt;h2 id='but_dont_overdo_it'&gt;But don&amp;#8217;t over-do it&lt;/h2&gt;

&lt;p&gt;&amp;#8220;Too much&amp;#8221; is never a good idea. A dyno is essentially it&amp;#8217;s own little isolated environment. You probably get your own dedicated amount of CPU cycles, and there is a limit to how much RAM you may consume (for obvious reasons), even if you can&amp;#8217;t see your memory usage. I believe that there is a 300mb memory usage limit per dyno. Also, the more instances you try to cram in to a single VM, the worse your throughput gets when your VM starts to reach its resource limits, in which case you&amp;#8217;re better off with less instances.&lt;/p&gt;

&lt;h2 id='benchmarks'&gt;Benchmarks!&lt;/h2&gt;

&lt;p&gt;Yup. I&amp;#8217;ve done some basic AB runs to see the difference in throughput. Now, I&amp;#8217;m not going to show every detail of these results, but I think you&amp;#8217;ll get the main picture.&lt;/p&gt;
&lt;div class='notice'&gt;&lt;p&gt;All tests were performed on a page where there is no interaction with a database, because that&amp;#8217;s not what I&amp;#8217;m testing. The tests were performed by doing &lt;strong&gt;1000&lt;/strong&gt; requests, at a concurrency level of &lt;strong&gt;100&lt;/strong&gt;. The tests have been performed multiple times and I took the average results of each test.&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;1 Unicorn Worker&lt;/strong&gt; (Roughly equivalent to a single Thin instance)&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  Time taken for tests:   3.835 seconds
  Requests per second:    260.77 [#/sec]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;2 Unicorn Workers&lt;/strong&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  Time taken for tests:   1.689 seconds
  Requests per second:    592.00 [#/sec]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;3 Unicorn Workers&lt;/strong&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  Time taken for tests:   1.164 seconds
  Requests per second:    833.91 [#/sec]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;4 Unicorn Workers&lt;/strong&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  Time taken for tests:   0.979 seconds
  Requests per second:    918.90 [#/sec]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;5 Unicorn Workers&lt;/strong&gt;&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  Time taken for tests:   10.621 seconds
  Requests per second:    83.32 [#/sec]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;See what happened? It seems that spawning 5 workers is &amp;#8220;too much&amp;#8221; and actually reduces throughput. 3-4 workers per dyno seems to be your best bet in this case. There were also times that spinning up 4 workers was too much. Notice how little 4 workers improves over 3 workers. This is clearly its limit. This is a bare-bones web application, if you actually wrote code, added gems and such, then 4 unicorn workers might be too much.&lt;/p&gt;

&lt;p&gt;But! If having 3 Unicorn workers isn&amp;#8217;t enough for you, go ahead and spin up 3 more:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  heroku scale web=2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will spin up a second dyno that&amp;#8217;ll spin up 3 more Unicorn workers, bringing it up to a total of 6 Unicorn workers spread across 2 dynos! Need 30 workers?&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='term'&gt;
  heroku scale web=10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;With this you have 30 Unicorn workers spread across 10 dynos, and so forth.&lt;/p&gt;

&lt;p&gt;Imagine if you were using Sinatra instead of Rails, and your app consumes a lot less memory. You may even be able to spin up 7-8 Unicorn worker instances per dyno.&lt;/p&gt;

&lt;h2 id='conclusion'&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Before Heroku released the Celadon Cedar stack, this simply wasn&amp;#8217;t possible. You&amp;#8217;d have a dyno that should (in terms of memory) be able to spin up 3 or 4 instances, but you were only able to spin up a single Thin instance per dyno. With Thin, even after the introduction of Cedar, this is still the case. But now since the application server constraint no longer exists, you can use Unicorn to do it. With this, you should be able to improve your application&amp;#8217;s throughput &lt;strong&gt;by a factor of 3 to 4&lt;/strong&gt; on the same dyno.&lt;/p&gt;</content>
    <summary type="html">With the introduction of Heroku's new stack called Celadon Cedar, it is now possible to dramatically improve throughput by adding more concurrency to a single dyno.</summary>
  <feedburner:origLink>http://michaelvanrooijen.com/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/</feedburner:origLink></entry>
  <entry>
    <id>tag:michaelvanrooijen.com,2011-05-28:/articles/2011/05/28-setting-up-coffeescript-in-rails-3/</id>
    <title type="html">Setting up CoffeeScript in Rails 3</title>
    <published>2011-05-27T22:00:00Z</published>
    <updated>2011-05-27T22:00:00Z</updated>
    <link rel="alternate" href="http://feedproxy.google.com/~r/michaelvanrooijen/~3/yhUAsyEeDmc/" />
    <content type="html">&lt;p&gt;About a year ago I attempted to start a blog, mainly to write about Ruby, basic server provisioning and other related technologies to share my knowledge and experiences to help other people out or start discussions. Unfortunately for various reasons I wasn&amp;#8217;t able to spend enough time to do this back then.&lt;/p&gt;

&lt;p&gt;Thankfully this is no longer the case and I&amp;#8217;m excited to pick it up again!&lt;/p&gt;

&lt;p&gt;Last week I decided to throw away the few articles I wrote up back then since they are pretty much obsolete now. I re-designed and rewrote my blog. Hope you like it!&lt;/p&gt;

&lt;h2 id='now_on_to_the_actual_article'&gt;Now, on to the actual article&lt;/h2&gt;

&lt;p&gt;As of late, more and more people are getting in to CoffeeScript. It was previously mentioned that CoffeeScript would ship with Ruby on Rails 3.1. Although you can easily switch it off if you prefer to write raw JavaScript. But it&amp;#8217;s nice that they provide the option to quickly get up and running with CoffeeScript if you wanted to, I know I&amp;#8217;ll be using it!&lt;/p&gt;

&lt;p&gt;I recently released a new application I was working on and I had trouble getting CoffeeScript working in my production environment. &lt;a href='http://jashkenas.github.com/coffee-script/'&gt;CoffeeScript&lt;/a&gt; requires the &lt;a href='http://code.google.com/p/v8/'&gt;V8 engine&lt;/a&gt;. I was using &lt;a href='http://nodejs.org/'&gt;Node&lt;/a&gt; which enabled me to use CoffeeScript by installing &lt;a href='http://npmjs.org/'&gt;NPM&lt;/a&gt; and then installing CoffeeScript via NPM. This was tragic. I previously somehow got it working in my staging environment, but when I tried to push it to production I simply could not get it to compile my &lt;code&gt;.coffee&lt;/code&gt; files to &lt;code&gt;.js&lt;/code&gt; files. I figured it had something to do with the load paths even though I set them up. I asked a bunch of people for help regarding this, but unfortunately no one I asked had a direct answer to my Node/NPM problem.&lt;/p&gt;

&lt;h2 id='the_solution'&gt;The solution&lt;/h2&gt;

&lt;p&gt;After spending a good amount of hours researching this, someone pointed out there is an open source project called &lt;a href='https://github.com/cowboyd/therubyracer'&gt;The Ruby Racer&lt;/a&gt;. This is the &lt;strong&gt;V8 runtime, embedded in Ruby&lt;/strong&gt; - sound cool? Well, it is. Since I only installed Node and NPM for the purpose of being able to use CoffeeScript, I figured that it&amp;#8217;s pretty much overkill to use it.&lt;/p&gt;

&lt;p&gt;I removed Node and read up on The Ruby Racer. It turned out to be incredibly easy to set up!&lt;/p&gt;

&lt;p&gt;In a typical Ruby on Rails 3 app that ships with Bundler, all you define are the following gems in your &lt;code&gt;Gemfile&lt;/code&gt;:&lt;/p&gt;
&lt;div class='code'&gt;&lt;pre&gt;&lt;code class='ruby'&gt;
  gem &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;therubyracer&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;
  gem &lt;span class='s'&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;span class='k'&gt;barista&lt;/span&gt;&lt;span class='dl'&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That&amp;#8217;s it. No Node, no NPM, just 2 gems defined in the &lt;code&gt;Gemfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href='https://github.com/Sutto/barista'&gt;Barista&lt;/a&gt; is a gem that seamlessly integrates CoffeeScript support in to your Rails or other Rack-based applications. It only took me a minute to set up.&lt;/p&gt;

&lt;p&gt;So if you want to use CoffeeScript, and nothing else Node related, you should definitely take a look at this solution before installing Node just for the purpose of being able to use V8 with CoffeeScript in Ruby.&lt;/p&gt;

&lt;h2 id='wrapping_it_up'&gt;Wrapping it up&lt;/h2&gt;

&lt;p&gt;This was a simple initial / introduction article, but it may save a couple of hours of headaches for developers trying to deploy their code base with unnecessary complexity as I did.&lt;/p&gt;

&lt;p&gt;From here on out I&amp;#8217;ll be frequently submitting new articles. They will mostly be related to Ruby, (App/Web/OS) server technologies, open source and perhaps various (actual) community related discussions/thoughts.&lt;/p&gt;

&lt;p&gt;So if that interests you, be sure to keep checking back!&lt;/p&gt;</content>
    <summary type="html">Here's a pro-tip for when you attempt to set up CoffeeScript with Ruby on Rails 3.</summary>
  <feedburner:origLink>http://michaelvanrooijen.com/articles/2011/05/28-setting-up-coffeescript-in-rails-3/</feedburner:origLink></entry>
</feed>

