TokyoTyrant serverを起動/終了など
gemにした → http://shokai.org/blog/archives/5219
複数DB使うために設定ファイルを書いておく
config.yaml
# tokyotyrant config ttdb : - name : pages port : 20005 - name : olds port : 20006 - name : uploads port : 20007
rakeでまとめて起動/終了させる
Rakefile
require 'rubygems' $VERBOSE = nil def loadconf begin conf = YAML::load open(File.dirname(__FILE__)+'/config.yaml') rescue puts 'config.yaml load error' end conf['ttdb'] = conf['ttdb'].map{|db| name = db['name'] port = db['port'].to_i basedir = File.dirname(__FILE__)+'/ttdb' { :cmd => 'ttserver', :basedir => basedir, :name => name, :port => port, :pidfile => "#{basedir}/#{name}.pid", :dbname => "#{basedir}/#{name}.tch#bnum=1000000" } } conf end desc 'start TokyoTyrant server' task 'ttstart' do exit 1 if(conf = loadconf) == nil puts 'starting TokyoTyrant servers..' for tt in conf['ttdb'] do Dir.mkdir(tt[:basedir]) if !File.exists?(tt[:basedir]) puts '' puts "name=>#{tt[:name]}, port=>#{tt[:port]}, pid=>#{tt[:pidfile]}" if File.exists?(tt[:pidfile]) pid = open(tt[:pidfile]).read puts "existing process - pid:#{pid}" else puts `#{tt[:cmd]} -port #{tt[:port]} -dmn -pid #{tt[:pidfile]} #{tt[:dbname]}` puts 'done' end end end desc 'stop TokyoTyrant server' task 'ttstop' do exit 1 if(conf = loadconf) == nil puts 'stopping TokyoTyrant servers..' for tt in conf['ttdb'] do if File.exists?(tt[:pidfile]) pid = open(tt[:pidfile]).read puts '' puts "name=>#{tt[:name]}, port=>#{tt[:port]}, pid=>#{pid}" puts `kill -TERM #{pid}` count = 0 loop do sleep 0.1 if !File.exists?(tt[:pidfile]) puts 'done' break end if (count+=1) > 100 puts "hanging process - pid:#{pid}" break end end else puts 'no process found' end end end desc 'restart TokyoTyrant server' task 'ttrestart' => ['ttstop', 'ttstart'] desc 'run spec' require 'spec' require 'spec/rake/spectask' Spec::Rake::SpecTask.new do |t| t.spec_opts = ['-c', '-fs'] t.spec_files = Dir.glob(File.dirname(__FILE__)+'/spec/*_spec.rb') end task :default => [:spec]
DBに接続する
test.rb
require 'rubygems' require 'yaml' require 'tokyotyrant' include TokyoTyrant begin conf = YAML::load open(File.dirname(__FILE__) + '/config.yaml') rescue puts 'config.yaml load error' exit 1 end dbs = Hash.new for db in conf['ttdb'] do rdb = RDB::new if !rdb.open('127.0.0.1', db['port']) STDERR.puts "tokyotyrant : #{rdb.errmsg(rdb.ecode)}" exit 1 end dbs[db['name'].to_sym] = rdb end pages = dbs[:pages] uploads = dbs[:uploads] olds = dbs[:olds]