monicak
monicak

Reputation: 45

rs.stepDown using pymongo

I am trying to step down MongoDB primary using python script.I see the below on my output. Is there a way I can mark the exit code as OK.

Code:

 if 'primary' in isMaster:
        primary =(isMaster['primary']).split(':')[0]
        conn = pymongo.MongoClient('mongodb://'+ primary +':10000 replicaSet=test-ipe1')
        stepdown = conn.admin.command("replSetStepDown",100)
        if 'ismaster' in isMaster == 'true':
            print("I am still the primary")
        else:
            print("I am no longer the primary")
    else:
        primary = "No primary currently elected."

Traceback:

Traceback (most recent call last):

File "./repldown.py", line 39, in <module>
    stepdown = conn.admin.command("replSetStepDown",60)

 File "/usr/local/lib64/python2.6/site-packages/pymongo/database.py", line 391, in command
    result = self["$cmd"].find_one(command, **extra_opts)

File "/usr/local/lib64/python2.6/site-packages/pymongo/collection.py", line 604, in find_one
    for result in self.find(spec_or_id, *args, **kwargs).limit(-1):

File "/usr/local/lib64/python2.6/site-packages/pymongo/cursor.py", line 904, in next
    if len(self.__data) or self._refresh():

File "/usr/local/lib64/python2.6/site-packages/pymongo/cursor.py", line 848, in _refresh
    self.__uuid_subtype))

 File "/usr/local/lib64/python2.6/site-packages/pymongo/cursor.py", line 782, in __send_message
    res = client._send_message_with_response(message, **kwargs)

File "/usr/local/lib64/python2.6/site-packages/pymongo/mongo_client.py", line 1051, in _send_message_with_response

raise AutoReconnect(str(e))
pymongo.errors.AutoReconnect: connection closed

Upvotes: 0

Views: 666

Answers (1)

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24007

Yes, do:

from pymongo.errors import AutoReconnect

try:
    stepdown = conn.admin.command("replSetStepDown",100)
except AutoReconnect:
    pass

The primary deliberately closes all connections immediately when it steps down, so the command throws an exception. Just ignore it and continue.

Upvotes: 2

Related Questions