def run(self): #Overwrite run() method, put what you want the thread do here self.thread_stop = False while not self.thread_stop: time.sleep(1) if self.thread_stop: break self.lock.acquire() for pos in range(len(self.tasknodeds)): if self.tasknodeds[pos].deadline <= msg.g_now: newtns = self.tasknodeds[pos:] del self.tasknodeds[pos:] for timeout in newtns: msgobj = {'class':timeout.nname} try: for case in switch(timeout.nname): if case(msg.g_class_inner_chk_task_domain) \ or case(msg.g_class_inner_chk_task_record): self.http_tq.put(msgobj, block=False) break if case(msg.g_class_inner_chk_task_db_heartbeat): self.tq.put(msgobj, block=False) self.tq4init.put(msgobj, block=False) break if case(): self.tq.put(msgobj, block=False) except Queue.Full, e: self.loger.debug(_lineno(self), 'taskq is ', repr(e)) self.loger.debug(traceback.format_exc()) finally: timeout.deadline = msg.g_now + timeout.interval self.add_tasknode(timeout)
def __del_item(self, _type, *args): retobj = None if not self.desc.has_key(_type): return retobj arr = self.desc[_type] for case in switch(_type): if case(self.type_err_view): for i in range(len(arr)-1,-1,-1): if arr[i]['opt'] == args[0] \ and arr[i]['view'] == args[1] \ and arr[i]['mask'] == args[2]: retobj = arr[i] del arr[i] break if case(self.type_err_record): for i in range(len(arr)-1,-1,-1): if arr[i]['opt'] == args[0] \ and arr[i]['view'] == args[1] \ and arr[i]['domain'] == args[2] \ and arr[i]['type'] == args[3]: retobj = arr[i] del arr[i] break break if case(self.type_err_db): for i in range(len(arr)-1,-1,-1): if arr[i]['desc'] == args[0]: retobj = arr[i] del arr[i] break break if case(): self.loger.warn(_lineno(self), 'type %s not implemented!' % (_type,)) return retobj
def handler(self, data): if self.dbcon.conn_error: self.dbcon = MySQL.MySQL(self.dbip, mgr_conf.g_db_user, mgr_conf.g_db_passwd, mgr_conf.g_db_db, loger=self.loger) try: self.loger.care(_lineno(self), 'recv request class %s' % (data['class'])) for case in switch(data['class']): if case(msg.g_class_init_view_reply) or case( msg.g_class_init_dns_reply): self.just4testcnt += 1 req_handler.handle_proxy_init_reply( self, data, data['inner_addr'][0]) break if case(msg.g_class_inner_chk_task_db_heartbeat): req_handler.handle_inner_chk_task_db_heartbeat(self) break if case(msg.g_class_proxy_heartbeat): req_handler.handle_proxy_heartbeat(self, data) break if case(): self.loger.warn(_lineno(self), 'recv something else: ', data['class']) except Exception as e: self.loger.error(_lineno(self), 'inner error: ', repr(e)) self.loger.error(traceback.format_exc())
def callme(self, worker, data, ali_tbl, opt): for case in switch(opt): if case('add'): return self.add(worker, data, ali_tbl) if case('set'): return self.set(worker, data, ali_tbl) if case('del'): return self.delete(worker, data, ali_tbl) if case(): self.loger.error(_lineno(), self.record_type, ' opt:', opt, ' not implement') raise Exception(self.record_type + ' opt:' + opt + ' not implement')
def handler(self, data): self.loger.debug(_lineno(self), 'msg class: ', data['class']) for case in switch(data['class']): if case(msg.g_class_inner_chk_task_domain) or case(msg.g_class_inner_chk_task_record): req_handler.handle_inner_chk_task(self, self.worker, data['class']) break if case(msg.g_class_inner_chk_task_done): req_handler.handle_inner_chk_task_done(self, data) break if case(): self.loger.warn(_lineno(self), 'recv something else: ', data['class'])
def run(self ): #Overwrite run() method, put what you want the thread do here ADDR = (self.HOST, self.PORT) self.udpSerSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udpSerSock.setblocking(False) self.udpSerSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.udpSerSock.bind(ADDR) rlists = [self.udpSerSock] while not self.thread_stop: try: rs, ws, es = select.select([self.udpSerSock], [], [], 1) if self.thread_stop: break if not (rs or ws or es): #timeout continue for s in rs: data, addr = s.recvfrom(self.BUFSIZE) self.loger.info(_lineno(self), 'received from ', addr, ' data:', data) if data == None: self.loger.info(_lineno(self), 'recv data none') continue if len(data) == 0: self.loger.info(_lineno(self), 'recv data len 0') continue decodejson = json.loads(data) decodejson['inner_addr'] = addr for case in switch(decodejson['class']): if case(msg.g_class_init_view_reply) or \ case(msg.g_class_init_dns_reply) or \ case(msg.g_class_proxy_heartbeat) : #if msg.g_init_complete == True: # self.loger.info(_lineno(self), 'send to worker') # self.worker.put(decodejson) #else: # self.loger.info(_lineno(self), 'send to 4init') # self.worker4init.put(decodejson) self.worker4init.put(decodejson) break if case(msg.g_class_init): req_handler.g_init_should_stop = 1 self.loger.info( _lineno(self), 'set g_init_should_stop %d' % (req_handler.g_init_should_stop, )) self.worker.put(decodejson) break if case(): self.worker.put(decodejson) except Exception as e: self.loger.error(_lineno(self), 'inner error:', repr(e)) self.loger.error(traceback.format_exc()) self.udpSerSock.close()
def handler(self, data): self.loger.debug(_lineno(self), 'msg class: ', data['class']) for case in switch(data['class']): if case(msg.g_class_inner_chk_task_domain) or case( msg.g_class_inner_chk_task_record): req_handler.handle_inner_chk_task(self, self.worker, data['class']) break if case(msg.g_class_inner_chk_task_done): req_handler.handle_inner_chk_task_done(self, data) break if case(): self.loger.warn(_lineno(self), 'recv something else: ', data['class'])
def handle_proxy_init_reply(worker, answ, addr): str_class = answ['class'].partition('_')[0] state_set = 0 if answ['type'] == msg.g_ptr_tbl: answ['data'] = partition_reserve_ip_from_ptr(answ['data']) for case in switch(answ['result']): if case(0): state_set = 1 break if case(1): #操作失败 state_set = 3 break if case(2): #不存在 state_set = 2 break if case(): state_set = 0 mgr_singleton.g_singleton.get_loger().debug(_lineno(), "tid:", answ['id'], ' ', str_class, ' ', answ['type'], ' ', answ['viewid'], ' ', answ['data'], ' ', state_set, ' ', answ['opt']) if worker.just4testcnt > 0: worker.dbcon.query(msg.g_init_sql_replytask % (state_set, answ['id'])) if False and worker.just4testcnt > 0: for case in switch(str_class): if case('dns'): mgr_singleton.g_singleton.get_err_info().del_record_timeout(answ['opt'], answ['viewid'], answ['data'], answ['type']) break if case('view'): mgr_singleton.g_singleton.get_err_info().del_view_timeout(answ['opt'], answ['viewid'], answ['data']) break if case(): mgr_singleton.g_singleton.get_loger().warn(_lineno(), 'can not del error describe for unknow type ', str_class) if msg.g_init_sendstate == 2 and msg.g_init_maxid-int(answ['id']) < 10: req_handler.set_chk_snd_timer(worker) msg.g_init_sendstate = 0
def run(self): #Overwrite run() method, put what you want the thread do here ADDR = (self.HOST,self.PORT) self.udpSerSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.udpSerSock.setblocking(False) self.udpSerSock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) self.udpSerSock.bind(ADDR) rlists = [self.udpSerSock] while not self.thread_stop: try: rs,ws,es = select.select([self.udpSerSock], [], [], 1) if self.thread_stop: break if not (rs or ws or es): #timeout continue for s in rs: data, addr = s.recvfrom(self.BUFSIZE) self.loger.info(_lineno(self), 'received from ', addr, ' data:', data) if data==None: self.loger.info(_lineno(self), 'recv data none') continue if len(data)==0: self.loger.info(_lineno(self), 'recv data len 0') continue decodejson = json.loads(data) decodejson['inner_addr'] = addr for case in switch(decodejson['class']): if case(msg.g_class_init_view_reply) or \ case(msg.g_class_init_dns_reply) or \ case(msg.g_class_proxy_heartbeat) : #if msg.g_init_complete == True: # self.loger.info(_lineno(self), 'send to worker') # self.worker.put(decodejson) #else: # self.loger.info(_lineno(self), 'send to 4init') # self.worker4init.put(decodejson) self.worker4init.put(decodejson) break if case(msg.g_class_init): req_handler.g_init_should_stop = 1 self.loger.info(_lineno(self), 'set g_init_should_stop %d' % (req_handler.g_init_should_stop,)) self.worker.put(decodejson) break if case(): self.worker.put(decodejson) except Exception as e: self.loger.error(_lineno(self), 'inner error:', repr(e)) self.loger.error(traceback.format_exc()) self.udpSerSock.close()
def __to_array(self): objs = [[], []] objs_err = objs[0] objs_regain = objs[1] for k in self.desc: v = self.desc.get(k) if type(v) is types.ListType: for j in v: if 1 == j['sent']: continue j['sent'] = 1 item = {'etype': k, 'desc': {}} set_where = -1 # -1 or 1 or 0 not set for case in switch(k): if case(self.type_err_db): item['desc']['regain'] = j['regain'] item['desc']['settime'] = j['settime'] item['desc']['detail'] = self.db_desc[j['desc']] item['desc']['desc'] = j['desc'] break if case(self.type_err_view): item['desc']['regain'] = j['regain'] item['desc']['settime'] = j['settime'] item['desc']['opt'] = j['opt'] item['desc']['view'] = j['view'] item['desc']['mask'] = j['mask'] break if case(self.type_err_record): item['desc']['regain'] = j['regain'] item['desc']['settime'] = j['settime'] item['desc']['opt'] = j['opt'] item['desc']['view'] = j['view'] item['desc']['domain'] = j['domain'] item['desc']['type'] = j['type'] break if case(): set_where = 0 self.loger.warn(_lineno(self), 'type %s not implemented!' % (k, )) if 0 != set_where: if 1 == j['regain']: objs_regain.append(item) else: objs_err.append(item) return objs
def __to_array(self): objs = [[],[]] objs_err = objs[0] objs_regain = objs[1] for k in self.desc: v = self.desc.get(k) if type(v) is types.ListType: for j in v: if 1 == j['sent']: continue j['sent'] = 1 item = {'etype':k, 'desc':{}} set_where = -1 # -1 or 1 or 0 not set for case in switch(k): if case(self.type_err_db): item['desc']['regain'] = j['regain'] item['desc']['settime'] = j['settime'] item['desc']['detail'] = self.db_desc[j['desc']] item['desc']['desc'] = j['desc'] break if case(self.type_err_view): item['desc']['regain'] = j['regain'] item['desc']['settime'] = j['settime'] item['desc']['opt'] = j['opt'] item['desc']['view'] = j['view'] item['desc']['mask'] = j['mask'] break if case(self.type_err_record): item['desc']['regain'] = j['regain'] item['desc']['settime'] = j['settime'] item['desc']['opt'] = j['opt'] item['desc']['view'] = j['view'] item['desc']['domain'] = j['domain'] item['desc']['type'] = j['type'] break if case(): set_where = 0 self.loger.warn(_lineno(self), 'type %s not implemented!' % (k,)) if 0 != set_where: if 1 == j['regain']: objs_regain.append(item) else: objs_err.append(item) return objs
def handler(self, data): if self.dbcon.conn_error: self.dbcon = MySQL.MySQL(self.dbip, mgr_conf.g_db_user, mgr_conf.g_db_passwd, mgr_conf.g_db_db, loger=self.loger) try: self.loger.care(_lineno(self), 'recv request class %s' % (data['class'])) for case in switch(data['class']): if case(msg.g_class_init_view_reply) or case(msg.g_class_init_dns_reply): self.just4testcnt += 1 req_handler.handle_proxy_init_reply(self, data, data['inner_addr'][0]) break if case(msg.g_class_inner_chk_task_db_heartbeat): req_handler.handle_inner_chk_task_db_heartbeat(self) break if case(msg.g_class_proxy_heartbeat): req_handler.handle_proxy_heartbeat(self, data) break if case(): self.loger.warn(_lineno(self), 'recv something else: ', data['class']) except Exception as e: self.loger.error(_lineno(self), 'inner error: ', repr(e)) self.loger.error(traceback.format_exc())
def __del_item(self, _type, *args): retobj = None if not self.desc.has_key(_type): return retobj arr = self.desc[_type] for case in switch(_type): if case(self.type_err_view): for i in range(len(arr) - 1, -1, -1): if arr[i]['opt'] == args[0] \ and arr[i]['view'] == args[1] \ and arr[i]['mask'] == args[2]: retobj = arr[i] del arr[i] break if case(self.type_err_record): for i in range(len(arr) - 1, -1, -1): if arr[i]['opt'] == args[0] \ and arr[i]['view'] == args[1] \ and arr[i]['domain'] == args[2] \ and arr[i]['type'] == args[3]: retobj = arr[i] del arr[i] break break if case(self.type_err_db): for i in range(len(arr) - 1, -1, -1): if arr[i]['desc'] == args[0]: retobj = arr[i] del arr[i] break break if case(): self.loger.warn(_lineno(self), 'type %s not implemented!' % (_type, )) return retobj
def handler(self, data): if self.dbcon.conn_error: self.dbcon = MySQL.MySQL(self.dbip, mgr_conf.g_db_user, mgr_conf.g_db_passwd, mgr_conf.g_db_db, loger=self.loger) try: self.loger.info(_lineno(self), 'recv request class %s' % (data['class'])) for case in switch(data['class']): if case(msg.g_class_init): self.proxy_addr[data['inner_addr'][0]] = [data['inner_addr'], self.proxy_health] msg.g_init_resp_expect = -1 #msg.g_init_complete = False req_handler.g_init_should_stop = 1 self.loger.info(_lineno(self), 'set g_init_should_stop %d' % (req_handler.g_init_should_stop,)) req_handler.handle_proxy_init_new(self, data['inner_addr'][0]) break if case(msg.g_class_init_test): self.check_thd.del_tasknode_byname_lock(msg.g_class_init_test) self.proxy_addr['121.201.12.66'] = [('121.201.12.66', 12353), self.proxy_health] msg.g_init_resp_expect = -1 #msg.g_init_complete = False req_handler.g_init_should_stop = 1 req_handler.handle_proxy_init_new(self, '121.201.12.66') break if case(msg.g_class_proxy_register): self.loger.info(_lineno(self), data['class'], '... expect[', msg.g_init_resp_expect, ']') self.proxy_addr[data['inner_addr'][0]] = [data['inner_addr'], self.proxy_health] if msg.g_init_resp_expect == -1: self.check_thd.del_tasknode_byname_lock(msg.g_class_inner_chk_init_ok) self.check_thd.add_tasknode_byinterval_lock(msg.g_class_inner_chk_snd, mgr_conf.g_inner_chk_snd_time) self.check_thd.add_tasknode_byinterval_lock(msg.g_class_inner_chk_task_domain, mgr_conf.g_inner_chk_task_domain_time) self.check_thd.add_tasknode_byinterval_lock(msg.g_class_inner_chk_task_record, mgr_conf.g_inner_chk_task_record_time) msg.g_init_resp_expect = 0 #msg.g_init_complete = True self.loger.info(_lineno(self), 'on register add timers OK') break if case(msg.g_class_inner_chk_init_ok): ok_cnt = req_handler.handle_inner_chk_init_ok(self) if ok_cnt: self.loger.info(_lineno(self), data['class'], '... expect[', msg.g_init_resp_expect, '][', ok_cnt,']') else: self.loger.info(_lineno(self), data['class'], '... expect[', msg.g_init_resp_expect, '][NULL]') if ok_cnt != None and ok_cnt == msg.g_init_resp_expect: self.check_thd.del_tasknode_byname_lock(msg.g_class_inner_chk_init_ok) self.check_thd.add_tasknode_byinterval_lock(msg.g_class_inner_chk_snd, mgr_conf.g_inner_chk_snd_time) self.check_thd.add_tasknode_byinterval_lock(msg.g_class_inner_chk_task_domain, mgr_conf.g_inner_chk_task_domain_time) self.check_thd.add_tasknode_byinterval_lock(msg.g_class_inner_chk_task_record, mgr_conf.g_inner_chk_task_record_time) #msg.g_init_complete = True self.loger.info(_lineno(self), 'on init add timers OK') break if case(msg.g_class_inner_chk_snd) or case(msg.g_class_inner_chk_init): self.loger.info(_lineno(self), data['class'], '...') del_items = [] for k, v in self.proxy_addr.iteritems(): v[1] = v[1] - 1 self.loger.info(_lineno(self), 'proxy %s->%s[%d]' % (k, v, v[1])) if v[1] <= 0: del_items.append(k) for p in del_items: self.proxy_addr.pop(p) if len(self.proxy_addr.keys()) > 0: req_handler.handle_inner_chk_snd(self) else: self.loger.warn(_lineno(self), 'proxy ip is empty') break if case(msg.g_class_inner_chk_task_domain_reply) or case(msg.g_class_inner_chk_task_record_reply): req_handler.handle_inner_chk_task_reply(self, data) break if case(msg.g_class_inner_chk_task_db_heartbeat): req_handler.handle_inner_chk_task_db_heartbeat(self) break if case(): self.loger.warn(_lineno(self), 'recv something else: ', data['class']) except Exception as e: self.loger.error(_lineno(self), 'inner error: ', repr(e)) self.loger.error(traceback.format_exc())
def handler(self, data): if self.dbcon.conn_error: self.dbcon = MySQL.MySQL(self.dbip, mgr_conf.g_db_user, mgr_conf.g_db_passwd, mgr_conf.g_db_db, loger=self.loger) try: self.loger.info(_lineno(self), 'recv request class %s' % (data['class'])) for case in switch(data['class']): if case(msg.g_class_init): self.proxy_addr[data['inner_addr'][0]] = [ data['inner_addr'], self.proxy_health ] msg.g_init_resp_expect = -1 #msg.g_init_complete = False req_handler.g_init_should_stop = 1 self.loger.info( _lineno(self), 'set g_init_should_stop %d' % (req_handler.g_init_should_stop, )) req_handler.handle_proxy_init_new(self, data['inner_addr'][0]) break if case(msg.g_class_init_test): self.check_thd.del_tasknode_byname_lock( msg.g_class_init_test) self.proxy_addr['121.201.12.66'] = [ ('121.201.12.66', 12353), self.proxy_health ] msg.g_init_resp_expect = -1 #msg.g_init_complete = False req_handler.g_init_should_stop = 1 req_handler.handle_proxy_init_new(self, '121.201.12.66') break if case(msg.g_class_proxy_register): self.loger.info(_lineno(self), data['class'], '... expect[', msg.g_init_resp_expect, ']') self.proxy_addr[data['inner_addr'][0]] = [ data['inner_addr'], self.proxy_health ] if msg.g_init_resp_expect == -1: self.check_thd.del_tasknode_byname_lock( msg.g_class_inner_chk_init_ok) self.check_thd.add_tasknode_byinterval_lock( msg.g_class_inner_chk_snd, mgr_conf.g_inner_chk_snd_time) self.check_thd.add_tasknode_byinterval_lock( msg.g_class_inner_chk_task_domain, mgr_conf.g_inner_chk_task_domain_time) self.check_thd.add_tasknode_byinterval_lock( msg.g_class_inner_chk_task_record, mgr_conf.g_inner_chk_task_record_time) msg.g_init_resp_expect = 0 #msg.g_init_complete = True self.loger.info(_lineno(self), 'on register add timers OK') break if case(msg.g_class_inner_chk_init_ok): ok_cnt = req_handler.handle_inner_chk_init_ok(self) if ok_cnt: self.loger.info(_lineno(self), data['class'], '... expect[', msg.g_init_resp_expect, '][', ok_cnt, ']') else: self.loger.info(_lineno(self), data['class'], '... expect[', msg.g_init_resp_expect, '][NULL]') if ok_cnt != None and ok_cnt == msg.g_init_resp_expect: self.check_thd.del_tasknode_byname_lock( msg.g_class_inner_chk_init_ok) self.check_thd.add_tasknode_byinterval_lock( msg.g_class_inner_chk_snd, mgr_conf.g_inner_chk_snd_time) self.check_thd.add_tasknode_byinterval_lock( msg.g_class_inner_chk_task_domain, mgr_conf.g_inner_chk_task_domain_time) self.check_thd.add_tasknode_byinterval_lock( msg.g_class_inner_chk_task_record, mgr_conf.g_inner_chk_task_record_time) #msg.g_init_complete = True self.loger.info(_lineno(self), 'on init add timers OK') break if case(msg.g_class_inner_chk_snd) or case( msg.g_class_inner_chk_init): self.loger.info(_lineno(self), data['class'], '...') del_items = [] for k, v in self.proxy_addr.iteritems(): v[1] = v[1] - 1 self.loger.info(_lineno(self), 'proxy %s->%s[%d]' % (k, v, v[1])) if v[1] <= 0: del_items.append(k) for p in del_items: self.proxy_addr.pop(p) if len(self.proxy_addr.keys()) > 0: req_handler.handle_inner_chk_snd(self) else: self.loger.warn(_lineno(self), 'proxy ip is empty') break if case(msg.g_class_inner_chk_task_domain_reply) or case( msg.g_class_inner_chk_task_record_reply): req_handler.handle_inner_chk_task_reply(self, data) break if case(msg.g_class_inner_chk_task_db_heartbeat): req_handler.handle_inner_chk_task_db_heartbeat(self) break if case(): self.loger.warn(_lineno(self), 'recv something else: ', data['class']) except Exception as e: self.loger.error(_lineno(self), 'inner error: ', repr(e)) self.loger.error(traceback.format_exc())
def handle_inner_chk_task_reply(worker, data): dic_result = data['result'] if len(dic_result) < 1: mgr_singleton.g_singleton.get_loger().info(_lineno(), data['class'], ' no task') return replymsg = {'class': msg.g_class_inner_chk_task_done, 'opt' : 'set', 'data' : { 'sid':mgr_conf.g_mgr_sid, 'tasks':{} } } ptr_tasks = replymsg['data']['tasks'] msgobj = [] bat_task = [] for task_id in dic_result: task_data = dic_result.get(task_id) mgr_singleton.g_singleton.get_loger().info(_lineno(), task_id, '\t:', task_data) try: dedata = json.loads(task_data['data']) except Exception,e: mgr_singleton.g_singleton.get_loger().error(_lineno(), 'load json data error!', repr(e)) mgr_singleton.g_singleton.get_loger().error(traceback.format_exc()) continue task_type = task_data['type'] replymsg['type'] = task_type found = False ali = None for case in switch(task_type): if case('record'): for ali in http_tbl_alise: if dedata.has_key(ali): found = True break break if case(): ali = '__any__' found = True break if not found: ptr_tasks[task_id] = {'ret':1, 'result':'task id '+task_id + ' failed', 'error':'table error'} continue #1、修改数据库 try: db_ret, go_on, old_data = worker.m_handlers[task_type][ali].callme(worker, dedata, ali, task_data['opt']) except Exception as e: mgr_singleton.g_singleton.get_loger().error(_lineno(), '!!!!--->', repr(e)) mgr_singleton.g_singleton.get_loger().error(traceback.format_exc()) #ptr_tasks[task_id] = {'ret':1, 'result':'task id '+task_id + ' failed', 'error':repr(e)} ptr_tasks[task_id] = {'ret':1, 'result':'task id '+task_id + ' failed', 'error':'set db failed!'} continue if not db_ret: ptr_tasks[task_id] = {'ret':1, 'result':'task id '+task_id + ' failed', 'error':'db error'} continue ptr_tasks[task_id] = {'ret':0, 'result':'task id '+task_id + ' succ', 'error':''} #2、如果有需要,通知proxy if go_on: try: is_bat_task = worker.m_handlers[task_type][ali].notify(worker, msgobj, opt=task_data['opt'], data=dedata, odata=old_data) if is_bat_task: dedata['handle_class'] = worker.m_handlers[task_type][ali] bat_task.append(dedata) except Exception as e: mgr_singleton.g_singleton.get_loger().error(_lineno(), 'notify proxy error!!!!--->', repr(e)) mgr_singleton.g_singleton.get_loger().error(traceback.format_exc()) continue