Start another Rails Server from within Rails App with backticks

廉价感情. 提交于 2020-01-02 04:48:06

问题


I'm currently working on a Rails application that serves as an Updater for another Rails application.

I have the update process working,

  • Download new release zip
  • Extract to proper location
  • Sync Assets
  • Bundle install
  • Precompile Assets
  • Start server with - bundle exec rails server

I'm having an issue with the last step.

When I run:

Dir.chdir('../other-project')
`bundle exec rails server -d -p 3000`

from the updater app it seems to be pulling from the updaters bundle and not the new application bundle that it should be pulling from.

The updater is written in Rails 4 and the app it is updating is rails 3.

When I try to start the server I get the following:

/home/vagrant/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/railtie/configuration.rb:95:in `method_missing': undefined method `handlebars' for #<Rails::Application::Configuration:0x007f9de18de100> (NoMethodError)
    from /home/vagrant/apps/other-project/config/application.rb:22:in `<class:Application>'
    from /home/vagrant/apps/other-project>'
    from /home/vagrant/apps/other-project/config/application.rb:13:in `<top (required)>'
    from /home/vagrant/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:79:in `require'
    from /home/vagrant/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:79:in `block in server'
    from /home/vagrant/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:76:in `tap'
    from /home/vagrant/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:76:in `server'
    from /home/vagrant/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/vagrant/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

From this output I can tell that it is trying to use the incorrect version of railties...

When I manually cd ../other-project and bundle exec rails server -d -p 3000 it works fine.

Are there any bash tricks I can use to get around this? The base box is Ubuntu 14.04

Thanks!


回答1:


Alright, I've spent the morning troubleshooting this and I found a solution!

All you have to do is set the BUNDLE_GEMFILE environment variable before the:

bundle exec rails server -d -p 3000

It seems that Bundler needs a little help finding the projects Gemfile since I'm trying to start another app within the current bundle, here is the class that I created to control the app that this updater will be responsible for updating.

I'm happy to say that the start method finally works as expected!

class AppController
  @dir = Rails.root.join('../', 'Other-app/')

  def self.running?
    File.exist?("#{@dir}/tmp/pids/server.pid")
  end

  def self.start
    if running?
      puts "app already running"
    else
      Dir.chdir(@dir)
      puts "starting app..."
      `BUNDLE_GEMFILE=Gemfile bundle exec rails server -d -p 3000`
      puts "app started"
    end
  end

  def self.kill
    if not running?
      puts "app already dead"
    else
      Dir.chdir(@dir)

      puts "killing app..."
      `kill $(cat tmp/pids/server.pid)`
      puts "app dead"
    end
  end

  def self.restart
    if running?
      kill
      start
    else
      start
    end
  end
end



回答2:


You are right Mike !!

I'll say just set port :

bundle exec rails s -p 3001

bundle exec rails s -p 3000

for two different server instance!

Cheers!



来源:https://stackoverflow.com/questions/26185467/start-another-rails-server-from-within-rails-app-with-backticks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!