Search code examples
ruby-on-railsreactjsforeman

Foreman not working with a rake tast on rails


So I have my rails backend running at port 3001 and may React frontend running at port 3000.

I want to setup a simple rake start task to start both.

To do so, I use the foreman gem, which works perfectly when I run: foreman start -f Procfile.dev.

However: when I run my task: rake start, I get the following error:

Running via Spring preloader in process 36257
15:56:57 web.1  | started with pid 36258
15:56:57 api.1  | started with pid 36259
15:56:57 api.1  | /usr/local/opt/rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/foreman-0.64.0/bin/foreman-runner: line 41: exec: PORT=3001: not found
15:56:57 api.1  | exited with code 127
15:56:57 system | sending SIGTERM to all processes
15:56:57 web.1  | terminated by SIGTERM

Here is my my start.rake file:

namespace :start do
  desc 'Start dev server'
  task :development do
    exec 'foreman start -f Procfile.dev'
  end

  desc 'Start production server'
  task :production do
    exec 'NPM_CONFIG_PRODUCTION=true npm run postinstall && foreman start'
  end
end
task :start => 'start:development'

and my Procfile.dev file:

web: cd client && PORT=3000 npm start
api: PORT=3001 && bundle exec rails s

Any idea ?


Solution

  • I faced the same issue. I am not sure why, but when foreman is running from rake, it is unable to handle multiple commands on the same line, e.g.

    web: cd client && PORT=3000 npm start
    

    To solve the issue, I changed my Procfile.dev to

    web: npm start --prefix client
    api: bundle exec rails s -p 3001
    

    and in my package.json, I changed

    "scripts": {
        "start": "react-scripts start",
        ...
    }
    

    to

    "scripts": {
        "start": "PORT=3000 react-scripts start",
        ...
    }
    

    This allows you to specify the ports for both react and rails servers, and works fine with both

    foreman start -f Procfile.dev and rake start