Chris Lees
Chris Lees

Reputation: 2210

Automatically Starting ElasticSearch with Rails

I've been doing Ruby on Rails development with ElasticSearch between two machines and its starting to get a little annoying. My usual workflow is:

git pull
bundle install
rake db:migrate (or rake db:setup depending)
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true

Is there anyway I can add all of these commands to some type of start up script in Rails to bring them all into one place? It would make bringing up a dev environment a lot easier on me everytime I switch machines.

Upvotes: 0

Views: 3169

Answers (3)

awenkhh
awenkhh

Reputation: 6111

It looks like you should do this in your deployment using Capistrano. Here is an example config/deploy.rb file:

[basic parts omitted]

after "deploy", "bundler:bundle_install"
after "bundler:bundle_install", "db:db_migrate"
after "deploy:db_migrate", "deploy:elastic_search_indexing"

namespace :bundler do
  desc 'call bundle install'
  task :bundle_install do
    run "cd #{deploy_to}/current && bundle install"
  end
end

namespace :db do
  desc 'fire the db migrations'
  task :db_migrate do
    run "cd #{deploy_to}/current && bundle exec rake db:migrate RAILS_ENV=\"production\""
  end
end

namespace :elasticsearch do
  desc 'run elasticsearch indexing via tire'
  task :index_classes do
    run "cd #{deploy_to}/current && bundle exec rake environment tire:import CLASS=YourObject FORCE=true "
  end
end

[rest omitted]

Make sure you have a config file on the target machine (Linux) in /etc/elasticsearch/elasticsearch.yml with contents like:

cluster:
   name:   elasticsearch_server

network:
   host:   66.98.23.12

And the last point to mention is that you should create an initializer config/initializers/tire.rb:

if Rails.env == 'production'
  Tire.configure do
    url "http://66.98.23.12:9200"
  end
end

As you can see, this is the exact same IP address, but only used for the production environment. I assume that you access elasticsearch locally (in development mode) via localhost. elasticsearch is connection per default to

 http://0.0.0.0:9200

A good starting point and also in depth help is provided by awesome Ryan Bates and his Railscasts http://railscasts.com/episodes?utf8=%E2%9C%93&search=capistrano

Upvotes: 2

Gareth
Gareth

Reputation: 138042

The best way I've found is to use the Foreman gem to kickstart your project and associated processes.

Upvotes: 3

Roger
Roger

Reputation: 7612

Whats keeping you from putting it in a bash script? And put the script inside your RAILS_APP_HOME/scripts folder?

#!/bin/sh
git pull
bundle install
rake db:migrate 
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true

Upvotes: 0

Related Questions