2015-06-09 22:09:28 +02:00
|
|
|
'''
|
|
|
|
Created on 09.06.2015
|
|
|
|
|
|
|
|
@author: wn
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
import threading
|
|
|
|
from logger import Logger
|
|
|
|
import pymongo
|
|
|
|
|
|
|
|
class MongoWriter(threading.Thread):
|
|
|
|
def __init__(self, queue, dbhost, database, collection):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.queue = queue
|
|
|
|
self.dbhost = dbhost
|
|
|
|
self.database = database
|
|
|
|
self.collection = collection
|
|
|
|
self.setDaemon(True)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
msg = self.queue.get()
|
|
|
|
# Logger.debug("MongoWriter receives: %s" % msg)
|
|
|
|
mongoClient = pymongo.MongoClient(self.dbhost)
|
|
|
|
db = mongoClient[self.database]
|
|
|
|
res = db[self.collection].insert_one(msg)
|
2016-10-31 16:27:39 +01:00
|
|
|
Logger.log("MongoWriter inserts: %s" % res.inserted_id)
|
2015-06-09 22:09:28 +02:00
|
|
|
except pymongo.errors.ServerSelectionTimeoutError, e:
|
|
|
|
Logger.log("Exception %s in MongoWriter, run" % str(e))
|
|
|
|
Logger.log("Msg dropped: %s" % msg)
|
|
|
|
except TypeError, e:
|
|
|
|
Logger.log("Exception %s in MongoWriter, run" % str(e))
|
|
|
|
except Exception, e:
|
2016-10-31 16:27:39 +01:00
|
|
|
Logger.log("Unexcepted exception %s in MongoWriter: %s" % (e.__class__.__name__, str(e)))
|