Select Page
Using Foreman with Upstart & Capistrano

Using Foreman with Upstart & Capistrano

Foreman is a great tool for running multiple processes along side your rails project. Processes like Redis, SidekiqFaye or any other process that doesn’t run within Rails itself. Foreman runs in your development environment as well as your production environments. To use Foreman in production, you have to export it’s config file in one of the following formats:

Since most of the servers we work on are Ubuntu, and (newer versions of) Ubuntu come with Upstart preinstalled, we generate the Foreman export script to Upstart.

When recently deploying a customer’s project, we noticed that the Foreman Sidekiq process wasn’t starting up.

The Procfile we were working with had this content:

Exporting it generated these files:

Isolated the Sidekiq Upstart script and ran it by itself:

Resulted in:

Unfortunately there was no log output in the usual place: /var/log/upstart/myapp-staging-sidekiq-1.log
It would merely fail without a result.

After many hours of research, trial and error, the closest answer we came to was that bundle couldn’t be found (via PATH). Issue #443 states that you need to setup a .env file with your servers PATH because as of version 0.63 of Foreman it no longer assumes your PATH. However, most of our development happens on Macs so the environment variables are vastly different than the servers we deploy on. The solution that seems to work best for any server setup we deploy to is the following:

Notice that line 5 takes the current server’s path and assigns it to the .env file in the current directory. Line 6 and 7 setup the environment for the Foreman processes. This helps ensure the .env file is always running what the server is running as well as it gets updated when a new foreman:export command is executed.

Please let us know if you found this helpful.

User does not respond to ‘devise’ method

I’ve recently went back to an old project and it seems to have stopped working giving me this error:

[read full stack trace at: gist]
Since I didn’t really change anything, I was surprised to see it break. So I tried to do what it said, I changed that I had a require statement for my ORM which was Mongoid. I had: (in config/devise.rb)


RVM install ruby-1.8.7-p330 fails

Today I’m thinking of getting back into so good old work and I read there is a new patch to Ruby, version p330. So I figure that RVM (Ruby Version Manager) being so easy and quick to update, I’d do the good old “rvm get head, rvm reload, rvm update 1.8.7” and I’m off to the races.

Well it wasn’t quite as simple. I ended up with an error:
ruby-1.8.7-p330 - #fetching
ruby-1.8.7-p330 - #extracting ruby-1.8.7-p330 to /Users/andrew/.rvm/src/ruby-1.8.7-p330
ruby-1.8.7-p330 - #extracted to /Users/andrew/.rvm/src/ruby-1.8.7-p330
ruby-1.8.7-p330 - #configuring
ruby-1.8.7-p330 - #compiling
Error running 'make ', please read /Users/andrew/.rvm/log/ruby-1.8.7-p330/make.log
There has been an error while running make. Halting the installation.

When I looked into the the make.log, It had this to say:
readline.c: In function ‘username_completion_proc_call’:
readline.c:730: error: ‘username_completion_function’ undeclared (first use in this function)
readline.c:730: error: (Each undeclared identifier is reported only once
readline.c:730: error: for each function it appears in.)

So, I figured this was going to take too long to deal with, and moved on to working on my projects. That is till I opened up my project and and did a “rvm [email protected]” and it come up with:
warn: ruby ruby-1.8.7-p330 is not installed.
To install do: 'rvm install ruby-1.8.7-p330'

Great! Now I can’t upgrade, and I can’t work with what I have!

Long story short, I figured out that I was missing readline (now I’m sure I’ve installed in a long while ago, but I think I was missing the latest version). Also, rvm install readline resulted in an “Unrecognized command line argument: ‘readline’ ( see: ‘rvm usage’ )

Turns out RVM updated it’s command to install readline to:
rvm package install readline

Update: (Nov 10, 2011)
If you’re using a newer version of RVM, package has been renamed to pkg, so the command to install readline would be:
rvm pkg install readline

However this didn’t fix my install issue, still got the same error. Luckily I found a post that pointed out that my rvm may not know where readline is (odd, as I used rvm to install readline). Running this command allowed me to install it correctly:
rvm install 1.8.7 -C –with-readline-dir=/Users/andrew/.rvm/usr
[Notice: ‘andrew’ is my username on my mac, yours might be different]

rvm install 1.8.7 -C --with-readline-dir=$rvm_path/usr

Like always, I’m hoping this helps some people out.

Let me know if you have any issues.

Stripping ASCII colour from a ruby string

Stripping ASCII colour from a ruby string

I spent a few good hours trying to figure out how to strip a coloured ruby string:

Coloured Ruby String

which comes out as this:

>> p t
"2 scenarios (e[33m2 undefinede[0m), 7 steps (e[36m2 skippede[0m, e[33m3 undefinede[0m, e[32m2 passede[0m)"
=> nil

Now I tried to gsub the string and remove the colour using a RegEx like this:

> t.gsub(/\e[(d+)m/, "")
=> "2 scenarios (e[33m2 undefinede[0m), 7 steps (e[36m2 skippede[0m, e[33m3 undefinede[0m, e[32m2 passede[0m)"

But as you can see, nothing changed. After reading a whole lot of docs on Ruby, Googling around and reading the Ruby String class I came to this conclusion:

>> b = "e[33m"
=> "e[33m"
>> b.each_byte{|c| puts c}
=> "e[33m"
>> "" << 27 #The only way I know how to add ASCII codes to string
=> "e"

Now what this allowed me to do is figured out the ASCII code for the e character. However because normally you escape the back slash on regular expressions, once I figured out e was a valid character, I got this:

>> "e[33m".gsub(/e[(d+)m/, '')
=> ""

I hope this saves someone a few hours!

Getting Paperclip to work with MongoMapper

Last night I was trying really hard to get MongoMapper to work with Paperclip via GridFS and failed. It seems right now Paperclip or even CarrierWave are not ready to be used with the latest MongoMapper drivers and GridFS (and I needed to use the latest drivers due to Scopify).

After giving up using GridFS, I wanted to use Paperclip with MongoMapper with the normal file system and I found this article from Ben Curtis, however with the latest version of Paperclip (version and MongoMapper (version 0.7.6) I was unsuccessful, kept getting:

Anonymous modules have no name to be referenced by

After reading the backtrace and diving into the paperclip source code I noticed that it was trying to use the ActiveRecord.logger and since I’ve removed ActiveRecord from my app, it was failing (with the odd error).

After some tweaking of Ben’s code, I came up with this:

Error with fsevent and autospec (on Mac)

Today I’ve upgraded my gems and noticed that when running my autospec I get an odd error:

[01:09:25 /usr/local/lib/ruby/gems/1.8/gems/ruby-ole-1.2.10/lib/ole/storage/base.rb:146:load]

WARN   root name was “R”

And my autospec would keep printing out:
sh: line 1:  6303 Trace/BPT trap          /usr/local/lib/ruby/gems/1.8/gems/autotest-fsevent-0.1.2/fsevent/darwin/fsevent_sleep ‘/Users/andrew/rails_apps/example_com’ 2>&1
sh: line 1:  6307 Trace/BPT trap          /usr/local/lib/ruby/gems/1.8/gems/autotest-fsevent-0.1.2/fsevent/darwin/fsevent_sleep ‘/Users/andrew/rails_apps/example_com’ 2>&1
Unfortunately I don’t have the time to look into the fsevent code to see if I can make a work around, however I found that you can remove the gem (‘sudo gem uninstall autotest-fsevent‘) and install the older version (‘sudo gem install autotest-fsevent --version 0.1.1‘)
Hopefully this helps someone out!
UPDATE: I forgot to mention I was using a Mac OS X 10.5.8, and based on the History.txt they made changes effecting Macs.
UPDATE 2 (Oct 22, 09): It seems that the same issue exists in fsevent 0.1.3 as well
UPDATE 3 (Mar 05, 2010): Issue still present in 0.2.1 however I found out that the error is only for OS X 10.5 and will not happen on OS X 10.6.
The full error message is:

/usr/local/lib/ruby/gems/1.8/gems/autotest-fsevent-0.2.1/fsevent/darwin/fsevent_sleep ; exit;
~%  /usr/local/lib/ruby/gems/1.8/gems/autotest-fsevent-0.2.1/fsevent/darwin/fsevent_sleep ; exit;
dyld: unknown required load command 0x80000022
zsh: trace trap

The only work around (outside of downgrading to version 0.1.1) is to copy fsevent_sleep from version 0.1.1 to version 0.2.1. However I’m not sure how stable that solution is.