class Queue(): #配置对象 __config = "" #队列字典 __sqs = {} #初始化 def __init__(self): self.__config = Config() #初始化队列 queue_type_list = str(self.__config.get("queue.queue_type_list")) for queue_type in queue_type_list.split(","): self.__sqs[queue_type] = deque([]) #进队列 def handler_8001(self, p): i = p.get('i', 0) queue_type = p.get('queue_type', "") if queue_type not in self.__sqs: return [4001, {"msg": "queue type error"}] try: del p['queue_type'] self.__sqs[queue_type].append(p) except: Log.error() return [8001, {"msg": "success" + str(i)}] #出队列 def handler_8002(self, p): queue_type = p.get('queue_type', "") if queue_type not in self.__sqs: return [4001, {"msg": "queue type error"}] try: temp = self.__sqs[queue_type].popleft() if temp == -1: return [4002, {"msg": "queue is empty"}] except: Log.error() return [8002, temp] #队列长度 def handler_8003(self, p): queue_type = p.get('queue_type', "") if queue_type not in self.__sqs: return [4001, {"msg": "queue type error"}] try: temp = len(self.__sqs[queue_type]) except: Log.error() return [8003, temp]
class Cache(): # 是否连接 connected = False # 操作句柄 handler = None # 缓存连接参数 options = {} # 配置对象 config = None # 缓存对象 cache = None # __instance __instance = None def __init__(self): self.config = Config() def connect(self, type=""): if '' == type: type = self.config.get("cache.data_cache_type") module_type = type.title() module = "caches." + module_type m = getattr(__import__(module), module_type) # print(m) className = getattr(m, module_type) # print(className) try: cache = className(self.options) except: Log.error(L('_CACHE_TYPE_INVALID_') + ':' + type) return cache def setOptions(self, name="", value=""): self.options[name] = value def getOptions(self, name=""): return self.options[name] # 取得缓存类实例 @classmethod def getinstance(cls, type=""): if (cls.__instance == None): cls.__instance = Cache() return cls.__instance
class Db(): # 配置对象 config = "" # 数据库类型 dbType = "" # 数据库链接ID,支持多个连接 linkID = {} # 当前链接ID clinkID = '' # 是否已经连接数据库 connected = False # 数据库连接参数配置 dbConfig = {} # 数据配置缓存 __config_dbs = {} # 错误信息 error = '' # 是否显示调试信息,如果启用会在日志文件里记录SQL语句 debug = False # 当前SQL指令 queryStr = "" # 日期格式 __format = "%Y-%m-%d %H:%M:%S" # 数据库类实例 __instance = None # 返回或影响记录数 numRows = 0 # 当前查询ID queryID = None # 最后插入ID lastInsID = 0 # 事务指令数 transTimes = 0 def __init__(self): self.config = Config() # 取得数据库类实例 @classmethod def getinstance(cls, db_config=''): if (cls.__instance == None): db = Db() cls.__instance = db.factory(db_config) return cls.__instance # 加载数据库 支持配置文件或DSN def factory(self, db_config=''): # 读取数据库配置 db_config = self.__parseConfig(db_config) # print(db_config) if db_config['dbms'] == '': Log.error(L('_NO_DB_CONFIG_')) # 数据库类型 self.dbType = db_config['dbms'].capitalize() # 首字母大写 # 加载数据库驱动 module_name = "dbs." + self.dbType # print(dir(__import__(module_name))) # print(sys.modules) m = getattr(__import__(module_name), self.dbType) # print(m) if module_name not in sys.modules: Log.error(L('_NO_DB_MODULE_')) # 实例化数据库驱动类 if hasattr(m, self.dbType): dbClass = getattr(m, self.dbType) db = dbClass(db_config) if "pdo" != db_config['dbms'].lower(): db.dbType = self.dbType.upper() if self.config.get('debug'): db.debug = True else: Log.error(L('_NO_SUPPORT_DB_')) return db # 分析数据库配置信息 def __parseConfig(self, db_config=''): if db_config != '' and isinstance(db_config, str): db_config = self.__parseDSN(db_config) else: db_config = { "dbms": self.config.get('db.type'), "username": self.config.get('db.username'), "password": self.config.get('db.password'), "hostname": self.config.get('db.hostname'), "hostport": self.config.get('db.hostport'), "database": self.config.get('db.database') } return db_config # DSN解析(Data Source Name) # 事例:mysql://username:password@localhost:3306/dbname def __parseDSN(self, dsn_str=''): if (dsn_str == ''): return False p = re.compile("^(.*?)\:\/\/(.*?)\:(.*?)\@(.*?)\:([0-9]{1,6})\/(.*?)$") info = p.findall(dsn_str) dsn = { "dbms": info[0][0], "username": info[0][1], "password": info[0][2], "hostname": info[0][3], "hostport": info[0][4], "database": info[0][5] } return dsn # 初始化数据库链接 def initConnect(self, master=True): if 1 == self.config.get('db.deploy_type'): # 部署类型,多库或单库 # 采用分布式主从服务器 self.clinkID = self.multiConnect(master) else: # 默认单数据库 if self.connected == False: self.clinkID = self.connect() # 连接分布式服务器 def multiConnect(self, master=False): if len(self.__config_dbs) == 0: if len(self.dbConfig) == 0: self.dbConfig = self.config.getSection('db') for (k, v) in self.dbConfig.items(): self.__config_dbs[k] = v.split(',') # 数据库读写是否分离 # 是:读写区分服务器,否:读写不区分服务器 if self.config.get('db.rw_separate'): # “写” 操作时master=true,默认往第一个数据库里写数据 if master: r = 0 else: # “读” 操作时master=false,随机抽取一个服务器做读取操作 r = math.trunc( random.randint(1, len(self.__config_dbs['hostname']) - 1)) else: # 每次随机链接数据库,从第一个数据库开始 r = math.trunc( random.randint(0, len(self.__config_dbs['hostname']) - 1)) username = self.__config_dbs['username'] password = self.__config_dbs['password'] hostname = self.__config_dbs['hostname'] hostport = self.__config_dbs['hostport'] database = self.__config_dbs['database'] # print("r = %s" % r) db_config = { "username": username[r] if r <= len(username) - 1 else username[0], "password": password[r] if r <= len(password) - 1 else password[0], "hostname": hostname[r] if r <= len(hostname) - 1 else hostname[0], "hostport": hostport[r] if r <= len(hostport) - 1 else hostport[0], "database": database[r] if r <= len(database) - 1 else database[0] } return self.connect(db_config, r) # 数据库调试,记录当前SQL def Debug(self): if self.debug: G('queryEndTime') Log.record(self.queryStr + " [ RunTime:" + str(G('queryStartTime', 'queryEndTime', 6)) + "s ]") # 最近一次查询的SQL语句 def getLastSql(self): return self.queryStr # 最近一次的错误信息 def getError(self): return self.error
# !/usr/bin/python # coding=utf-8 # # @Author: LiXiaoYu # @Time: 2013-10-17 # @Info: 公共方法库 import time from eserver.common.const import * from eserver.library.config import Config _config = Config() _default_lang = _config.get("default_lang") _lang_set = getattr(__import__("lang."+_default_lang), _default_lang) #print(_lang_set._lang) #获取内存信息 def get_mem_info(): meminfo = {} with open('/proc/meminfo') as f: for line in f: meminfo[line.split(':')[0]] = line.split(':')[1].strip() return meminfo #内存使用 def memory_get_usage(): meminfo = get_mem_info() return int(meminfo['Active'].split(" ")[0]) #格式化数字 def number_format(num=0, dec=2): if isinstance(num, float):
# !/usr/bin/python # coding=utf-8 # # @Author: LiXiaoYu # @Time: 2013-11-06 # @Info: queue Server. import eserver.library.epoll as epoll from eserver.library.config import Config from eserver.app.queue.circularQueue import CircularQueue # 获取队列服务配置 _config = Config("queue") # 初始参数 _port = _config.get("queue.port") # 服务端口 _app = CircularQueue() # _allow_ip = ['192.168.1.100'] #白名单IP列表 # 开始服务 s = epoll.createServer(_app) # s.setAllowIp(_allow_ip) s.listen(_port)
class Log(): #日志类型 __type = {1: "SYSTEM", 2: "QUEUE"} #日志级别 __level = { 50: "CRITICAL", 40: "ERROR", 30: "WARNING", 20: "INFO", 10: "DEBUG" } #日志信息 __log = [] #日期格式 __format = "%Y-%m-%d %H:%M:%S" #配置对象 __config = "" #日志目录 __log_path = "" def __init__(self): #获取当前目录 _cur_path = os.path.normpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) _log_path = _cur_path + "/../../app/logs" if not os.path.isdir(_log_path): os.makedirs(_log_path) #创建解析器 self.__config = Config() self.__log_path = _log_path #记录日志,并且过滤未设置的级别 def record(self, msg="", type=LOG_SYSTEM, level=ERROR, record=False): if isinstance(msg, (list, tuple)): s = [str(i) for i in msg] msg = " | ".join(s) type = self.__type[type] level = self.__level[level] log_level = self.__config.get("log_level").split(",") if record or (level in log_level): now = datetime.datetime.now().strftime(self.__format) self.__log.append("%s %s: %s" % (now, type, msg)) #日志保存 #destination 写入目标 def save(self, destination='', level=ERROR): if destination == "": now = datetime.datetime.now().strftime("%Y_%m_%d") destination = self.__log_path + "/" + now + ".log" else: destination = self.__log_path + "/" + destination #检测日志文件大小,超过配置大小则备份日志文件重新生成 is_file = os.path.isfile(destination) #判断文件,是否存在 if is_file == True: file_size = os.path.getsize(destination) #获取文件尺寸,以字节为单位 log_file_size = math.floor(int(self.__config.get("log_file_size"))) if log_file_size <= file_size: cur_time = datetime.datetime.now() os.rename( destination, os.path.dirname(destination) + '/' + str(cur_time) + '-' + os.path.basename(destination)) #写日志 msg = "\r\n".join(self.__log) logging.basicConfig(filename=destination, level=level) logging.log(level, msg) #保存后清空日志缓存 self.__log = [] #写日志 def write(self, msg="", destination="", type=LOG_SYSTEM, level=ERROR): if isinstance(msg, (list, tuple)): s = [str(i) for i in msg] msg = " | ".join(s) if destination == "": now = datetime.datetime.now().strftime("%Y_%m_%d") destination = self.__log_path + "/" + now + ".log" else: destination = self.__log_path + "/" + destination #检测日志文件大小,超过配置大小则备份日志文件重新生成 is_file = os.path.isfile(destination) #判断文件,是否存在 if is_file == True: file_size = os.path.getsize(destination) #获取文件尺寸,以字节为单位 log_file_size = math.floor(int(self.__config.get("log_file_size"))) if log_file_size <= file_size: cur_time = datetime.datetime.now() os.rename( destination, os.path.dirname(destination) + '/' + str(cur_time) + '-' + os.path.basename(destination)) #写日志 now = datetime.datetime.now().strftime(self.__format) type = self.__type[type] msg = "%s %s: %s" % (now, type, msg) logging.basicConfig(filename=destination, level=level) logging.log(level, msg)
class Redis(Cache): config = None def __init__(self, options={}): self.config = Config() if "redis" not in sys.modules: Log.error(L('_NO_MODULE_')+":redis") if len(options) == 0: options = { "host":self.config.get("redis.host") if self.config.get("redis.host") else "127.0.0.1", "port":self.config.get("redis.port") if self.config.get("redis.port") else "6397" } self.options = options self.handler = redis.Redis(**options) #set prefix if 'prefix' in options: self.options['prefix'] = options['prefix'] else: self.options['prefix'] = self.config.get("redis.prefix") ## # 读取缓存 # @access public # @param string $name 缓存变量名 # @return mixed None ## def get(self, name=""): value = self.handler.get(self.options['prefix']+name) value = value.decode('UTF-8') try: jsonData = json.loads(value) except: jsonData = value return jsonData ## # 写入缓存 # @access public # @param string $name 缓存变量名 # @param mixed $value 存储数据 # @param integer $expire 有效时间(秒) # @return boolean ## def set(self, name="", value="", expire=0): name = self.options['prefix']+name if isinstance(value, (dict,tuple,list)): value = json.dumps(value) if isinstance(expire, int) and expire>0: result = self.handler.setex(name, value, expire) else: result = self.handler.set(name, value) return result ## # 删除缓存 # @access public # @param string $name 缓存变量名 # @return boolean ## def rm(self, name=""): self.handler.delete(self.options['prefix']+name) ## # 清除缓存 # @access public # @return boolean ## def clear(self): return self.handler.flushdb()