示例#1
0
    def on_message(self, headers: Dict[str, Any], message: Any) -> None:
        """Set a property when a MESSAGE frame is received by the STOMP connection.

        If the needed key is in the headers dict and has the correct value, signal
        that we've been successful.

        Args:
            headers: a dictionary containing all headers sent by the server
            as key/value pairs.

            message: the frame's payload - the message body.
        """
        try:
            if "model_class" in headers:
                if headers["model_class"] == "Operation":
                    parsed = SchemaParser.parse_operation(message,
                                                          from_string=True)

                    if parsed.model and parsed.model.name:
                        if parsed.model.name.startswith("REQUEST"):
                            self.create_event_captured = True
                        else:
                            print("ERROR: no 'REQUEST' in parsed model")
                    else:
                        print("ERROR: no parsed model found")
                elif headers["model_class"] == "error_message":
                    print(f"ERROR: Message returned: {message!r}")
            else:
                print(
                    f"ERROR: 'model_class' not in headers, message={message}")
        except Exception:
            print(f"ERROR: unable to parse, message={message}")
示例#2
0
    async def post(self):
        """
        ---
        summary: Forward a request from a parent or child BG instance
        description: |
            When a Beer Garden needs to forward a request, this API will support routing
            to all CRUD actions exposed by the entry points.
        parameters:
          - name: forward
            in: body
            required: true
            description: The Forward Object
            schema:
                $ref: '#/definitions/Forward'
        responses:
          200:
            description: Forward Request Accepted
          400:
            $ref: '#/definitions/400Error'
          50x:
            $ref: '#/definitions/50xError'
        tags:
          - Forward
        """
        operation = SchemaParser.parse_operation(self.request.decoded_body,
                                                 from_string=True)

        response = await self.client(operation)

        self.set_header("Content-Type", "application/json; charset=UTF-8")
        self.write(response)
示例#3
0
    async def post(self):
        """
        ---
        summary: Forward a request from a parent or child BG instance
        description: |
            When a Beer Garden needs to forward a request, this API will support routing
            to all CRUD actions exposed by the entry points.
        parameters:
          - name: blocking
            in: query
            required: false
            description: Flag indicating whether to wait for request completion
            type: boolean
            default: false
          - name: timeout
            in: query
            required: false
            description: Max seconds to wait for request completion. (-1 = wait forever)
            type: float
            default: -1
          - name: forward
            in: body
            required: true
            description: The Forward Object
            schema:
                $ref: '#/definitions/Forward'
        responses:
          204:
            description: Forward Request Accepted
          400:
            $ref: '#/definitions/400Error'
          50x:
            $ref: '#/definitions/50xError'
        tags:
          - Forward
        """
        self.verify_user_global_permission(EVENT_FORWARD)

        operation = SchemaParser.parse_operation(self.request.decoded_body,
                                                 from_string=True)

        task = asyncio.create_task(self.client(operation))

        # Deal with blocking
        blocking = self.get_argument("blocking", default="").lower() == "true"
        if not blocking:
            self.set_status(204)
        else:
            timeout = float(self.get_argument("timeout", default="-1"))

            done, _ = await asyncio.wait({task}, timeout=timeout)

            if not done:
                raise TimeoutExceededError("Timeout exceeded")

            self.set_header("Content-Type", "application/json; charset=UTF-8")
            self.write(done.result())
示例#4
0
    def on_message(self, headers: dict, message: str):
        """Handle an incoming message

        Will first verify that the model type (according to the message headers) is an
        Operation. When creating requests on a child garden the initial response will be
        the created Request object, which we want to ignore.

        Will parse the message as an Operation and attempt to route it. If the result of
        the routing is truthy will send a response with the result.

        If routing raised an exception an error response will be sent.

        Args:
            headers: Message header dict
            message: The message body

        Returns:
            None
        """
        logger.debug(f"Message:\n\tMessage: {message}\n\tHeaders: {headers}")

        try:
            if headers.get("model_class") == "Operation":
                operation = SchemaParser.parse_operation(message,
                                                         from_string=True)

                if hasattr(operation, "kwargs"):
                    operation.kwargs.pop("wait_timeout", None)

                result = beer_garden.router.route(operation)

                if result:
                    send(
                        result,
                        request_headers=headers,
                        conn=self.conn,
                        send_destination=self.send_destination,
                    )
        except Exception as e:
            logger.warning(f"Error parsing and routing message: {e}")
            send(
                str(e),
                request_headers=headers,
                conn=self.conn,
                send_destination=self.send_destination,
            )
    def on_message(self, headers, message):
        try:
            if headers['model_class'] == 'Operation':

                parsed = SchemaParser.parse_operation(message,
                                                      from_string=True)

                if parsed.model and parsed.model.name:
                    if parsed.model.name.startswith("REQUEST"):
                        print(message)
                        self.create_event_captured = True
                    else:
                        print(parsed.model.name)
            elif headers['model_class'] == 'error_message':
                print("Error Message Returned:", message)
        except:
            print("Error: unable to parse message:", message)