使用反引号从Rails App中启动另一个Rails服务器 [英] Start another Rails Server from within Rails App with backticks

查看:73
本文介绍了使用反引号从Rails App中启动另一个Rails服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个Rails应用程序,该应用程序可以用作另一个Rails应用程序的更新程序。

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

我正在执行更新过程,


  • 下载新版本zip

  • 提取到正确的位置

  • 同步资产

  • 捆绑安装

  • 预编译资产

  • 使用-bundle exec rails服务器启动服务器

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

最后一步出现问题。

当我跑步时:

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.

更新程序是用Rails 4编写的,正在更新的应用程序是Rails 3。

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>'

从此输出中,我可以看出它正在尝试使用错误版本的Railties ...

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

当我手动 cd ../ other-project bundle exec rails server -d- p 3000 很好。

我有什么bash技巧可以用来解决这个问题吗?
基础框是Ubuntu 14.04

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

谢谢!

推荐答案

好的,我已经花了整整一个上午的时间进行故障排除,并且找到了解决方案!

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

您要做的就是在以下位置设置BUNDLE_GEMFILE环境变量:

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

bundle exec rails server -d -p 3000

它似乎Bundler需要一些帮助来找到项目Gemfile,因为我试图在当前包中启动另一个应用程序,这是我创建的用于控制该更新程序负责更新的应用程序的类。

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.

我很高兴地说start方法终于按预期工作了!

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

这篇关于使用反引号从Rails App中启动另一个Rails服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆