def process(data): try: data = TmProtocol.deserial(data) logging.info('External Receive: {0}'.format( json.dumps(data.to_dict(), ensure_ascii=False))) except DeserialError, e: logging.warning('Task Deserialize Error: {0}'.format(e))
def send(data): """ 发送消息 :param data: 未序列化的数据 :return: """ if isinstance(data, TaskResult): dump_data = data.serial() # 发送数据 while 1: global socket_client try: logging.info('Client Send Len: {0}'.format(len(dump_data))) socket_client.sendall(dump_data) logging.info(u'Client Send: {0}'.format(json.dumps(data.to_dict(), ensure_ascii=False))) ack = socket_client.recv(8192) logging.info('Client Receive: {0}'.format(ack)) ack = json.loads(ack) if ack.get('ack') == 'Fail': raise Exception('TaskResult Deserialize Error') except socket.error, e: # 因server端socket异常未正常发送 retry_count = 0 while 1: retry_count = retry_count + 1 logging.warning('Internal Disconnect, Retry {0}'.format(retry_count)) ret = reconnect_socket() if ret == 0: break else: time.sleep(5) logging.warning('Client Send Error, Retry {0}'.format(retry_count)) else: break
def init_socket(self): retry_count = 0 while 1: retry_count = retry_count + 1 ret = self.reconnect_socket() if ret == 0: break else: logging.warning( 'External Disconnect, Retry {0}'.format(retry_count)) time.sleep(5)
def reconnect_socket(self): try: self.socket_client.connect((self.master_host, self.master_port)) logging.info('External Connect To Host: {0}, Port: {1}'.format( self.master_host, self.master_port)) except socket.error: logging.warning('External Connect Fail') return -1 else: self.hello() self.health() return 0
def init_conf(task, queue_uuid, task_uuid): try: conf = SSHConfig(**task.task_info["remote"]["params"]) return conf except ConfigInvalid, e: # 配置文件格式错误 直接返回-1 logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.InitFailed.value)) status_code, status_msg = TaskStatus.InitFailed, e.message result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code, status_msg=status_msg, session=task.session) send(result) return -1
def unregister_callback(self, event_name, func): if event_name not in self.callback_dict: logging.warning(u'未找到该事件名称') return -1 else: callback_list = self.callback_dict.get(event_name) if func not in callback_list: logging.warning(u'未注册该回调函数') return -1 else: callback_list.remove(func) if not callback_list: self.callback_dict.pop(event_name)
def handle(self): while True: data = self.request.recv(8192) if data: logging.info('Server Receive Len: {0}'.format(len(data))) ret = self.process(data) if ret != -1: ack = '{"ack": "Pass"}' else: ack = '{"ack": "Fail"}' self.request.sendall(ack) logging.info('Server Send: {0}'.format(ack)) else: logging.warning('Server Disconnect: {0}'.format( self.client_address)) break
def run(self): while True: try: data = self.socket_client.recv(8192) if not data: raise socket.error except socket.error: retry_count = 0 while 1: retry_count = retry_count + 1 logging.warning( 'External Disconnect, Retry {0}'.format(retry_count)) # 删除原先的连接重连 del self.socket_client self.socket_client = self.init_ssl() ret = self.reconnect_socket() if ret == 0: break else: time.sleep(5) else: self.process(data)
def init_status(task, queue_uuid, task_uuid): if not worker_pool.vacant(): # 进程池满 logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.WorkerWaiting.value)) status_code = TaskStatus.WorkerWaiting status_msg = MSG_DICT.get(status_code) result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code, status_msg=status_msg, session=task.session) send(result) return -1 else: ret_code, ret_msg = get_time \ .compare_timestamps(task.trigger_time, task.task_earliest, task.task_latest) if ret_code == 3: # 无法执行 直接返回-1 logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.TimeRangeExcept.value)) status_code = TaskStatus.TimeRangeExcept status_msg = MSG_DICT.get(status_code) result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code, status_msg=status_msg, session=task.session) send(result) return -1 elif ret_code == 2: # 需要等待 logging.info('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.WorkerWaiting.value)) status_code = TaskStatus.TriggerTimeWaiting status_msg = u'需要等待{0}秒'.format(ret_msg) result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code, status_msg=status_msg, session=task.session) send(result) elif ret_code == 1: # 该状态不通知Master # 可以执行 # logging.info('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.Runnable.value)) # status_code = TaskStatus.Runnable # status_msg = MSG_DICT.get(status_code) # result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code, # status_msg=status_msg, session=task.session) # send(result) pass else: # 异常情况 直接返回-1 logging.warning('TaskUUID: {0}, TaskStatus: {1}'.format(task_uuid, TaskStatus.UnKnown.value)) status_code = TaskStatus.UnKnown status_msg = MSG_DICT.get(status_code) result = TaskResult(queue_uuid=queue_uuid, task_uuid=task_uuid, status_code=status_code, status_msg=status_msg, session=task.session) send(result) return -1