def _send_message(self, message, msg_type='pass'):
     """
     投递消息到 模式为:mustang.# 的队列 => mustang.[msg_type]
     其中 模式: mustang.# 与 Java 约定,不可修改
     :param str message:  消息内容 str
     :param str msg_type: pass | fail 分别对应发送到成功和失败队列, 默认 pass
     :raises ValueError:
     """
     validators.require_string(message, 'message')
     self.channel.basic_publish(exchange=self.exchange,
                                routing_key=self.SEND_ROUTING_KEY_PREFIX +
                                msg_type,
                                body=bytes(message, 'utf8'))
     logger.info("send %s message successful!", msg_type)
 def update_status(self, record_id, status='ON_CLASSIFY'):
     """
     更新执行状态
     :param str record_id: ''
     :param str status:
             ON_CLASSIFY 分类中 (默认值)
             ON_EXTRACT 提取中
             ON_SUB_KEY_RECOGNISE 子项识别中
             ON_ANALYSIS 解析中(规则比对)
             DONE 处理完成
     """
     if record_id is None or record_id == '':
         raise ValueError('record_id cannot be blank')
     message = {'record_id': record_id, 'status': status}
     self.channel.basic_publish(exchange=self.exchange,
                                routing_key='notice.status',
                                body=bytes(str(message), 'utf8'))
     logger.info("update %s status to %s successful!", record_id, status)
Beispiel #3
0
def get_file(path_from_request, verbose, server_working_directory):
    absolute_path, server_working_directory = get_absolute_path(
        server_working_directory, path_from_request)
    # Check if user has access to the file
    user_has_access(absolute_path, server_working_directory)

    if path_from_request == "/" or os.path.isdir(absolute_path):
        #return tree
        logger.info("Returning list of files in main directory: %s",
                    absolute_path)
        return json.dumps(path_to_dir(absolute_path), sort_keys=True, indent=4)
    else:
        # Check if file exists
        file_exists(absolute_path)
        #return contents of file
        with open(absolute_path, 'r') as content_file:
            logger.info("Returning content of file at %s", absolute_path)
            return content_file.read()
Beispiel #4
0
def handle_client(conn, addr, verbose, server_working_directory):
    logger.info('New connection: %s', str(addr))
    try:
        data = conn.recv(1024)
        print("Receiving request: \n" +data.decode("utf-8"))
        response = "".encode("utf-8")
        try:
            response = receive_request(data.decode("utf-8"), verbose, str(server_working_directory))
            response = response.encode("utf-8")
        except HTTPException as e:
            # catch any HTTPExceptions and instead transform it into the appropriate response code
            http_code = e.args[0]
            http_code_name = http_name_from_code(http_code)
            response = "HTTP/1.1 " +str(http_code) +" " +http_code_name +"\r\n\r\nError " +str(http_code) +" (" +http_code_name +") \n"
            response = response.encode("utf-8")
        finally:
            print("\nSending response: \n" +response.decode("utf-8"))
            conn.sendall(response)
    finally:
        logger.info("Connection with client %s closed.", str(addr))
        conn.close()
 def callback(ch, method, properties, body):
     delivery_tag = method.delivery_tag
     routing_key = method.routing_key
     msg_body = body.decode('utf-8')
     logger.info("Received message from %s, body: %s ", routing_key,
                 msg_body)
     record = json.loads(msg_body)
     if 'record_id' not in record:
         raise AnalysisException(message='illegal msg, miss:record_id')
     record_id = record['record_id']
     # 执行回调
     try:
         do_task(record)
         # msg
         success_message = str({
             'time_cost': 0,
             'document_classify': [],
             'document_kv_info': [],
             'rule_output_data': [],
             'record_id': record_id
         })
         self._send_message(success_message)
         self.channel.basic_ack(delivery_tag=delivery_tag)
     except AnalysisException as e:
         logger.warn('analysis exception: error: %s', e)
         fail_msg = str({
             'sid': e.sid,
             'message': e.message,
             'record_id': record_id
         })
         self._send_message(fail_msg, msg_type='fail')
         self.channel.basic_ack(delivery_tag=delivery_tag)
     except BaseException as e:
         logger.error('something bad happened..., error: %s', e.args)
         self.channel.basic_ack(delivery_tag=delivery_tag)
     finally:
         logger.info('Done !')
 def __init__(self,
              host,
              port=5672,
              username=None,
              password=None,
              queue_config=None):
     """
     初始化消息服务,建立连接,创建队列
     :param str host: IP
     :param int port: 端口
     :param str username: 用户名
     :param str password: 密码
     :param QueueConfig queue_config: 队列配置
     """
     if queue_config is None:
         raise ValueError('queue_config must supply!')
     #  establish a connection with RabbitMQ server
     credentials = ''
     if username is not None and password is not None:
         credentials = PlainCredentials(username=username,
                                        password=password)
     logger.info('connecting to rabbitmq server...')
     connection = pika.BlockingConnection(
         pika.ConnectionParameters(host=host,
                                   port=port,
                                   credentials=credentials))
     self.channel = connection.channel()
     self.exchange = queue_config.exchange
     self.default_listen_queue = queue_config.default_listen_queue
     self.binding_keys = queue_config.binding_keys
     for binding_key in self.binding_keys:
         queue_name = binding_key + '_Queue'
         self.channel.queue_declare(queue=queue_name, exclusive=True)
         self.channel.queue_bind(exchange=self.exchange,
                                 queue=queue_name,
                                 routing_key=binding_key)
def receive_request(request_data, verbose, server_working_directory):
    logger.info("Request Received")
    request_type = request_type_parsing(request_data)
    header_dictionary = header_parsing(request_data)
    path_from_request = pathname_parsing(request_data)
    body = body_parsing(request_data)
    if request_type == "GET":
        logger.info("Request is of type GET %s", path_from_request)
        return handle_get(header_dictionary, path_from_request, verbose,
                          server_working_directory)
    elif request_type == "POST":
        logger.info("Request is of type POST %s", path_from_request)
        return handle_post(header_dictionary, body, path_from_request, verbose,
                           server_working_directory)
    else:
        raise HTTPException(400)
    def start_listening(self, do_task):
        """
        开始监听队列 default_listen_queue => 路由 binding_keys
        Java 会将消息 发送至模式 coffee.#
        @:param fun do_task 接收到消息后的处理函数
        callback(channel, method, properties, body)
                channel: BlockingChannel
                method: spec.Basic.Deliver
                properties: spec.BasicProperties
                body: bytes
        :raises ValueError:
        """

        validators.require_callback(do_task, callback_name='do_task')

        def callback(ch, method, properties, body):
            delivery_tag = method.delivery_tag
            routing_key = method.routing_key
            msg_body = body.decode('utf-8')
            logger.info("Received message from %s, body: %s ", routing_key,
                        msg_body)
            record = json.loads(msg_body)
            if 'record_id' not in record:
                raise AnalysisException(message='illegal msg, miss:record_id')
            record_id = record['record_id']
            # 执行回调
            try:
                do_task(record)
                # msg
                success_message = str({
                    'time_cost': 0,
                    'document_classify': [],
                    'document_kv_info': [],
                    'rule_output_data': [],
                    'record_id': record_id
                })
                self._send_message(success_message)
                self.channel.basic_ack(delivery_tag=delivery_tag)
            except AnalysisException as e:
                logger.warn('analysis exception: error: %s', e)
                fail_msg = str({
                    'sid': e.sid,
                    'message': e.message,
                    'record_id': record_id
                })
                self._send_message(fail_msg, msg_type='fail')
                self.channel.basic_ack(delivery_tag=delivery_tag)
            except BaseException as e:
                logger.error('something bad happened..., error: %s', e.args)
                self.channel.basic_ack(delivery_tag=delivery_tag)
            finally:
                logger.info('Done !')

        for binding_key in self.binding_keys:
            queue_name = binding_key + '_Queue'
            # bind exchange
            self.channel.queue_bind(exchange=self.exchange,
                                    queue=queue_name,
                                    routing_key=binding_key)
            # set consume info
            self.channel.basic_consume(queue=queue_name,
                                       on_message_callback=callback,
                                       auto_ack=False)
        # handle guarantee queue
        self.channel.basic_consume(queue=self.default_listen_queue,
                                   on_message_callback=callback,
                                   auto_ack=False)

        logger.info('Start listening message with binding routing_keys: %s',
                    self.binding_keys)
        self.channel.start_consuming()