forked from mongodb/mongo-perf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongomgr.py
164 lines (142 loc) · 5.64 KB
/
mongomgr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright 2013 10gen, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on a1n "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Manages starting and stopping mongod"""
import os
import subprocess
import time
import sys
import socket
import logging
import logging.handlers
class mongod(object):
def __init__(self, mongod="mongod", logger=None, nojournal=None, port=27017, config_path=None, **kwargs):
self.mongod = mongod
self.logger = logger
self.kwargs = kwargs
self.port = port
self.proc = None
self.nojournal = nojournal
self.config_path = config_path
def __enter__(self):
"""Start mongod
"""
self.start()
def __exit__(self, type, value, traceback):
"""Stop mongod
"""
try:
self.stop()
except Exception, e:
self.logger.error("Error shutting down mongod - {0}".
format(e))
return not isinstance(value, Exception)
def check_mongo_port(self, port=27017):
"""Tries to connect to mongod on given port
"""
sock = socket.socket()
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
sock.settimeout(1)
sock.connect(("localhost", port))
sock.close()
def did_mongod_start(self, port=27017, timeout=300):
"""Checks if mongod started
"""
while timeout > 0:
time.sleep(1)
try:
self.check_mongo_port(port)
return True
except Exception, e:
timeout = timeout - 1
self.logger.error("Timeout starting mongod")
return False
def start(self):
"""Opens a subprocess to start mongod
"""
if self.proc:
self.logger.error("Probable bug: self.proc already" \
" set in start()")
raise Exception("Failed to start mongod")
dbpath = os.getcwd() + "/db"
logpath = dbpath + "/log.txt"
os.makedirs(dbpath)
argv = [self.mongod, "--port", self.port, "--dbpath",
dbpath, "--logpath", logpath]
if self.nojournal is not None:
argv.append("--nojournal")
if self.config_path is not None:
argv.append("--config")
argv.append(self.config_path)
self.proc = self._start(argv)
if not self.did_mongod_start(int(self.port)):
raise Exception("Failed to start mongod")
self.logger.info("Started mongod with args: {0}".
format(" ".join(argv)))
def _start(self, argv):
"""In most cases, just call subprocess.Popen(). On windows,
add the started process to a new Job Object, so that any
child processes of this process can be killed with a single
call to TerminateJobObject (see self.stop()).
"""
proc = subprocess.Popen(argv, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
if os.sys.platform == "win32":
# Create a job object with the "kill on job close"
# flag; this is inherited by child processes (ie
# the mongod started on our behalf by buildlogger)
# and lets us terminate the whole tree of processes
# rather than orphaning the mongod.
import win32job
self.job_object = win32job.CreateJobObject(None, '')
job_info = win32job.QueryInformationJobObject(
self.job_object, win32job.JobObjectExtendedLimitInformation)
job_info['BasicLimitInformation'][
'LimitFlags'] |= win32job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
win32job.SetInformationJobObject(
self.job_object,
win32job.JobObjectExtendedLimitInformation,
job_info)
win32job.AssignProcessToJobObject(self.job_object, proc._handle)
return proc
def stop(self):
"""Stops a running mongod
"""
if not self.proc:
self.logger.error("Probable bug: self.proc already" \
" unset in stop()")
raise Exception("Failed to stop mongod")
return
try:
if os.sys.platform.startswith("win"):
import win32job
win32job.TerminateJobObject(self.job_object, -1)
import time
# Windows doesn't seem to kill the process immediately, so give
# it some time to die
time.sleep(5)
else:
# This actually works
# mongo_executable = os.path.abspath(os.path.join(self.mongod, '..', 'mongo'))
# argv = [mongo_executable, "--port", self.port, "--eval", "db.getSiblingDB('admin').shutdownServer()"]
# proc = subprocess.Popen(argv)
# This function not available in Python 2.5
self.proc.terminate()
except AttributeError:
from os import kill
kill(self.proc.pid, 15)
except Exception, e:
self.logger.error("Error shutting down mongod - {0}".format(e))
sys.exit(1)
self.proc.wait()
sys.stderr.flush()