Jonathan Yeong


Unicorn not updating ENV variables

Sending mail was working on development and also on rails production console. But it wasn’t working on the production app. It kept spitting out SMTPSyntaxError. Which is surprisingly, unhelpful.

First thing, I did was to make sure the object returned the correct values. I ended up setting the mail view to a hardcoded template (ie it didn’t use get any data from an object). This didn’t work. I then changed out the SMTP settings with the Mailgun Ruby API. All of a sudden this worked in development but didn’t work on production.

However, this step simplified the amount I had to test. When I was using SMTP settings the number of break points spanned across files. Now the only break point was in one file, my production.rb.

The next thing I did was test the environment variables. This was done last because I thought that my production app had already loaded them in. Previously, I checked that the env variables were on my production server (they were). However, upon further debugging statements that were implemented directly into the production app (bad I know!) I realised that none of my updated env variables were loaded.

The next hour was spent googling “rails unicorn not seeing environment variables” or something along that phrase. I soon came across an engine yard article. Which stated:

When using environment variables with Unicorn, hot restarts don’t pick up changes to environment variables.

Light bulb!

When running cap deploy it hot restarts your unicorn server. It doesn’t take it down completely. This is expected behaviour and totally fine in most cases. But in this case I needed to shut down the server completely so it could pick up the new env variables. I did this by:

$ cd /var/www/<project-folder>/shared/tmp/pids
$ cat unicorn.pid
21590
$ kill -quit 21590
$ /etc/init.d/unicorn start
$ cat unicorn.pid
25239


Leave a comment