Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
MongoDBProxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
MongoDBProxy
Commits
ed7e512a
Commit
ed7e512a
authored
Apr 28, 2013
by
Gustav Arngården
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First checkin
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
mongodb_proxy.py
+76
-0
No files found.
mongodb_proxy.py
0 → 100644
View file @
ed7e512a
import
time
import
pymongo
EXECUTABLE_MONGO_METHODS
=
set
([
typ
for
typ
in
dir
(
pymongo
.
collection
.
Collection
)
if
not
typ
.
startswith
(
'_'
)])
EXECUTABLE_MONGO_METHODS
.
update
(
set
([
typ
for
typ
in
dir
(
pymongo
.
Connection
)
if
not
typ
.
startswith
(
'_'
)]))
EXECUTABLE_MONGO_METHODS
.
update
(
set
([
typ
for
typ
in
dir
(
pymongo
)
if
not
typ
.
startswith
(
'_'
)]))
def
safe_mongocall
(
call
):
""" Decorator for automatic handling of AutoReconnect-exceptions.
"""
def
_safe_mongocall
(
*
args
,
**
kwargs
):
for
i
in
xrange
(
5
):
try
:
return
call
(
*
args
,
**
kwargs
)
except
pymongo
.
errors
.
AutoReconnect
:
print
'AutoReconnecting, try'
,
i
time
.
sleep
(
pow
(
2
,
i
))
print
'Error: Failed operation!'
return
_safe_mongocall
class
Executable
:
""" Wrap a MongoDB-method and handle AutoReconnect-exceptions
using the safe_mongocall decorator.
"""
def
__init__
(
self
,
method
):
self
.
method
=
method
@safe_mongocall
def
__call__
(
self
,
*
args
,
**
kwargs
):
return
self
.
method
(
*
args
,
**
kwargs
)
class
MongoProxy
:
""" Proxy for MongoDB connection.
Methods that are executable, i.e find, insert etc, get wrapped in an
Executable-instance that handles AutoReconnect-exceptions transparently.
"""
def
__init__
(
self
,
conn
):
""" conn is an ordinary MongoDB-connection.
"""
self
.
conn
=
conn
def
__getitem__
(
self
,
key
):
""" Create and return proxy around the method in the connection
named "key".
"""
return
MongoProxy
(
getattr
(
self
.
conn
,
key
))
def
__getattr__
(
self
,
key
):
""" If key is the name of an executable method in the MongoDB connection,
for instance find or insert, wrap this method in Executable-class that
handles AutoReconnect-Exception.
"""
if
key
in
EXECUTABLE_MONGO_METHODS
:
return
Executable
(
getattr
(
self
.
conn
,
key
))
return
self
[
key
]
def
__call__
(
self
,
*
args
,
**
kwargs
):
return
self
.
conn
(
*
args
,
**
kwargs
)
def
__dir__
(
self
):
return
dir
(
self
.
conn
)
def
__repr__
(
self
):
return
self
.
conn
.
__repr__
()
def
__nonzero__
(
self
):
return
True
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment