11#!/usr/bin/ruby
22
3+ require 'thread'
4+
35STDOUT . sync = true
46
57unless defined? Mongo
@@ -17,6 +19,7 @@ def initialize(opts={})
1719 @host = opts [ :host ] || 'localhost'
1820 @retries = opts [ :retries ] || 60
1921 @config = { "_id" => @name , "members" => [ ] }
22+ @durable = opts . fetch ( :durable , false )
2023 @path = File . join ( File . expand_path ( File . dirname ( __FILE__ ) ) , "data" )
2124
2225 @arbiter_count = opts [ :arbiter_count ] || 2
@@ -96,19 +99,21 @@ def init_node(n)
9699 def start_cmd ( n )
97100 @mongods [ n ] [ 'start' ] = "mongod --replSet #{ @name } --logpath '#{ @mongods [ n ] [ 'log_path' ] } ' " +
98101 " --dbpath #{ @mongods [ n ] [ 'db_path' ] } --port #{ @mongods [ n ] [ 'port' ] } --fork"
102+ @mongods [ n ] [ 'start' ] += " --dur" if @durable
103+ @mongods [ n ] [ 'start' ]
99104 end
100105
101- def kill ( node )
106+ def kill ( node , signal = 2 )
102107 pid = @mongods [ node ] [ 'pid' ]
103108 puts "** Killing node with pid #{ pid } at port #{ @mongods [ node ] [ 'port' ] } "
104- system ( "kill -2 #{ @mongods [ node ] [ 'pid' ] } " )
109+ system ( "kill -#{ signal } #{ @mongods [ node ] [ 'pid' ] } " )
105110 @mongods [ node ] [ 'up' ] = false
106111 sleep ( 1 )
107112 end
108113
109- def kill_primary
114+ def kill_primary ( signal = 2 )
110115 node = get_node_with_state ( 1 )
111- kill ( node )
116+ kill ( node , signal )
112117 return node
113118 end
114119
@@ -184,6 +189,16 @@ def arbiters
184189 get_all_host_pairs_with_state ( 7 )
185190 end
186191
192+ # String used for adding a shard via mongos
193+ # using the addshard command.
194+ def shard_string
195+ str = "#{ @name } /"
196+ str << @mongods . map do |k , mongod |
197+ "#{ @host } :#{ mongod [ 'port' ] } "
198+ end . join ( ',' )
199+ str
200+ end
201+
187202 private
188203
189204 def initiate
@@ -239,13 +254,13 @@ def attempt
239254 while count < @retries do
240255 begin
241256 return yield
242- rescue Mongo ::OperationFailure , Mongo ::ConnectionFailure
257+ rescue Mongo ::OperationFailure , Mongo ::ConnectionFailure => ex
243258 sleep ( 1 )
244259 count += 1
245260 end
246261 end
247262
248- raise exception
263+ raise ex
249264 end
250265
251266end
0 commit comments