Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running foreman from a rake task

I have the following Rake task:

namespace :foreman do
  task :dev do
    `foreman start -f Procfile.dev`
  end
end

desc "Run Foreman using Procfile.dev"
task :foreman => 'foreman:dev'

The forman command works fine from the shell, however when I run rake foreman I get the following error:

/Users/me/.gem/ruby/2.0.0/gems/bundler-1.5.2/lib/bundler/rubygems_integration.rb:240:in `block in replace_gem': foreman is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
    from /Users/me/.gem/ruby/2.0.0/bin/foreman:22:in `<main>'

Forman specifically states:

Ruby users should take care not to install foreman in their project's Gemfile

So how can I get this task to run?

like image 511
Undistraction Avatar asked Nov 28 '14 13:11

Undistraction


People also ask

When would you use a rake task?

Rake allows you to define a list of other tasks that must run before the current task.

What is rake job?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.


3 Answers

If you must make it work via rake, try changing the shell-out via backtick to use a hard-coded path to the system-wide foreman binary

`/global/path/to/foreman start -f Procfile.dev`

You just need to use 'which' or 'locate' or a similar tool to determine the path that works outside your bundler context. If you are using rbenv, then this might be sufficient :

$ rbenv which rake
/home/name/.rbenv/versions/1.9.3-p448/bin/rake

I hope that helps you move forward.

like image 151
SciPhi Avatar answered Oct 24 '22 05:10

SciPhi


Not sure if this will work, but you could export the environment variables associated with your shell explicitly and then make a call to foreman. FWIW, I don't think this is recommended, and would suggest using a bash script as @dax proposes.

Steps

  1. Get the $PATH and other environment variables from your shell

    printenv >> shell.env
    
  2. Get the environment variables from the rails environment

    namespace :foreman_test do
      task :dev do
        `printenv >> rails.env`
      end
    end
    
  3. Compare the two and find out the changed environment variables, and set them up in your rake task in the system call

    namespace :foreman do
      task :dev do
        `export PATH=/original/path:/value && GEM_DIR=/some/folder && foreman start -f Procfile.dev`
      end
    end
    
like image 2
Anshul Goyal Avatar answered Oct 24 '22 06:10

Anshul Goyal


if it has to be a rake task, try this (from this answer):

namespace :foreman do
  task :dev do
    sh "foreman start -f Procfile.dev"
  end
end

if it doesn't have to be a rake task, I have a simple bash script to start for a specific project that works well:

#!/bin/bash

export PROJECT_DIR=`pwd`
export PORT=$1

source "$HOME/.rvm/scripts/rvm"

unset BUNDLE_GEMFILE
unset BUNDLE_BIN_PATH
unset RUBYOPT
unset GEM_HOME
unset GEM_PATH

(cd <project full path> && exec foreman start -p $PORT)
like image 1
dax Avatar answered Oct 24 '22 07:10

dax