Unicorn + Bluepill + Nginx

If you found this article and are expecting a post about the Matrix Blue/Red pill analogy combined with a fairytale animal then I’m afraid you’re on the wrong page. If however, you’re interested in a fast, kick-ass Ruby on rails environment then you ARE in the right place.

Getting a stable, Ruby on Rails app running in a production environment can often be tricky. Sometimes there’s different Ruby versions installed, different users might have different gem paths, environment variables need to be set, RVM needs to be considered, unicorn, nginx, apache- sometimes it’s a complete and utter mess.

That’s why my advice is simple. Keep it simple. With Halftone, the only Rails application on the server is Halftone. We don’t share resources with any other application. Since I started from scratch on a clean server, our rails app has the bare minimum needed to run. Only one Ruby version, one repository of gems, one HTTP server and one process monitoring software.

I cannot stress this enough, keep your app’s system requirements simple and isolated.

Below I’m going to run through the software I use to keep things dandy at Halftoneapp.

Bluepill

Bluepill is a process monitoring engine written in Ruby.

We love Ruby here at Halftone so it was hard to pass up working with configuration files which accept Ruby code. Bluepill monitors our Unicorn process, if it runs amuck and starts chewing up valuable memory or CPU usage – we send a QUIT signal which starts a graceful shutdown, allowing Unicorn to finish. Unicorn will then spawn a new worker process to make up for the dead unicorn, as sad as that sounds.

We did start out using GOD, however recently switched to Bluepill due to its ability to monitor child processes such as those spawned by Unicorn.

How did we install it?

gem install bluepill
sudo mkdir /var/bluepill (Bluepill requires this folder as per it's docs)
sudo chown appuser.appuser /var/bluepill

How do we run it?
First you’ll need a configuration file. We’ve uploaded a version of the Bluepill config we use, though modified slightly with extended comments. You can find it here.

DON’T FREAK OUT. Yes, the configuration file linked above is lengthy but don’t be scared – take some time to read it all and become familiar with the settings.

We run Bluepill as a local user, the same user that runs our Rails application.

To start Bluepill run
[bash]bluepill load /path/to/unicorn.pill –no-privileged[/bash]

This starts Bluepill and should start Unicorn. Run ps -aux to see if all looks swell. Keep an eye on the Bluepill log to debug any problems you may encounter.

Visit Bluepill for more details.

Note! Because we’ve started Bluepill with the no-privileged mode, you’ll need to specify this everytime you interact with it.

For example;

[bash]bluepill stop unicorn –no-privileged[/bash]
[bash]bluepill start unicorn –no-privileged[/bash]

In order to take advantage of syslog logging, you’ll need to follow the instructions in the Bluepill README on how to setup syslogd.

UNICORN

Straight from the homepage; “Unicorn is an HTTP server for Rack applications designed to only serve fast clients on low-latency, high-bandwidth connections and take advantage of features in Unix/Unix-like kernels”.

Or in simple terms, Unicorn gets in, serves the request and then gets the heck outta there. Unicorn does not implement persistent connections which can often leave client connections idle; holding up valuable worker processes. All our intensive, lengthy processes are handled by Resque; a background job processor (we’ll talk about that in a separate post).

“Unicorns don’t care if you believe in them any more than you care if they believe in you”

How did we install it?

gem install unicorn

Visit the Unicorn website for more details.

What about our config file?
There’s not much to Unicorn. Simply point it to your Rails app, set the number of worker processes you’d like and away you go. See here for a sample unicorn config.

How do you run it?
If you’re using Bluepill to manage Unicorn, you’ll see how we start Unicorn in the config file. Otherwise you can launch Unicorn manually using the following.

[bash]/path/to/unicorn_rails -Dc unicorn.conf.rb -E production[/bash]

Nginx

We use Nginx to reverse proxy HTTP requests to Unicorn.

A request comes in for halftoneapp.com/blah and Nginx sends this to Unicorn via a unix socket.

How did we install it?
On Debian – apt-get install nginx

We added the following to the Nginx configuration file.

[bash]
server {
server_name yourdomain.com;
root /home/yourappuser/app/current/public;
try_files /system/maintenance.html $uri $uri/index.html @app;
location @app {
proxy_pass http://unix:/home/yourappuser/app/shared/sockets/yourappuser.sock;
}
error_page 404 400 500 502 /error.html;
location = /error.html {
root /var/www;
}
}
[/bash]

The important part is lining up the proxy_pass option to the same socket you’ve specified in the Listen option in the Unicorn configuration file.

Parting lines

I hope this helps you in your quest for the ultimate Ruby on Rails environment. If you have any questions or if something I’ve said is incorrect (we are human after all), feel free to leave a comment below.

So how exactly do I delete a design?

You accidentally uploaded that picture of your cat in a tree instead of the bosses new business card design. No problems!

If you are the project owner, or the original person who uploaded the design – you’ll see a “gear” icon when you hover over your design on the collections page.

Clicking the gear reveals the Hide Design button. Clicking it will make it disappear.


Easy as pie.

CSS Guide: Annotation Comments

Today I’m going to step through a CSS Guide to creating Contextual Hover-Overs. You may have seen these in things like tooltips or in Halftone’s Annotation system. This same technique can also be employed to create Contextual Modal Boxes, similar to the iPad menus. Here’s the finished result (hover over the annotation to see it pop-out).

[iframe_loader src="http://jsfiddle.net/taybenlor/nVgbT/5/embedded/result/" height="400" width="500"]

We’ll be doing this with a pure-css approach with minor progressive enhancement. With the aim of keeping our page lean and fast by reducing JS overheads.

Continue reading

Annotations v.3

With the latest Halftone minor release we’ve squashed a heap of bugs and prepared for a host of new functionality coming with the next major release. We’ve also decided to push through a brand new version of Annotations. We’re calling this (excitingly) Annotations v.3. What happened to v.2? We’ll never know.

The new look Annotations

Continue reading

A day in the life of a Halftoneapp Developer

Does anyone else hate their alarm? I do, with a passion. No matter what tone or song I choose – I end up hati – no wait, loathing it.

Nevertheless, I launch myself out of bed and attempt to make sense of my surroundings. A week old coffee on my desk, a plate of noodles from last night’s dinner, my girlfriends unde- .. err, let’s keep this PG rated ok?

Recently I bought a brand spanking new desk. A glass desk to be precise. And I like it. Load’s of room, comfortable height though I did have to purchase a mouse matt (Who would have known my Apple magic mouse wouldn’t work on *glass*).

Upon opening the lid of my laptop, the sweet tone of OSX’s new mail alert bursts through my Sennheiser headphones. Somebody’s been busy. Oh goody, the Halftoneapp testers worked late last night and my assigned tickets are proof they were indeed busy.

Adding comments doesn’t work in Firefox? My bad. Deployed a fix and punished myself for not performing proper cross-browser testing. The punishment? No strawberries with my yogurt. Tough I know.

The day continues as I work through the Halftoneapp tickets, which can often be exciting. Purely because as the day progresses, the product seems to take shape on a new level. Yesterday’s boring form-submit, is now today’s new-sexy-fandangled-ajaxed-comments.

The day isn’t complete with a trip to the local hoops for some bball action. A daily routine which breaks up the day and get’s me outside for some much needed fresh air and exercise.

Headphones of choice: Wireless Sennheiser HDR-120’s (Bought these a few years ago, I love that I can visit the bathroom without missing a beat)

Music of choice: Electro, dubstep, Trance, Indie – I go through stages. I also love using Last.fm to discover new music (my profile). I also spend way too much on iTunes tracks.

Beverage of choice: Coffeeeeeeeeeeeeeeee.

Nerd stuff: Macbook pro 13inch, with a 19inch HP widescreen display (which really needs updating). Textmate, Ruby on Rails, github, gmail, twitter, iTunes, Last.fm,

——————

This post was written by James, Halftoneapp developer who often tweets here.

 

Introducting Halftone

Email has been the de-facto standard for sharing designs with clients and workmates. You know the process: export your Photoshop or Fireworks comp as a image, chuck it in an email, and wait for the replies to come in. But:

  • We get so much email, it’s hard to keep track. Emails get lost. Important feedback is delayed.
  • It’s hard to hold a conversation. Some email apps have email threading and they’re awesome. But some don’t. The client usually has the one that doesn’t, so they never remember what’s been said and repeat themselves. They repeat themselves.
  • You have to wrestle with attachments limits. 10 MB? 2 MB? “Inbox full”. Then there’s the one client who can never seem to open any attachment at all. Peachy!
  • When they do: blurry, pixelated, or shrunken screenies. They never see it the way you intended it to be seen.
  • “And in the top right corner…” With big, long previews, it’s hard to point out a particular thing just by writing.

With Halftone, your designs are the focus—big, beautiful previews against a muted background. Encourage workmates and clients tocomment on designs and annotate particular parts. Keep a history of all a design’s revisions. And it’s all archived in one place. There’s no clutter and no unnecessary signups for collaborators. Super-simple and client-proof.

If you’re a designer—freelance or work in a company—and want to try out Halftone in beta, email Ben. We’d love you to put it to the test.