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:

redis: redis-server ./config/redis.conf
sidekiq: bundle exec sidekiq -C ./config/sidekiq.yml

Exporting it generated these files:

[foreman export] writing: myapp-staging.conf
[foreman export] writing: myapp-staging-redis.conf
[foreman export] writing: myapp-staging-redis-1.conf
[foreman export] writing: myapp-staging-sidekiq.conf
[foreman export] writing: myapp-staging-sidekiq-1.conf

Isolated the Sidekiq Upstart script and ran it by itself:
sudo service start myapp-staging-sidekiq-1
Resulted in:
start: Job failed to start
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:

namespace :foreman do
  set :foreman_application, "#{application}-#{rails_env}"
  desc "Export the Procfile to Ubuntu's upstart scripts"
  task :export, roles: :app do
    run "echo PATH=\"$PATH\"\n > #{current_path}/.env"
    run "echo RAILS_ENV=#{rails_env}\n >> #{current_path}/.env"
    run "echo RACK_ENV=#{rails_env}\n >> #{current_path}/.env"
    run "cd #{current_path} && #{sudo} bundle exec foreman export upstart /etc/init -a #{foreman_application} -u #{user} -l #{shared_path}/log -d #{current_path}"
  end

  [:start, :stop, :restart].each do |action|
    desc "#{action} the foreman processes"
    task action, :roles => :app do
      run "#{sudo} service #{foreman_application} #{action}"
    end
    after "deploy:#{action}", "foreman:#{action}"
  end
end

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.