Sidekiq with environment variables and systemd

Normal setup

Running Sidekiq with systemd is easy, download the sidekiq.service file from Github and place it in /etc/systemd/system.

Start it with: sudo systemctl start sidekiq

Enable it to run at boot: sudo systemctl enable sidekiq

And stop it: sudo systemctl stop sidekiq

The only lines you have to change are:

WorkingDirectory=/opt/myapp/current 

User=sysadmin

Group=sysadmin

 

With environment variables

But what if you have some environment variables that you want to load before starting Sidekiq? Keep in mind that there are many ways to do this, and this only how I do it.

I put the environment variables in a hidden file called ‘.env’ in the home directory of the user that runs the application. If this user is called deploy, the file would be /home/deploy/.env

In it are my environment variables with export in front of them:

export DATABASE_URL=postgres://deploy:sdfsdfDNFSDF@localhost/my_prod_db
export SECRET_KEY_BASE=640c916asdfasdfasdfasdfasdfasdfasdfasdf
export SMTP_ADDRESS="smtp.mandrillapp.com"

These are loaded when you log in through ssh with source as the first line in .bashrc. It doesn’t have to be the first line but it has to be above the “if not running interactively don’t do anything” line.

source ~/.env

But how do these env variables reach Sidekiq? We’ll simply start Sidekiq with bash in the Systemd service file. Change the ExecStart line in /etc/systemd/system/sidekiq.service and add /bin/bash -lc in front.

ExecStart=/bin/bash -lc '/usr/local/bin/bundle exec sidekiq -e production -C /home/sysadmin/checkout/config/sidekiq.yml'

Bash will load the environment variables and then start Sidekiq.

Is Digital Ocean’s One-click app for Ruby on Rails any good?

Digital Ocean offers 2 types of droplets (servers):

  1. Droplets with a clean Linux install.
  2. Droplets with some application preinstalled: “One-click apps”

Let’s have a look at the One-click Rails installation they offer. I’ll describe what you get and what I like about, what I don’t like about it and I’ll give some tips on how to use it.

Welcome to One-click apps, what do they do? Do they do things? Let’s find out!”

Continue reading “Is Digital Ocean’s One-click app for Ruby on Rails any good?”

Use a git-hook to deploy your app

There are a lot of ways to deploy an app to a server, here is a simple one that I often use. This can be a bit confusing if you are not familiar with Git but I promise it’s the easiest way!

It works like this: you create an empty git repository on the server and you push the branch you want to this reposity from your development machine. The repository on the server has a little script (hook) that puts the files in the right directory (‘rails_project’) and runs all the bundle commands. You end up with 2 directories: the bare repository and the ‘checkout’ with the project. Continue reading “Use a git-hook to deploy your app”

Why you shouldn’t run two Rails apps on the same server

It sounds convenient right? Install a server once and every time you build an app you add it to the server. If you update something on the server it will be updated for all the apps at the same time. Which may save you time and money.

Is it hard to install multiple apps on the same server? No not at all. Will it save you time setting things up? Yes. Will it make you happy in the long run? No!

Continue reading “Why you shouldn’t run two Rails apps on the same server”