Created
August 14, 2009 03:15
-
-
Save neilfws/167620 to your computer and use it in GitHub Desktop.
Start/stop mongod for a Rails app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # mongodb.rake for Rails | |
| # assumes that mongodb installed in Rails.root/lib/mongodb | |
| namespace :db do | |
| namespace :mongo do | |
| # start db server | |
| task :start do | |
| pr = get_pids | |
| if pr.length == 0 | |
| sh "#{Rails.root}/lib/mongodb/bin/mongod run --dbpath=#{Rails.root}/db/ >> #{Rails.root}/log/mongodb.log &" | |
| puts "mongod running - press return." | |
| else | |
| puts "mongod already running - rake db:mongo:stop first." | |
| end | |
| end | |
| # stop db server | |
| task :stop do | |
| pr = get_pids | |
| if pr.length > 0 | |
| pr.each do |p| | |
| begin | |
| Process.kill(2,p) | |
| rescue SystemCallError => e | |
| raise e | |
| else | |
| puts "Killed mongod #{p} with signal 2" | |
| end | |
| end | |
| else | |
| puts "No mongod process is running." | |
| end | |
| end | |
| end | |
| end | |
| # return array with mongod PIDs (or empty) | |
| def get_pids | |
| require 'sys/proctable' | |
| include Sys | |
| procs = Array.new | |
| ProcTable.ps {|p| | |
| if p.name == "mongod" | |
| procs << p.pid | |
| end | |
| } | |
| return procs | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment