コード例 #1
0
ファイル: publish.py プロジェクト: xulong2005/zato
    def _get_message(self,
                     topic,
                     input,
                     now,
                     pattern_matched,
                     endpoint_id,
                     _initialized=_initialized,
                     _zato_none=ZATO_NONE):

        priority = get_priority(self.cid, input)
        expiration = get_expiration(self.cid, input)
        expiration_time = now + (expiration * 1000)

        pub_msg_id = input.get('msg_id', '').encode('utf8') or new_msg_id()

        has_gd = input.get('has_gd', _zato_none)
        if has_gd != _zato_none:
            if not isinstance(has_gd, bool):
                raise ValueError(
                    'Input has_gd is not a bool (found:`{}`)'.format(
                        repr(has_gd)))
        else:
            has_gd = topic.has_gd

        pub_correl_id = input.get('correl_id')
        in_reply_to = input.get('in_reply_to')
        ext_client_id = input.get('ext_client_id')

        ext_pub_time = input.get('ext_pub_time')
        if ext_pub_time:
            ext_pub_time = dt_parse(ext_pub_time)
            ext_pub_time = datetime_to_ms(ext_pub_time)

        pub_correl_id = pub_correl_id.encode('utf8') if pub_correl_id else None
        in_reply_to = in_reply_to.encode('utf8') if in_reply_to else None
        ext_client_id = ext_client_id.encode('utf8') if ext_client_id else None

        ps_msg = PubSubMessage()
        ps_msg.topic = topic
        ps_msg.pub_msg_id = pub_msg_id
        ps_msg.pub_correl_id = pub_correl_id
        ps_msg.in_reply_to = in_reply_to
        ps_msg.pub_time = now
        ps_msg.delivery_status = _initialized
        ps_msg.pattern_matched = pattern_matched
        ps_msg.data = input['data'].encode('utf8')
        ps_msg.mime_type = input.get('mime_type', 'text/plain')
        ps_msg.size = len(input['data'])
        ps_msg.priority = priority
        ps_msg.expiration = expiration
        ps_msg.expiration_time = expiration_time
        ps_msg.published_by_id = endpoint_id
        ps_msg.topic_id = topic.id
        ps_msg.cluster_id = self.server.cluster_id
        ps_msg.has_gd = has_gd
        ps_msg.ext_client_id = ext_client_id
        ps_msg.ext_pub_time = ext_pub_time
        ps_msg.group_id = input.get('group_id') or None
        ps_msg.position_in_group = input.get('position_in_group') or None

        # Invoke hook service here because it may want to update data in which case
        # we need to take it into account below.
        if topic.before_publish_hook_service_invoker:
            response = topic.before_publish_hook_service_invoker(topic, ps_msg)

            # Hook service decided that we should not process this message
            if response['skip_msg']:
                logger_audit.info(
                    'Skipping message pub_msg_id:`%s`, pub_correl_id:`%s`, ext_client_id:`%s`',
                    ps_msg.pub_msg_id, ps_msg.pub_correl_id,
                    ps_msg.ext_client_id)
                return
            else:
                ps_msg.size = len(ps_msg.data)

        # These are needed only for GD messages that are stored in SQL
        if has_gd:
            ps_msg.data_prefix = ps_msg.data[:2048].encode('utf8')
            ps_msg.data_prefix_short = ps_msg.data[:64].encode('utf8')

        return ps_msg
コード例 #2
0
ファイル: publish.py プロジェクト: Mattlk13/zato
    def _get_message(self,
                     topic,
                     input,
                     now,
                     pub_pattern_matched,
                     endpoint_id,
                     subscriptions_by_topic,
                     has_wsx_no_server,
                     _initialized=_initialized,
                     _zato_none=ZATO_NONE,
                     _skip=PUBSUB.HOOK_ACTION.SKIP,
                     _default_pri=PUBSUB.PRIORITY.DEFAULT,
                     _opaque_only=PUBSUB.DEFAULT.SK_OPAQUE,
                     _float_str=PUBSUB.FLOAT_STRING_CONVERT):

        priority = get_priority(self.cid, input)

        # So as not to send it to SQL if it is a default value anyway = less overhead = better performance
        if priority == _default_pri:
            priority = None

        expiration = get_expiration(self.cid, input)
        expiration_time = now + (expiration / 1000.0)

        pub_msg_id = input.get('msg_id', '').encode('utf8') or new_msg_id()

        # If there is at least one WSX subscriber to this topic which is not connected at the moment,
        # which means it has no delivery server, we uncoditionally turn this message into a GD one ..
        if has_wsx_no_server:
            has_gd = True
            logger_pubsub.info(_log_turning_gd_msg.format('wsx'), pub_msg_id)

        # .. otherwise, use input GD value or the default per topic.
        else:
            has_gd = input.get('has_gd', _zato_none)
            if has_gd != _zato_none:
                if not isinstance(has_gd, bool):
                    raise ValueError(
                        'Input has_gd is not a bool (found:`{}`)'.format(
                            repr(has_gd)))
            else:
                has_gd = topic.has_gd

        pub_correl_id = input.get('correl_id')
        in_reply_to = input.get('in_reply_to')
        ext_client_id = input.get('ext_client_id')
        mime_type = input.get('mime_type')

        ext_pub_time = input.get('ext_pub_time') or None
        if ext_pub_time:
            ext_pub_time = dt_parse(ext_pub_time)
            ext_pub_time = datetime_to_ms(ext_pub_time) / 1000.0

        pub_correl_id = pub_correl_id.encode('utf8') if pub_correl_id else None
        in_reply_to = in_reply_to.encode('utf8') if in_reply_to else None
        ext_client_id = ext_client_id.encode('utf8') if ext_client_id else None
        mime_type = mime_type.encode('utf8') if mime_type else None
        reply_to_sk = input.get('reply_to_sk') or []
        deliver_to_sk = input.get('deliver_to_sk') or []

        user_ctx = input.get('user_ctx')
        zato_ctx = input.get('zato_ctx')

        ps_msg = PubSubMessage()
        ps_msg.topic = topic
        ps_msg.pub_msg_id = pub_msg_id
        ps_msg.pub_correl_id = pub_correl_id
        ps_msg.in_reply_to = in_reply_to

        # Convert to string to prevent pg8000 from rounding up float values
        ps_msg.pub_time = _float_str.format(now)
        ps_msg.ext_pub_time = _float_str.format(
            ext_pub_time) if ext_pub_time else ext_pub_time

        ps_msg.delivery_status = _initialized
        ps_msg.pub_pattern_matched = pub_pattern_matched
        ps_msg.data = input['data']
        ps_msg.mime_type = mime_type
        ps_msg.priority = priority
        ps_msg.expiration = expiration
        ps_msg.expiration_time = expiration_time
        ps_msg.published_by_id = endpoint_id
        ps_msg.topic_id = topic.id
        ps_msg.topic_name = topic.name
        ps_msg.cluster_id = self.server.cluster_id
        ps_msg.has_gd = has_gd
        ps_msg.ext_client_id = ext_client_id
        ps_msg.group_id = input.get('group_id') or None
        ps_msg.position_in_group = input.get('position_in_group') or None
        ps_msg.is_in_sub_queue = bool(subscriptions_by_topic)
        ps_msg.reply_to_sk = reply_to_sk
        ps_msg.deliver_to_sk = deliver_to_sk
        ps_msg.user_ctx = user_ctx
        ps_msg.zato_ctx = zato_ctx

        # Opaque attributes - we only need reply to sub_keys to be placed in there
        # but we do not do it unless we known that any such sub key was actually requested.
        if reply_to_sk or deliver_to_sk:
            set_instance_opaque_attrs(ps_msg, input, only=_opaque_only)

        # If there are any subscriptions for the topic this message was published to, we want to establish
        # based on what subscription pattern each subscriber will receive the message.
        for sub in subscriptions_by_topic:
            ps_msg.sub_pattern_matched[sub.sub_key] = sub.sub_pattern_matched

        if ps_msg.data:
            ps_msg.size = len(
                ps_msg.data.encode('utf8')
            )  # We need to store the size in bytes rather than Unicode codepoints
        else:
            ps_msg.size = 0

        # Invoke hook service here because it may want to update data in which case
        # we need to take it into account below.
        if topic.before_publish_hook_service_invoker:
            response = topic.before_publish_hook_service_invoker(topic, ps_msg)

            # Hook service decided that we should not process this message
            if response['hook_action'] == _skip:
                logger_audit.info(
                    'Skipping message pub_msg_id:`%s`, pub_correl_id:`%s`, ext_client_id:`%s`',
                    ps_msg.pub_msg_id, ps_msg.pub_correl_id,
                    ps_msg.ext_client_id)
                return

        # These are needed only for GD messages that are stored in SQL
        if has_gd:
            data_prefix, data_prefix_short = self._get_data_prefixes(
                ps_msg.data)
            ps_msg.data_prefix = data_prefix
            ps_msg.data_prefix_short = data_prefix_short

        return ps_msg
コード例 #3
0
    def _get_message(self,
                     topic,
                     input,
                     now,
                     pattern_matched,
                     endpoint_id,
                     has_subs,
                     _initialized=_initialized,
                     _zato_none=ZATO_NONE,
                     _skip=PUBSUB.HOOK_ACTION.SKIP,
                     _default_pri=PUBSUB.PRIORITY.DEFAULT):

        priority = get_priority(self.cid, input)

        # So as not to send it to SQL if it is a default value anyway = less overhead = better performance
        if priority == _default_pri:
            priority = None

        expiration = get_expiration(self.cid, input)
        expiration_time = now + (expiration / 1000.0)

        pub_msg_id = input.get('msg_id', '').encode('utf8') or new_msg_id()

        has_gd = input.get('has_gd', _zato_none)
        if has_gd != _zato_none:
            if not isinstance(has_gd, bool):
                raise ValueError(
                    'Input has_gd is not a bool (found:`{}`)'.format(
                        repr(has_gd)))
        else:
            has_gd = topic.has_gd

        pub_correl_id = input.get('correl_id')
        in_reply_to = input.get('in_reply_to')
        ext_client_id = input.get('ext_client_id')
        mime_type = input.get('mime_type')

        ext_pub_time = input.get('ext_pub_time') or None
        if ext_pub_time:
            ext_pub_time = dt_parse(ext_pub_time)
            ext_pub_time = datetime_to_ms(ext_pub_time)

        pub_correl_id = pub_correl_id.encode('utf8') if pub_correl_id else None
        in_reply_to = in_reply_to.encode('utf8') if in_reply_to else None
        ext_client_id = ext_client_id.encode('utf8') if ext_client_id else None
        mime_type = mime_type.encode('utf8') if mime_type else None

        ps_msg = PubSubMessage()
        ps_msg.topic = topic
        ps_msg.pub_msg_id = pub_msg_id
        ps_msg.pub_correl_id = pub_correl_id
        ps_msg.in_reply_to = in_reply_to
        ps_msg.pub_time = now
        ps_msg.delivery_status = _initialized
        ps_msg.pattern_matched = pattern_matched
        ps_msg.data = input['data']
        ps_msg.mime_type = mime_type
        ps_msg.priority = priority
        ps_msg.expiration = expiration
        ps_msg.expiration_time = expiration_time
        ps_msg.published_by_id = endpoint_id
        ps_msg.topic_id = topic.id
        ps_msg.topic_name = topic.name
        ps_msg.cluster_id = self.server.cluster_id
        ps_msg.has_gd = has_gd
        ps_msg.ext_client_id = ext_client_id
        ps_msg.ext_pub_time = ext_pub_time
        ps_msg.group_id = input.get('group_id') or None
        ps_msg.position_in_group = input.get('position_in_group') or None
        ps_msg.is_in_sub_queue = has_subs

        if ps_msg.data:
            ps_msg.size = len(
                ps_msg.data.encode('utf8')
            )  # We need to store the size in bytes rather than Unicode codepoints
        else:
            ps_msg.size = 0

        # Invoke hook service here because it may want to update data in which case
        # we need to take it into account below.
        if topic.before_publish_hook_service_invoker:
            response = topic.before_publish_hook_service_invoker(topic, ps_msg)

            # Hook service decided that we should not process this message
            if response['hook_action'] == _skip:
                logger_audit.info(
                    'Skipping message pub_msg_id:`%s`, pub_correl_id:`%s`, ext_client_id:`%s`',
                    ps_msg.pub_msg_id, ps_msg.pub_correl_id,
                    ps_msg.ext_client_id)
                return

        # These are needed only for GD messages that are stored in SQL
        if has_gd:
            data_prefix, data_prefix_short = self._get_data_prefixes(
                ps_msg.data)
            ps_msg.data_prefix = data_prefix
            ps_msg.data_prefix_short = data_prefix_short

        return ps_msg