Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit a200f63

Browse files
author
Kristina Chodorow
committed
add mongos cmd line support for startup
1 parent c649cd8 commit a200f63

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

handlers.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ class MongoHandler:
2727

2828
_cursor_id = 0
2929

30+
def __init__(self, mongos):
31+
for host in mongos:
32+
args = MongoFakeFieldStorage({"server" : host})
33+
34+
out = MongoFakeStream()
35+
36+
name = host.replace(".", "")
37+
name = name.replace(":", "")
38+
39+
self._connect(args, out.ostream, name = name)
40+
3041
def _get_connection(self, name = None, host = None, port = None):
3142
if name == None:
3243
name = "default"
@@ -108,7 +119,7 @@ def _cmd(self, args, out, name = None, db = None, collection = None):
108119
out('{"ok" : 0, "errmsg" : "wasn\'t connected to the db and '+
109120
'couldn\'t reconnect", "name" : "%s"}' % name)
110121
return
111-
except OperationFailure, error:
122+
except (OperationFailure, error):
112123
out('{"ok" : 0, "errmsg" : "%s"}' % error)
113124
return
114125

@@ -262,6 +273,12 @@ def __output_results(self, cursor, out, batch_size=15):
262273
try:
263274
while len(batch) < batch_size:
264275
batch.append(cursor.next())
276+
except AutoReconnect:
277+
out(json.dumps({"ok" : 0, "errmsg" : "auto reconnecting, please try again"}))
278+
return
279+
except OperationFailure as of:
280+
out(json.dumps({"ok" : 0, "errmsg" : "%s" % of}))
281+
return
265282
except StopIteration:
266283
# this is so stupid, there's no has_next?
267284
pass

httpd.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class MongoHTTPRequest(BaseHTTPRequestHandler):
6464
"ico" : "image/vnd.microsoft.icon" }
6565

6666
docroot = "."
67+
mongos = None
6768

6869
def _parse_call(self, uri):
6970
"""
@@ -207,7 +208,7 @@ def serve_forever(port):
207208
print "--------Secure Connection--------\n"
208209
server = MongoServer(('', port), MongoHTTPSRequest)
209210

210-
MongoHandler.mh = MongoHandler()
211+
MongoHandler.mh = MongoHandler(MongoHTTPRequest.mongos)
211212

212213
print "listening for connections on http://localhost:27080\n"
213214
try:
@@ -234,7 +235,7 @@ def usage():
234235
if __name__ == "__main__":
235236

236237
try:
237-
opts, args = getopt.getopt(sys.argv[1:], "d:s:", ["docroot=", "secure="])
238+
opts, args = getopt.getopt(sys.argv[1:], "d:s:m:", ["docroot=", "secure=", "mongos="])
238239

239240
for o, a in opts:
240241
if o == "-d" or o == "--docroot":
@@ -244,11 +245,12 @@ def usage():
244245
if o == "-s" or o == "--secure":
245246
MongoServer.pem = a
246247
if o == "-m" or o == "--mongos":
247-
MongoServer.mongo = a.split(',')
248+
MongoHTTPRequest.mongos = a.split(',')
248249

249250
except getopt.GetoptError:
250251
print "error parsing cmd line args."
251252
usage()
252253
sys.exit(2)
253254

254255
MongoHTTPRequest.serve_forever(27080)
256+

0 commit comments

Comments
 (0)