Class: MongoTestServer::Mongod
- Inherits:
-
Object
- Object
- MongoTestServer::Mongod
- Defined in:
- lib/mongo_test_server.rb
Instance Attribute Summary collapse
-
#mongo_instance_id ⇒ Object
readonly
Returns the value of attribute mongo_instance_id.
- #name ⇒ Object
- #path ⇒ Object
- #port ⇒ Object
-
#use_ram_disk ⇒ Object
Returns the value of attribute use_ram_disk.
Class Method Summary collapse
- .configure(options = {}) {|server| ... } ⇒ Object
- .server ⇒ Object
- .start_server ⇒ Object
- .stop_server ⇒ Object
Instance Method Summary collapse
- #after_stop ⇒ Object
- #before_start ⇒ Object
- #configured? ⇒ Boolean
- #error? ⇒ Boolean
-
#initialize(port = nil, name = nil, path = nil) ⇒ Mongod
constructor
A new instance of Mongod.
- #killed=(killing) ⇒ Object
- #killed? ⇒ Boolean
- #mongo_cmd_line ⇒ Object
- #mongo_log ⇒ Object
- #mongo_storage ⇒ Object
- #mongoid3_options(options = {}) ⇒ Object
- #mongoid3_yml(options = {}) ⇒ Object
- #mongoid_options(options = {}) ⇒ Object
- #mongoid_yml(options = {}) ⇒ Object
- #pids ⇒ Object
- #run(command, *args) ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #started=(running) ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #storage ⇒ Object
- #test_connection! ⇒ Object
- #use_ram_disk? ⇒ Boolean
- #wait_until_ready ⇒ Object
Constructor Details
#initialize(port = nil, name = nil, path = nil) ⇒ Mongod
Returns a new instance of Mongod.
59 60 61 62 63 64 65 66 67 |
# File 'lib/mongo_test_server.rb', line 59 def initialize(port=nil, name=nil, path=nil) self.port = port self.path = path self.name = name @mongo_process_or_thread = nil @mongo_instance_id = "#{Time.now.to_i}_#{Random.new.rand(100000..900000)}" @oplog_size = 200 @configured = true end |
Instance Attribute Details
#mongo_instance_id ⇒ Object (readonly)
Returns the value of attribute mongo_instance_id.
56 57 58 |
# File 'lib/mongo_test_server.rb', line 56 def mongo_instance_id @mongo_instance_id end |
#name ⇒ Object
103 104 105 |
# File 'lib/mongo_test_server.rb', line 103 def name @name ||= "#{Random.new.rand(100000..900000)}" end |
#path ⇒ Object
99 100 101 |
# File 'lib/mongo_test_server.rb', line 99 def path @path ||= `which mongod`.chomp end |
#port ⇒ Object
95 96 97 |
# File 'lib/mongo_test_server.rb', line 95 def port @port ||= 27017 end |
#use_ram_disk ⇒ Object
Returns the value of attribute use_ram_disk.
57 58 59 |
# File 'lib/mongo_test_server.rb', line 57 def use_ram_disk @use_ram_disk end |
Class Method Details
.configure(options = {}) {|server| ... } ⇒ Object
26 27 28 29 30 31 |
# File 'lib/mongo_test_server.rb', line 26 def configure(={}, &block) .each do |k,v| server.send("#{k}=",v) if server.respond_to?("#{k}=") end yield(server) if block_given? end |
.server ⇒ Object
33 34 35 |
# File 'lib/mongo_test_server.rb', line 33 def server @mongo_test_server ||= new end |
.start_server ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/mongo_test_server.rb', line 37 def start_server unless @mongo_test_server.nil? @mongo_test_server.start else puts "MongoTestServer not configured properly!" end end |
.stop_server ⇒ Object
45 46 47 48 49 |
# File 'lib/mongo_test_server.rb', line 45 def stop_server unless @mongo_test_server.nil? @mongo_test_server.stop end end |
Instance Method Details
#after_stop ⇒ Object
115 116 117 |
# File 'lib/mongo_test_server.rb', line 115 def after_stop storage.delete end |
#before_start ⇒ Object
111 112 113 |
# File 'lib/mongo_test_server.rb', line 111 def before_start storage.create end |
#configured? ⇒ Boolean
148 149 150 |
# File 'lib/mongo_test_server.rb', line 148 def configured? @configured end |
#error? ⇒ Boolean
144 145 146 |
# File 'lib/mongo_test_server.rb', line 144 def error? File.exists?("#{self.mongo_storage}/error") end |
#killed=(killing) ⇒ Object
138 139 140 141 142 |
# File 'lib/mongo_test_server.rb', line 138 def killed=(killing) if File.directory?(self.mongo_storage) killing ? FileUtils.touch("#{self.mongo_storage}/killed") : FileUtils.rm_f("#{self.mongo_storage}/killed") end end |
#killed? ⇒ Boolean
128 129 130 |
# File 'lib/mongo_test_server.rb', line 128 def killed? !File.directory?(self.mongo_storage) || File.exists?("#{self.mongo_storage}/killed") end |
#mongo_cmd_line ⇒ Object
107 108 109 |
# File 'lib/mongo_test_server.rb', line 107 def mongo_cmd_line "#{self.path} --port #{self.port} --profile 2 --dbpath #{self.mongo_storage} --syncdelay 0 --nojournal --noauth --nohttpinterface --nssize 1 --oplogSize #{@oplog_size} --smallfiles --logpath #{self.mongo_log}" end |
#mongo_log ⇒ Object
91 92 93 |
# File 'lib/mongo_test_server.rb', line 91 def mongo_log "#{storage.path}/mongo_log" end |
#mongo_storage ⇒ Object
87 88 89 |
# File 'lib/mongo_test_server.rb', line 87 def mongo_storage storage.path end |
#mongoid3_options(options = {}) ⇒ Object
244 245 246 |
# File 'lib/mongo_test_server.rb', line 244 def (={}) = {hosts: ["localhost:#{self.port}"], database: "#{self.name}_test_db", use_utc: false, use_activesupport_time_zone: true}.merge() end |
#mongoid3_yml(options = {}) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/mongo_test_server.rb', line 259 def mongoid3_yml(={}) = () mongo_conf_yaml = <<EOY sessions: default: hosts: - #{[:hosts].first} database: #{[:database]} use_utc: #{[:use_utc]} use_activesupport_time_zone: #{[:use_activesupport_time_zone]} EOY end |
#mongoid_options(options = {}) ⇒ Object
240 241 242 |
# File 'lib/mongo_test_server.rb', line 240 def (={}) = {host: "localhost", port: self.port, database: "#{self.name}_test_db", use_utc: false, use_activesupport_time_zone: true}.merge() end |
#mongoid_yml(options = {}) ⇒ Object
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/mongo_test_server.rb', line 248 def mongoid_yml(={}) = () mongo_conf_yaml = <<EOY host: #{[:host]} port: #{[:port]} database : #{[:database]} use_utc: #{[:use_utc]} use_activesupport_time_zone: #{[:use_activesupport_time_zone]} EOY end |
#pids ⇒ Object
226 227 228 229 |
# File 'lib/mongo_test_server.rb', line 226 def pids pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.mongo_storage} | grep -v grep | awk '{print \$1}'`.chomp pids.split("\n").map {|p| (p.nil? || p=='') ? nil : p.to_i } end |
#run(command, *args) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mongo_test_server.rb', line 165 def run(command, *args) error_file = Tempfile.new('error') error_filepath = error_file.path error_file.close args = args.join(' ') rescue '' command << " #{args}" unless args.empty? result = `#{command} 2>"#{error_filepath}"` unless killed? || $?.success? = <<-ERROR <#{self.class.name}> Error executing command: #{command} <#{self.class.name}> Result is: #{IO.binread(self.mongo_log) rescue "No mongo log on disk"} <#{self.class.name}> Error is: #{File.read(error_filepath) rescue "No error file on disk"} ERROR File.open("#{self.mongo_storage}/error", 'w') do |f| f << end self.killed=true end result end |
#running? ⇒ Boolean
119 120 121 122 |
# File 'lib/mongo_test_server.rb', line 119 def running? pids = `ps ax | grep mongod | grep #{self.port} | grep #{self.mongo_storage} | grep -v grep | awk '{print \$1}'`.chomp !pids.empty? end |
#start ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/mongo_test_server.rb', line 152 def start unless started? before_start if RUBY_PLATFORM=='java' @mongo_process_or_thread = Thread.new { run(mongo_cmd_line) } else @mongo_process_or_thread = fork { run(mongo_cmd_line) } end wait_until_ready end self end |
#started=(running) ⇒ Object
132 133 134 135 136 |
# File 'lib/mongo_test_server.rb', line 132 def started=(running) if File.directory?(self.mongo_storage) running ? FileUtils.touch("#{self.mongo_storage}/started") : FileUtils.rm_f("#{self.mongo_storage}/started") end end |
#started? ⇒ Boolean
124 125 126 |
# File 'lib/mongo_test_server.rb', line 124 def started? File.directory?(self.mongo_storage) && File.exists?("#{self.mongo_storage}/started") end |
#stop ⇒ Object
231 232 233 234 235 236 237 238 |
# File 'lib/mongo_test_server.rb', line 231 def stop mongo_pids = pids self.killed = true self.started = false mongo_pids.each { |ppid| `kill -9 #{ppid} 2> /dev/null` } after_stop self end |
#storage ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/mongo_test_server.rb', line 77 def storage @storage ||= if use_ram_disk? $stderr.puts "MongoTestServer: using ram disk storage" RamDiskStorage.new(@name) else $stderr.puts "MongoTestServer: using tmp disk storage" TmpStorage.new(@name) end end |
#test_connection! ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mongo_test_server.rb', line 186 def test_connection! if defined?(Mongo) c = Mongo::Connection.new("localhost", self.port) c.close elsif defined?(Moped) session = Moped::Session.new(["localhost:#{self.port}"]) session.disconnect else raise Exeption.new "No mongo driver loaded! Only the official mongo driver and the moped driver are supported" end end |
#use_ram_disk? ⇒ Boolean
73 74 75 |
# File 'lib/mongo_test_server.rb', line 73 def use_ram_disk? @use_ram_disk end |
#wait_until_ready ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/mongo_test_server.rb', line 198 def wait_until_ready retries = 10 begin self.started = true test_connection! rescue Exception => e if retries>0 && !killed? && !error? retries -= 1 sleep 0.5 retry else self.started = false error_lines = [] error_lines << "<#{self.class.name}> cmd was: #{mongo_cmd_line}" error_lines << "<#{self.class.name}> ERROR: Failed to connect to mongo database: #{e.}" begin IO.binread(self.mongo_log).split("\n").each do |line| error_lines << "<#{self.class.name}> #{line}" end rescue Exception => e error_lines << "No mongo log on disk at #{self.mongo_log}" end stop raise Exception.new error_lines.join("\n") end end end |