Пример #1
0
 def get_list_top(cls, key, size=1):
     ret = False
     try:
         ret = cls._db.lrange(key, 0, size - 1)
     except Exception, e:
         Log.warning('Redis pop key %s failed(%s)', key, str(e))
         return False
Пример #2
0
 def pop(cls, key):
     ret = False
     try:
         ret = cls._db.lpop(key)
     except Exception, e:
         Log.warning('Redis pop key %s failed(%s)', key, str(e))
         return False
Пример #3
0
 def __reconnect(cls):
     if cls.__mc_conn is not None:
         try:
             cls.__mc_conn.disconnect_all()
         except Exception, e:
             Log.warning('memcache disconnect(%s)', str(e))
         cls.__mc_conn = None
Пример #4
0
 def set(cls, key, val, time_out = 0):
     ret = True
     try: 
         cls._db.set(key, val) # TODO time_out
     except Exception, e:
         Log.warning('Redis set %s failed(%s)', key, str(e))
         return False
Пример #5
0
 def __reconnect(cls):
     if cls.__mc_conn is not None:
         try:
             cls.__mc_conn.disconnect_all()
         except Exception, e:
             Log.warning('memcache disconnect(%s)', str(e))
         cls.__mc_conn = None
Пример #6
0
 def get(cls, key):
     val = None
     try:
         val = cls._db.get(key)
     except Exception, e:
         Log.warning('Redis get %s failed(%s)', key, str(e))
         return False
Пример #7
0
 def get(cls, key):
     val = None
     try:
         val = cls._db.get(key)
     except Exception, e:
         Log.warning('Redis get %s failed(%s)', key, str(e))
         return False
Пример #8
0
 def get_all_keys(cls):
     ret = []
     try: 
         ret = cls._db.keys()
     except Exception, e:
         Log.warning('Redis get all keys failed(%s)',str(e))
         return False
Пример #9
0
 def get_size(cls):
     ret = 0
     try: 
         ret = cls._db.dbsize()
     except Exception, e:
         Log.warning('Redis get size failed(%s)',str(e))
         return False
Пример #10
0
 def push(cls, key, value):
     ret = False
     try:
         ret = cls._db.rpush(key, value)
     except Exception, e:
         Log.warning('Redis push key %s failed(%s)', key, str(e))
         return False
Пример #11
0
 def pop(cls, key):
     ret = False
     try: 
         ret = cls._db.lpop(key)
     except Exception, e:
         Log.warning('Redis pop key %s failed(%s)',key, str(e))
         return False
Пример #12
0
 def is_key_exist(cls, key):
     ret = False
     try:
         ret = cls._db.exists(key)
     except Exception, e:
         Log.warning('Redis is key exist %s failed(%s)', key, str(e))
         return False
Пример #13
0
 def get_all_keys(cls):
     ret = []
     try:
         ret = cls._db.keys()
     except Exception, e:
         Log.warning('Redis get all keys failed(%s)', str(e))
         return False
Пример #14
0
 def get_size(cls):
     ret = 0
     try:
         ret = cls._db.dbsize()
     except Exception, e:
         Log.warning('Redis get size failed(%s)', str(e))
         return False
Пример #15
0
 def get_list_top(cls, key, size=1):
     ret = False
     try: 
         ret = cls._db.lrange(key, 0, size-1)
     except Exception, e:
         Log.warning('Redis pop key %s failed(%s)',key, str(e))
         return False
Пример #16
0
 def set(cls, key, val, time_out=0):
     ret = True
     try:
         cls._db.set(key, val)  # TODO time_out
     except Exception, e:
         Log.warning('Redis set %s failed(%s)', key, str(e))
         return False
Пример #17
0
 def push(cls, key, value):
     ret = False
     try: 
         ret = cls._db.rpush(key, value)
     except Exception, e:
         Log.warning('Redis push key %s failed(%s)',key, str(e))
         return False
Пример #18
0
 def is_key_exist(cls, key):
     ret = False
     try: 
         ret = cls._db.exists(key)
     except Exception, e:
         Log.warning('Redis is key exist %s failed(%s)',key, str(e))
         return False
Пример #19
0
 def get_rows_by_slave(self, sql, *parameters, **kwparameters):
     if self._restarting == True:
         return []
     rows = []
     try:
         rows = self._db_slave.query(sql, *parameters, **kwparameters)
     except Exception, e:
         Log.critical('Mysql get rows error(%s)(%s)', sql, str(e))
Пример #20
0
 def get_one_by_slave(self, sql, *parameters, **kwparameters):
     # 目前重连是阻塞状态,所以这个判断不会生效,暂时保留
     if self._restarting == True:
         return None
     row = None
     try:
         row = self._db_slave.get(sql, *parameters, **kwparameters)
     except Exception, e:
         Log.critical('Mysql get rows error(%s)(%s)', sql, str(e))
Пример #21
0
 def delete(cls, key):
     try:
         cls.__mc_conn.delete(key)
     except Exception, e:
         Log.warning('memcache delete %s failed(%s)', key, str(e))
         cls.__reconnect()
         try:
             cls.__mc_conn.delete(key)
         except Exception, e1:
             Log.warning('memcache re-delete %s failed(%s)', key, str(e1))
Пример #22
0
 def get_rows(self, sql, *parameters, **kwparameters):
     if self._restarting == True:
         return []
     rows = []
     try:
         rows = self._db.query(sql, *parameters, **kwparameters)
     except Exception, e:
         Log.critical('Mysql get rows error(%s)(%s)', sql, str(e))
         if self._db is None or str(e).find('connect to MySQL') >= 0 or str(e).find('MySQL server has gone away') >= 0:
             self._restart()
Пример #23
0
 def delete(cls, key):
     try:
         cls.__mc_conn.delete(key)
     except Exception, e:
         Log.warning('memcache delete %s failed(%s)', key, str(e))
         cls.__reconnect()
         try:
             cls.__mc_conn.delete(key)
         except Exception, e1:
             Log.warning('memcache re-delete %s failed(%s)', key, str(e1))
Пример #24
0
 def wrapper(self, *args, **kwargs):
     if isinstance(self, BaseModel) == False:
         return method(self, *args, **kwargs)
     else:
         start = time.clock()
         ret = method(self, *args, **kwargs)
         finsh = time.clock()
         BaseModel.set_debug_info((finsh - start) * 1000)
         if self._debug_flag == True and isinstance(args, tuple) and len(args) > 0:
             Log.debug('SQL-Time: %s[%.3fms]', args[0], (finsh - start) * 1000)
         return ret
Пример #25
0
 def start(cls, host="127.0.0.1", port=27017):
     cls._host = host
     cls._port = port
     #Log.info('Start connect to MongoDB server (%s:%s).', host, port)
     if cls._client is None:
         try:
             cls._client = pymongo.MongoClient(host,port)
         except Exception, e:
             Log.critical('Mongodb(%s:%s) connect failed. \n %s', host, port, str(e))
             sys.exit()
         Log.critical('Mongodb(%s:%s) Ok.', host, port)
Пример #26
0
 def get(cls, key):
     val = None
     try:
         val = cls.__mc_conn.get(key)
     except Exception, e:
         Log.warning('memcache get %s failed(%s)', key, str(e))
         cls.__reconnect()
         try:
             val = cls.__mc_conn.get(key)
         except Exception, e1: 
             val = None
             Log.warning('memcache re-get %s failed(%s)', key, str(e1))
Пример #27
0
 def set(cls, key, val, time_out=0):
     ret = True
     try:
         cls.__mc_conn.set(key, val, time_out)
     except Exception, e:
         Log.warning('memcache set %s failed(%s)', key, str(e))
         cls.__reconnect()
         try:
             cls.__mc_conn.set(key, val, time_out)
         except Exception, e1:
             ret = False
             Log.warning('memcache re-set %s failed(%s)', key, str(e1))
Пример #28
0
 def get(cls, key):
     val = None
     try:
         val = cls.__mc_conn.get(key)
     except Exception, e:
         Log.warning('memcache get %s failed(%s)', key, str(e))
         cls.__reconnect()
         try:
             val = cls.__mc_conn.get(key)
         except Exception, e1:
             val = None
             Log.warning('memcache re-get %s failed(%s)', key, str(e1))
Пример #29
0
 def set(cls, key, val, time_out = 0):
     ret = True
     try: 
         cls.__mc_conn.set(key, val, time_out)
     except Exception, e:
         Log.warning('memcache set %s failed(%s)', key, str(e))
         cls.__reconnect()
         try:
             cls.__mc_conn.set(key, val, time_out)
         except Exception, e1:
             ret = False
             Log.warning('memcache re-set %s failed(%s)', key, str(e1))
Пример #30
0
 def start(cls, host="127.0.0.1", port=27017):
     cls._host = host
     cls._port = port
     #Log.info('Start connect to MongoDB server (%s:%s).', host, port)
     if cls._client is None:
         try:
             cls._client = pymongo.MongoClient(host, port)
         except Exception, e:
             Log.critical('Mongodb(%s:%s) connect failed. \n %s', host,
                          port, str(e))
             sys.exit()
         Log.critical('Mongodb(%s:%s) Ok.', host, port)
Пример #31
0
 def execute(self, sql, *parameters, **kwparameters):
     if self._restarting == True:
         return [False, 0]
     
     ret = [False, 0]
     try:
         ret[1] = self._db.execute_lastrowid(sql, *parameters, **kwparameters)
         ret[0] = True
     except Exception, e:
         Log.critical('Mysql Error(%s)(%s)', sql, str(e))
         if self._db is None or str(e).find('connect to MySQL') >=0 or str(e).find('MySQL server has gone away') >= 0:
             self._restart()
Пример #32
0
 def start(cls, host=None, port=None, password=None):
     
     host = config_base.store['redis']['host']
     port = config_base.store['redis']['port']
     password = config_base.store['redis']['pwd']
     
     #Log.info('Start connect to Redis server (%s:%s).', host, port)
     if cls._pool is None:
         try:
             cls._pool = redis.ConnectionPool(host=host, port=port, password=password)
             cls._db = redis.Redis(connection_pool=cls._pool)
             cls._db.get("test")
         except Exception, e:
             Log.critical('Redis(%s:%s) connect failed. \n %s', host, port, str(e))
             sys.exit()
         Log.critical('Redis(%s:%s) Ok.', host, port)
Пример #33
0
 def _get_slave_db(cls, slaves_conf):
     
     for db_conf in slaves_conf:
         try:
             _db =  torndb.Connection(
                 host = '%s:%s' % (db_conf['host'], db_conf['port']),
                 user = db_conf['uname'],
                 password = db_conf['pwd'],
                 database = db_conf['name'],
                 connect_timeout = db_conf['time_out'],
                 time_zone = "+8:00"
             )
             sql = "SET NAMES 'utf8'"
             _db.execute(sql)
             Log.critical('Mysql Slave(%s:%s) Ok', db_conf['host'], db_conf['host'])
             return _db
         except Exception as e:
             Log.critical('Mysql Slave Error(%s:%s)(%s)', db_conf['host'], db_conf['host'], str(e))
     return cls._db
Пример #34
0
    def start(cls, host=None, port=None, password=None):

        host = config_base.store['redis']['host']
        port = config_base.store['redis']['port']
        password = config_base.store['redis']['pwd']

        #Log.info('Start connect to Redis server (%s:%s).', host, port)
        if cls._pool is None:
            try:
                cls._pool = redis.ConnectionPool(host=host,
                                                 port=port,
                                                 password=password)
                cls._db = redis.Redis(connection_pool=cls._pool)
                cls._db.get("test")
            except Exception, e:
                Log.critical('Redis(%s:%s) connect failed. \n %s', host, port,
                             str(e))
                sys.exit()
            Log.critical('Redis(%s:%s) Ok.', host, port)
Пример #35
0
 def start(cls):
     
     master_conf = config_base.store['db_master']
     debug_flag = config_base.store['db_master']['debug']
     
     slaves_conf = []
     
     if config_base.store.has_key('db_slaves'):
         slaves_conf = config_base.store['db_slaves']
     
     #host, port, user, password, database, time_out = 3, debug_flag = False
     
     if cls._db is None:
         cls._debug_flag = debug_flag
         try:
             cls._db = cls._get_db(master_conf)
             sql = "SET NAMES 'utf8'"
             cls._db.execute(sql)
             Log.critical("Mysql Master(%s:%s) OK" % (master_conf['host'], master_conf['host']))
         except Exception, e:
             cls._db = None
             Log.critical('Mysql Error(%s:%s)(%s)', master_conf['host'], master_conf['host'], str(e))
Пример #36
0
 def start(cls, host, port):
     if cls.__mc_conn is None:
         cls.__mc_conn_str = "%s:%s" % (host, port)
         try:
             cls.__mc_conn = memcache.Client([cls.__mc_conn_str])
             mc_stats = cls.__mc_conn.get_stats()
             if mc_stats is None or len(mc_stats) <= 0:
                 Log.critical('memcache(%s) connect failed', cls.__mc_conn_str)
                 sys.exit()
         except Exception, e:
             print e
             Log.critical('memcache connection error(%s)(%s)', cls.__mc_conn_str, str(e))
             sys.exit()
         Log.critical('Memcatched(%s:%s) Ok.', host, port)
Пример #37
0
 def start(cls, host, port):
     if cls.__mc_conn is None:
         cls.__mc_conn_str = "%s:%s" % (host, port)
         try:
             cls.__mc_conn = memcache.Client([cls.__mc_conn_str])
             mc_stats = cls.__mc_conn.get_stats()
             if mc_stats is None or len(mc_stats) <= 0:
                 Log.critical('memcache(%s) connect failed',
                              cls.__mc_conn_str)
                 sys.exit()
         except Exception, e:
             print e
             Log.critical('memcache connection error(%s)(%s)',
                          cls.__mc_conn_str, str(e))
             sys.exit()
         Log.critical('Memcatched(%s:%s) Ok.', host, port)
Пример #38
0
 def delete(cls, key):
     try:
         cls._db.delete(key)
     except Exception, e:
         Log.warning('Redis delete %s failed(%s)', key, str(e))
         return False
Пример #39
0
                sys.exit()
            Log.critical('Memcatched(%s:%s) Ok.', host, port)

    @classmethod
    def __reconnect(cls):
        if cls.__mc_conn is not None:
            try:
                cls.__mc_conn.disconnect_all()
            except Exception, e:
                Log.warning('memcache disconnect(%s)', str(e))
            cls.__mc_conn = None
        try:
            cls.__mc_conn = memcache.Client([cls.__mc_conn_str])
        except Exception, e:
            cls.__mc_conn = None
            Log.critical('memcache re_connection error(%s)(%s)', cls.__mc_conn_str, str(e))
 
    @classmethod
    def get(cls, key):
        val = None
        try:
            val = cls.__mc_conn.get(key)
        except Exception, e:
            Log.warning('memcache get %s failed(%s)', key, str(e))
            cls.__reconnect()
            try:
                val = cls.__mc_conn.get(key)
            except Exception, e1: 
                val = None
                Log.warning('memcache re-get %s failed(%s)', key, str(e1))
        return val
Пример #40
0
                sys.exit()
            Log.critical('Memcatched(%s:%s) Ok.', host, port)

    @classmethod
    def __reconnect(cls):
        if cls.__mc_conn is not None:
            try:
                cls.__mc_conn.disconnect_all()
            except Exception, e:
                Log.warning('memcache disconnect(%s)', str(e))
            cls.__mc_conn = None
        try:
            cls.__mc_conn = memcache.Client([cls.__mc_conn_str])
        except Exception, e:
            cls.__mc_conn = None
            Log.critical('memcache re_connection error(%s)(%s)',
                         cls.__mc_conn_str, str(e))

    @classmethod
    def get(cls, key):
        val = None
        try:
            val = cls.__mc_conn.get(key)
        except Exception, e:
            Log.warning('memcache get %s failed(%s)', key, str(e))
            cls.__reconnect()
            try:
                val = cls.__mc_conn.get(key)
            except Exception, e1:
                val = None
                Log.warning('memcache re-get %s failed(%s)', key, str(e1))
        return val
Пример #41
0
    ('.*', 'api.image.PageNotFoundH'),
    
]

application = Application(
    urls,
    cookie_secret = "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
    login_url = "/login",
    template_path = config_base.setting['template'],
    static_path = config_base.setting['static'],
    debug = True,
    xsrf_cookies = True,
    autoescape = None,
    ui_modules = dict(),
)

if __name__ == "__main__":
    reload(sys)
    sys.setdefaultencoding("utf-8")
    Log.start(config_api.log['path'], config_api.log['level'])
    BaseModel.start()
    http_server = HTTPServer(application, xheaders = True)
    print "Start listening: %s" % str(config_api.listen_port)
    if len(sys.argv) >= 2:
        http_server.listen(int(sys.argv[1]))
    else:
        http_server.listen(config_api.listen_port)
    tornado.autoreload.start(IOLoop.instance(), 500)
    IOLoop.instance().start()

Пример #42
0
    (r"/image/upload/common", 'api.image.UploadCommonH'),
    (r"/image/upload/avator", 'api.image.UploadAvatorH'),
    ('.*', 'api.image.PageNotFoundH'),
]

application = Application(
    urls,
    cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
    login_url="/login",
    template_path=config_base.setting['template'],
    static_path=config_base.setting['static'],
    debug=True,
    xsrf_cookies=True,
    autoescape=None,
    ui_modules=dict(),
)

if __name__ == "__main__":
    reload(sys)
    sys.setdefaultencoding("utf-8")
    Log.start(config_api.log['path'], config_api.log['level'])
    BaseModel.start()
    http_server = HTTPServer(application, xheaders=True)
    print "Start listening: %s" % str(config_api.listen_port)
    if len(sys.argv) >= 2:
        http_server.listen(int(sys.argv[1]))
    else:
        http_server.listen(config_api.listen_port)
    tornado.autoreload.start(IOLoop.instance(), 500)
    IOLoop.instance().start()
Пример #43
0
 def delete(cls, key):
     try:
         cls._db.delete(key)
     except Exception, e:
         Log.warning('Redis delete %s failed(%s)', key, str(e))
         return False