capistranoでthinking_shinxを扱う方法

capistranoでthinking_shinxを扱う方法についてです。
基本sshでのコマンド実行なので仕組みは簡単です。

ポイント

検索indexファイル

db/sphinx/production/ に検索indexファイルが作成されるので
これをcapistranoのshared配下で管理します。
log、pidと同様に、
shared内にディレクトリ作って、それに対してsymbolic linkを作成します。

|-- shared
      |-- log
      |-- pid
      |-- sphinx   # <- これを作成
      `-- system


この部分が該当。
deploy:create_db_dir

  desc 'Create a directory to store the sphinx indexes'
  task :create_db_dir, :roles => :app do
    run "mkdir -p #{shared_path}/sphinx"
  end


これをdeploy:setup前に実行するようにします。

before "deploy:setup", "deploy:create_db_dir"


また、ソースを最新(deploy:update_code) にした後に
symblic linkの作成を行います。
deploy:symllink_sphinx_indexes

  desc "Link up Sphinx's indexes"
  task :symlink_sphinx_indexes, :roles => [:app] do
    # If current_path doesn't work for you, use release_path.
    run "ln -nfs #{shared_path}/sphinx #{release_path}/db/sphinx"
  end

あとはデプロイ時にshinxのコマンドを実行

namespace :thinking_sphinx do
  [:start, :stop, :configure, :index, :rebuild].each do |t|
    task t, :roles => [:app] do
      run "cd #{release_path}; rake thinking_sphinx:#{t} RAILS_ENV=#{rails_env}"
    end
  end
end


update_code後の詳細は要件に合わせてでしょうが、
私は下記のようなコマンドを実施させています。

after "deploy:update_code" do
  deploy.change_executable_bt      # shellに実行権限を付与
  deploy.symlink_sphinx_indexes    # sphinxのindexへのsymbolic link作成
  thinking_sphinx.configure        # sphinxのconfファイル再作成
  thinking_sphinx.stop             # sphinx停止
  thinking_sphinx.start            # sphinx起動
end