Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (59 sloc) 2.04 KB
MongoDB transport.
:copyright: (c) 2010 by Flavio Percoco Premoli.
:license: BSD, see LICENSE for more details.
from Queue import Empty
from anyjson import serialize, deserialize
from pymongo import errors
from pymongo.connection import Connection
from kombu.transport import virtual
__author__ = "Flavio [FlaPer87] Percoco Premoli <>"
class Channel(virtual.Channel):
_client = None
def _new_queue(self, queue, **kwargs):
def _get(self, queue):
msg = self.client.database.command("findandmodify",
"messages", query={"queue": queue}, remove=True)
except errors.OperationFailure:
raise Empty()
return deserialize(msg["value"]["payload"])
def _size(self, queue):
return self.client.count()
def _put(self, queue, message, **kwargs):
self.client.insert({"payload": serialize(message), "queue": queue})
def _purge(self, queue):
size = self._size(queue)
self.client.remove({"queue": queue})
return size
def close(self):
super(Channel, self).close()
def _open(self):
conninfo = self.connection.client
mongoconn = Connection(host=conninfo.hostname, port=conninfo.port)
dbname = conninfo.virtual_host
if not dbname or dbname == "/":
dbname = "kombu_default"
database = getattr(mongoconn, dbname)
col = database.messages
col.ensure_index([("queue", 1)])
return col
def client(self):
if self._client is None:
self._client = self._open()
return self._client
class Transport(virtual.Transport):
Channel = Channel
interval = 1
default_port = DEFAULT_PORT
connection_errors = (errors.ConnectionFailure, )
channel_errors = (errors.ConnectionFailure,
errors.OperationFailure, )
Something went wrong with that request. Please try again.