def queryTask1(config, device_id, project_code, test_type, branch): try: # check dedicated queue firstly credentials = pika.PlainCredentials(g_username, g_password) params = pika.ConnectionParameters(config["rabbitMQ_address"], config["rabbitMQ_port"], '/', credentials) connection = pika.BlockingConnection(params) channel = connection.channel() queue = channel.queue_declare(queue=device_id, durable=True) if queue.method.message_count > 0: method_frame, header_frame, body = channel.basic_get(device_id) if method_frame: channel.basic_ack(method_frame.delivery_tag) return json.loads(body) else: LOGGER.warning('No message returned') for t in test_type: for b in branch: q_name = '{}_{}_{}'.format(project_code, b, t) queue = channel.queue_declare(queue=q_name, durable=True) if queue.method.message_count > 0: method_frame, header_frame, body = channel.basic_get( q_name) if method_frame: channel.basic_ack(method_frame.delivery_tag) return json.loads(body) else: LOGGER.warning('No message returned') except: print traceback.format_exc() LOGGER.error(traceback.format_exc()) return None
def cancelTask_pika(config, queue_id, task_id): task_to_nack = [] try: credentials = pika.PlainCredentials(g_username, g_password) params = pika.ConnectionParameters(config["rabbitMQ_address"], config["rabbitMQ_port"], '/', credentials) connection = pika.BlockingConnection(params) channel = connection.channel() queue = channel.queue_declare(queue=queue_id, durable=True) rc = -1 _target_delivery_tag = '' for m in range(queue.method.message_count): method_frame, header_frame, body = channel.basic_get(queue_id) if method_frame: task = json.loads(body) if task.get('task_id', 'not_a_valid_id') == task_id: # channel.basic_ack(method_frame.delivery_tag) _target_delivery_tag = method_frame.delivery_tag rc = 0 LOGGER.warning("cancellation op will ack: {}".format(task)) break else: task_to_nack.append(method_frame.delivery_tag) LOGGER.warning( "cancellation op will not ack: {}".format(task)) #channel.basic_nack(method_frame.delivery_tag) else: rc = -1 # task_to_nack = (_ for _ in task_to_nack) for t in task_to_nack: channel.basic_nack(t) channel.basic_ack(_target_delivery_tag) except: for t in task_to_nack: channel.basic_nack(t) LOGGER.error(traceback.format_exc()) rc = -2 return rc
def send_to_server(data, dut_name, platform, run_id, build): for case in data: jt = json_template.copy() jt["hw_serial"] = dut_name jt["hw_type"] = platform jt["run_id"] = run_id jt["sw_version"] = build try: jt["timestamp"] = float(case.start) jt["duration"] = float(case.end) - float(case.start) except: LOGGER.warning(traceback.format_exc()) jt["application"]["name"] = case.name jt["action_verdicts"]["final"] = case.pretty_result try: err_log = '' for run in case.runs(): err_log += '||'.join(['|'.join(_) for _ in run.error_log]) jt["notes"] = [err_log] except:
def init(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ip = self.__get_ip_address() try: if not self.__check_port_occupied( self.server.server_config["socket_server_port"]): self.sock.bind( (ip, self.server.server_config["socket_server_port"])) self.sock.listen( self.server.server_config["max_connection"]) LOGGER.info( "Connect to server {}:{} with max connection {}". format(ip, self.server.server_config["socket_server_port"], self.server.server_config["max_connection"])) self.launched = True return True else: LOGGER.warning("{} has been occupied!".format( self.server.server_config["socket_server_port"])) return False except: LOGGER.error(traceback.format_exc()) return False