示例#1
0
    def _post(self, messages=None):

        if messages:
            r3_list = []
            for r in messages:
                r2 = decode_tile_request_log(r)
                if self.verbose:
                    print "Logging: ",r2
                r3 = buildTileRequestDocument(** r2)
                r3_list.append(r3)

            #print "_m_client: ",self._m_client 
            #print "_m_db: ",self._m_db 
            self._m_db[self._m_collection].insert(r3_list, w=0)
            for r3 in r3_list:
                self._producer_stats.send_json(data=buildStats(self._list_stats, r3))
示例#2
0
def logTileRequest_old(tileorigin, tilesource, x, y, z, status, datetime, ip):
    #starttime = time.clock()
    #==#
    log_root = settings.LOG_REQUEST_ROOT
    #log_format = settings.LOG_REQUEST_FORMAT['tile_request']
    log_format = settings.LOG_REQUEST_FORMAT

    if log_root and log_format:
        #if not os.path.exists(log_root):
        #    os.makedirs(log_root)

        log_file = log_root+os.sep+"requests_tiles_"+datetime.strftime('%Y-%m-%d')+".tsv"

        with open(log_file,'a') as f:
            line = log_format.format(status=status,tileorigin=tileorigin,tilesource=tilesource,z=z,x=x,y=y,ip=ip,datetime=datetime.isoformat())
            f.write(line+"\n")

            # Import Gevent and monkey patch
            from gevent import monkey
            monkey.patch_all()
            # Update MongoDB
            from pymongo import MongoClient
            client = None
            db = None
            r = None
            try:
                client = MongoClient('/tmp/mongodb-27017.sock')
                db = client[settings.TILEJET_DBNAME]
                r = buildTileRequestDocument(
                    tileorigin=tileorigin,
                    tilesource=tilesource,
                    x=x,
                    y=y,
                    z=z,
                    extension=extension,
                    status=status,
                    datetime=datetime,
                    ip=ip)
            except:
                client = None
                db = None
                errorline = "Error: Could not connet to log database. Most likely issue with connection pool"
                error_file = settings.LOG_ERRORS_ROOT+os.sep+"requests_tiles_"+datetime.strftime('%Y-%m-%d')+"_errors.txt"
                with open(error_file,'a') as f:
                    f.write(errorline+"\n")

            # Update Mongo Logs
            if client and db and r:
                try:
                    db[settings.TILEJET_COLLECTION_LOGS].insert(r, w=0)
                except:
                    errorline = "Error: Could not write log entry into database.  Most likely socket issue.  For the following: "+line
                    error_file = settings.LOG_ERRORS_ROOT+os.sep+"requests_tiles_"+datetime.strftime('%Y-%m-%d')+"_errors.txt"
                    with open(error_file,'a') as f:
                        f.write(errorline+"\n")

                # Update Mongo Aggregate Stats
                stats = buildStats(settings.TILEJET_LIST_STATS, r)
                # Sync stats
                if settings.ASYNC_STATS:
                    try:
                        taskIncStats.apply_async(
                            args=[stats],
                            kwargs=None,
                            queue="statistics")
                    except:
                        errorline = "Error: Could not queue taskIncStats.  Most likely issue with rabbitmq."
                        error_file = settings.LOG_ERRORS_ROOT+os.sep+"requests_tiles_"+datetime.strftime('%Y-%m-%d')+"_errors.txt"
                        with open(error_file,'a') as f:
                            f.write(errorline+"\n")
                else:
                    incStats(db, stats)