Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4backticks

Start another Rails Server from within Rails App with backticks


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!


Solution

  • 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