Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Pymongo 3 #205

Merged
merged 3 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions mongodb_store/scripts/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def transform_outgoing_list(self, lst, collection):
class ConfigManager(object):
def __init__(self):
rospy.init_node("config_manager")
rospy.on_shutdown(self._on_node_shutdown)

use_daemon = rospy.get_param('mongodb_use_daemon', False)
db_host = rospy.get_param('mongodb_host')
Expand All @@ -81,6 +80,7 @@ def __init__(self):
sys.exit(1)

self._mongo_client = MongoClient(db_host, db_port)
rospy.on_shutdown(self._on_node_shutdown)

self._database=self._mongo_client.config
self._database.add_son_manipulator(MongoTransformer())
Expand Down Expand Up @@ -207,7 +207,11 @@ def _list_params(self):


def _on_node_shutdown(self):
self._mongo_client.disconnect()
try:
# PyMongo 2.9 or later
self._mongo_client.close()
except Exception as e:
self._mongo_client.disconnect()

# Could just use the ros parameter server to get the params
# but one day might not back onto the parameter server...
Expand Down
27 changes: 17 additions & 10 deletions mongodb_store/src/mongodb_store/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ def check_connection_to_mongod(db_host, db_port):
"""
if check_for_pymongo():
try:
from pymongo import Connection
Connection(db_host, db_port)
return True
except Exception as e:
print("Error: %s" % str(e))
print("Could not connect to mongo server %s:%d" % (db_host, db_port))
print("Make sure mongod is launched on your specified host/port")
try:
# pymongo 2.X
from pymongo import Connection
Connection(db_host, db_port)
return True
except:
# pymongo 3.X
from pymongo import MongoClient
client = MongoClient(db_host, db_port, connect=False)
result = client.admin.command('ismaster')
return True
except pymongo.errors.ConnectionFailure:
rospy.logerr("Could not connect to mongo server %s:%d" % (db_host, db_port))
rospy.logerr("Make sure mongod is launched on your specified host/port")
return False
else:
return False
Expand Down Expand Up @@ -61,9 +68,9 @@ def check_for_pymongo():
try:
import pymongo
except:
print("ERROR!!!")
print("Can't import pymongo, this is needed by mongodb_store.")
print("Make sure it is installed (sudo pip install pymongo)")
rospy.logerr("ERROR!!!")
rospy.logerr("Can't import pymongo, this is needed by mongodb_store.")
rospy.logerr("Make sure it is installed (sudo pip install pymongo)")
return False

return True
Expand Down