def transactions(groups): success, status, db, cursor = 0, None, None, None try: db = MysqlServer.connect() cursor = db.cursor() flag = 0 for query in groups: if query[0] and query[1]: status = cursor.executemany(query=query[0], args=query[1]) flag += 1 if flag > 0: db.commit() success = 1 except Exception as e: if db: db.rollback() status = traceback.format_exc() WarningMsg.sent(warning_title_mysql, warning_text_mysql % repr(e)) finally: if cursor: cursor.close() if db: db.close() return success, status
def update(sql, retry=3): with _th_lock: success = 0 status = 0 while retry: try: mysql_cli = MysqlServer.connect() status = mysql_cli.update(sql) success = 1 break except torndb.IntegrityError as ie: print ie success = 0 status = ie break except Exception as e: retry -= 1 if retry > 0: time.sleep(10) continue else: status = traceback.format_exc() WarningMsg.sent(warning_title_mysql, warning_text_mysql % repr(e)) break return success, status
def hset(name, key, value): try: redis_cli = RedisServer.connect() status = redis_cli.hset(name, key, value) except Exception as e: WarningMsg.sent(warning_title_redis, warning_text_redis % repr(e)) raise e return status
def hgetall(name): try: redis_cli = RedisServer.connect() value = redis_cli.hgetall(name) except Exception as e: WarningMsg.sent(warning_title_redis, warning_text_redis % repr(e)) raise e return value
def exists(name): try: redis_cli = RedisClusterServer.connect() status = redis_cli.exists(name) except Exception as e: WarningMsg.sent(warning_title_rediscluster, warning_text_rediscluster % repr(e)) raise e return status
def connect(): try: if not hasattr(RedisServer, '_connect_pool'): RedisServer.__create_pool() return redis.Redis(connection_pool=RedisServer._connect_pool) except Exception as e: WarningMsg.sent(warning_title_redis, warning_text_redis % repr(e)) raise e
def hget(name, key): try: redis_cli = RedisClusterServer.connect() value = redis_cli.hget(name, key) except Exception as e: WarningMsg.sent(warning_title_rediscluster, warning_text_rediscluster % repr(e)) raise e return value
def rename(src, dst): try: redis_cli = RedisServer.connect() status = redis_cli.rename(src, dst) except Exception as e: WarningMsg.sent(warning_title_redis, warning_text_redis % repr(e)) raise e return status
def ltrim(name, start, end): try: redis_cli = RedisServer.connect() status = redis_cli.lrange(name, start, end) redis_cli.ltrim(name, end+1, -1) except Exception as e: WarningMsg.sent(warning_title_redis, warning_text_redis % repr(e)) raise e return status
def fetchall(query, args=None): success, status, db, cursor = 0, None, None, None try: db = MysqlServer.connect() cursor = db.cursor() status = cursor.execute(query=query, args=args) status = cursor.fetchall() success = 1 except Exception as e: status = traceback.format_exc() WarningMsg.sent(warning_title_mysql, warning_text_mysql % repr(e)) finally: if cursor: cursor.close() if db: db.close() return success, status
def get(sql, retry=3): success = 0 status = [] while retry: try: mysql_cli = MysqlServer.connect() status = mysql_cli.get(sql) success = 1 break except Exception as e: retry -= 1 if retry > 0: time.sleep(10) continue else: status = traceback.format_exc() WarningMsg.sent(warning_title_mysql, warning_text_mysql % repr(e)) break return success, status
def insertmany(sql, data, retry=3): with _th_lock: success = 0 status = 0 while retry: try: mysql_cli = MysqlServer.connect() status = mysql_cli.insertmany(sql, data) success = 1 break except Exception as e: retry -= 1 if retry > 0: time.sleep(10) continue else: status = traceback.format_exc() WarningMsg.sent(warning_title_mysql, warning_text_mysql % repr(e)) break return success, status