Permalink
Browse files

Start sharded mongo task. Upgraded RSpec to 2.8

  • Loading branch information...
1 parent 84b2682 commit 3faa94a69767916bc5d2d78b8768a3d4ad02bf3e @rosic rosic committed Jan 10, 2012
Showing with 95 additions and 12 deletions.
  1. +2 −0 Gemfile
  2. +14 −12 Gemfile.lock
  3. +72 −0 lib/tasks/mongo.rake
  4. +7 −0 lib/tasks/sharded.js
View
@@ -50,3 +50,5 @@ group :test, :development do
gem 'guard-rspec'
gem 'guard-migrate'
end
+
+gem 'sys-proctable'
View
@@ -161,20 +161,20 @@ GEM
json (~> 1.4)
rest-client (1.6.7)
mime-types (>= 1.16)
- rspec (2.7.0)
- rspec-core (~> 2.7.0)
- rspec-expectations (~> 2.7.0)
- rspec-mocks (~> 2.7.0)
- rspec-core (2.7.1)
- rspec-expectations (2.7.0)
+ rspec (2.8.0)
+ rspec-core (~> 2.8.0)
+ rspec-expectations (~> 2.8.0)
+ rspec-mocks (~> 2.8.0)
+ rspec-core (2.8.0)
+ rspec-expectations (2.8.0)
diff-lcs (~> 1.1.2)
rspec-instafail (0.2.1)
- rspec-mocks (2.7.0)
- rspec-rails (2.7.0)
- actionpack (~> 3.0)
- activesupport (~> 3.0)
- railties (~> 3.0)
- rspec (~> 2.7.0)
+ rspec-mocks (2.8.0)
+ rspec-rails (2.8.1)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ railties (>= 3.0)
+ rspec (~> 2.8.0)
ruby_parser (2.3.1)
sexp_processor (~> 3.0)
rubyzip (0.9.5)
@@ -199,6 +199,7 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
+ sys-proctable (0.9.1)
tenacity (0.5.4)
activesupport (>= 2.3)
therubyracer (0.9.9)
@@ -259,6 +260,7 @@ DEPENDENCIES
sass-rails (~> 3.1.5)
simple_form
spork
+ sys-proctable
tenacity
thin
tire
View
@@ -0,0 +1,72 @@
+#!/usr/bin/env rake
+
+namespace :db do
+
+ namespace :mongo do
+
+ desc "Prepare Mongo for sharding"
+ task :shard_init do
+ `mkdir db/shard_conf` unless File.exists? 'db/shard_conf'
+ `mkdir db/shard_db1` unless File.exists? 'db/shard_db1'
+ `mkdir db/shard_db2` unless File.exists? 'db/shard_db2'
+ `mkdir log/db` unless File.exists? 'log/db'
+ end
+
+ desc "Starts or stops (with [stop] cmd) Mongo with two instances ready for sharding"
+ task :sharded, [:command] => [:shard_init] do |t, args|
+ cmd = args[:command] || :start
+ pr = get_pids
+ if cmd == :start
+ if pr.length == 0
+ `mongod --fork --dbpath db/shard_conf --configsvr --port 29000 --logpath log/db/shard_conf.log --logappend`
+ `mongod --fork --dbpath db/shard_db1 --shardsvr --port 29010 --logpath log/db/shard_db1.log --logappend`
+ `mongod --fork --dbpath db/shard_db2 --shardsvr --port 29020 --logpath log/db/shard_db2.log --logappend`
+ sleep 2
+ system `mongos --fork --configdb localhost:29000 --chunkSize 2 --logpath log/db/shard_router.log --logappend`
+ sleep 2
+ system "mongo localhost:27017/admin #{Rails.root}/lib/tasks/sharded.js"
+ puts "Started config (29000), two shards (29010, 29020) and shard router (27017)"
+ else
+ puts "mongod already running"
+ end
+ else
+ kill_all 'mongod'
+ kill_all 'mongos'
+ end
+ end
+
+ def kill_all(name)
+ pr = get_pids(name)
+ if pr.length > 0
+ pr.each do |p|
+ begin
+ Process.kill(2,p)
+ rescue SystemCallError => e
+ raise e
+ else
+ puts "Killed #{name} #{p} with signal 2"
+ end
+ end
+ else
+ puts "No #{name} process is running."
+ end
+ end
+
+ # return array with PIDs (or empty)
+ def get_pids(process_name = "mongod")
+ require 'sys/proctable'
+ include Sys
+
+ procs = Array.new
+
+ ProcTable.ps {|p|
+ if p.comm == process_name
+ procs << p.pid
+ end
+ }
+ return procs
+ end
+
+ end
+
+end
View
@@ -0,0 +1,7 @@
+var shards = db.runCommand( { listShards: 1 });
+if(shards["shards"].length === 0) {
+ db.runCommand( { addshard: "localhost:29010" } );
+ db.runCommand( { addshard: "localhost:29020" } );
+ db.runCommand( { enablesharding: "versative_mongo_development" } );
+ db.runCommand( { shardcollection: "versative_mongo_development.work_items", key : {project_id : 1} } );
+}

0 comments on commit 3faa94a

Please sign in to comment.