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
1c902435
Commit
1c902435
authored
Jun 10, 2013
by
Michael Cetrulo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove decorator and implement parametrized logger
parent
db818a64
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
28 deletions
+24
-28
mongodb_proxy.py
+24
-28
No files found.
mongodb_proxy.py
View file @
1c902435
...
...
@@ -2,9 +2,6 @@
import
time
import
pymongo
import
logging
logger
=
logging
.
getLogger
(
__name__
)
def
get_methods
(
*
objs
):
return
set
(
attr
...
...
@@ -19,33 +16,26 @@ EXECUTABLE_MONGO_METHODS = get_methods(pymongo.collection.Collection,
pymongo
)
def
safe_mongocall
(
call
):
""" Decorator for automatic handling of AutoReconnect-exceptions.
"""
def
_safe_mongocall
(
*
args
,
**
kwargs
):
for
i
in
xrange
(
4
):
try
:
return
call
(
*
args
,
**
kwargs
)
except
pymongo
.
errors
.
AutoReconnect
:
logger
.
warning
(
'AutoReconnecting, try
%
d'
%
i
)
time
.
sleep
(
pow
(
2
,
i
))
# Try one more time, but this time, if it fails, let the
# exception bubble up to the caller.
return
call
(
*
args
,
**
kwargs
)
return
_safe_mongocall
class
Executable
:
""" Wrap a MongoDB-method and handle AutoReconnect-exceptions
using the safe_mongocall decorator.
"""
def
__init__
(
self
,
method
):
def
__init__
(
self
,
method
,
logger
):
self
.
method
=
method
self
.
logger
=
logger
@safe_mongocall
def
__call__
(
self
,
*
args
,
**
kwargs
):
""" Automatic handling of AutoReconnect-exceptions.
"""
for
i
in
xrange
(
4
):
try
:
return
self
.
method
(
*
args
,
**
kwargs
)
except
pymongo
.
errors
.
AutoReconnect
:
self
.
logger
.
warning
(
'AutoReconnecting, try
%
d'
%
i
)
time
.
sleep
(
pow
(
2
,
i
))
# Try one more time, but this time, if it fails, let the
# exception bubble up to the caller.
return
self
.
method
(
*
args
,
**
kwargs
)
def
__dir__
(
self
):
...
...
@@ -63,16 +53,22 @@ class MongoProxy:
Executable-instance that handles AutoReconnect-exceptions transparently.
"""
def
__init__
(
self
,
conn
):
def
__init__
(
self
,
conn
,
logger
=
None
):
""" conn is an ordinary MongoDB-connection.
"""
if
logger
is
None
:
import
logging
logger
=
logging
.
getLogger
(
__name__
)
self
.
conn
=
conn
self
.
logger
=
logger
def
__getitem__
(
self
,
key
):
""" Create and return proxy around the method in the connection
named "key".
"""
item
=
self
.
conn
[
key
]
if
hasattr
(
item
,
'__call__'
):
...
...
@@ -85,10 +81,10 @@ class MongoProxy:
handles AutoReconnect-Exception.
"""
attr
=
getattr
(
self
.
conn
,
key
)
if
hasattr
(
attr
,
'__call__'
)
and
key
in
EXECUTABLE_MONGO_METHODS
:
return
Executable
(
attr
)
return
Executable
(
attr
,
self
.
logger
)
return
attr
def
__call__
(
self
,
*
args
,
**
kwargs
):
...
...
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