示例#1
0
 def foo(self, channel, content, message, match):
     consumer, kwargs = match
     if self.callback:
         self.callback(channel, message)
     try:
         logger.debug("Dispatching message on %s to %s", channel,
                      name_that_thing(consumer))
         # Send consumer started to manage lifecycle stuff
         consumer_started.send(sender=self.__class__, environ={})
         # Run consumer
         consumer(message, **kwargs)
     except DenyConnection:
         # They want to deny a WebSocket connection.
         if message.channel.name != "websocket.connect":
             raise ValueError(
                 "You cannot DenyConnection from a non-websocket.connect handler."
             )
         message.reply_channel.send({"close": True})
     except ChannelSocketException as e:
         e.run(message)
     except ConsumeLater:
         # They want to not handle it yet. Re-inject it with a number-of-tries marker.
         content['__retries__'] = content.get("__retries__", 0) + 1
         # If we retried too many times, quit and error rather than
         # spinning forever
         if content['__retries__'] > self.message_retries:
             logger.warning(
                 "Exceeded number of retries for message on channel %s: %s",
                 channel,
                 repr(content)[:100],
             )
             return
         # Try to re-insert it a few times then drop it
         for _ in range(10):
             try:
                 self.channel_layer.send(channel, content)
             except self.channel_layer.ChannelFull:
                 gevent.sleep(0.05)
                 print "full"
             else:
                 break
     except:
         logger.exception("Error processing message with consumer %s:",
                          name_that_thing(consumer))
     finally:
         # Send consumer finished so DB conns close etc.
         consumer_finished.send(sender=self.__class__)
示例#2
0
    def assertRoute(self, router, channel, content, consumer, kwargs=None):
        """
        Asserts that asking the `router` to route the `content` as a message
        from `channel` means it returns consumer `consumer`, optionally
        testing it also returns `kwargs` to be passed in

        Use `consumer` = None to assert that no route is found.
        """
        message = Message(content, channel, channel_layer="fake channel layer")
        match = router.match(message)
        if match is None:
            if consumer is None:
                return
            else:
                self.fail("No route found for %s on %s; expecting %s" % (
                    content,
                    channel,
                    name_that_thing(consumer),
                ))
        else:
            mconsumer, mkwargs = match
            if consumer is None:
                self.fail("Route found for %s on %s; expecting no route." % (
                    content,
                    channel,
                ))
            self.assertEqual(
                consumer, mconsumer,
                "Route found for %s on %s; but wrong consumer (%s not %s)." % (
                    content,
                    channel,
                    name_that_thing(mconsumer),
                    name_that_thing(consumer),
                ))
            if kwargs is not None:
                self.assertEqual(
                    kwargs, mkwargs,
                    "Route found for %s on %s; but wrong kwargs (%s not %s)." %
                    (
                        content,
                        channel,
                        mkwargs,
                        kwargs,
                    ))
示例#3
0
    def assertRoute(self, router, channel, content, consumer, kwargs=None):
        """
        Asserts that asking the `router` to route the `content` as a message
        from `channel` means it returns consumer `consumer`, optionally
        testing it also returns `kwargs` to be passed in

        Use `consumer` = None to assert that no route is found.
        """
        message = Message(content, channel, channel_layer="fake channel layer")
        match = router.match(message)
        if match is None:
            if consumer is None:
                return
            else:
                self.fail("No route found for %s on %s; expecting %s" % (
                    content,
                    channel,
                    name_that_thing(consumer),
                ))
        else:
            mconsumer, mkwargs = match
            if consumer is None:
                self.fail("Route found for %s on %s; expecting no route." % (
                    content,
                    channel,
                ))
            self.assertEqual(consumer, mconsumer, "Route found for %s on %s; but wrong consumer (%s not %s)." % (
                content,
                channel,
                name_that_thing(mconsumer),
                name_that_thing(consumer),
            ))
            if kwargs is not None:
                self.assertEqual(kwargs, mkwargs, "Route found for %s on %s; but wrong kwargs (%s not %s)." % (
                    content,
                    channel,
                    mkwargs,
                    kwargs,
                ))
示例#4
0
    def _consumer(message, *args, **kwargs):
        name = name_that_thing(consumer)
        group = get_consumer_group(name)
        info = {
            'layer': alias,
            'channel': message.channel.name,
            'consumer': name,
            'call_args': args,
            'call_kwargs': kwargs,
            'message': message.content,
        }

        try:
            result = consumer(message, *args, **kwargs)
        except Exception:
            info['message'] = traceback.format_exc()
            send_debug(info, 'error', group)
            raise
        else:
            send_debug(info, 'run', group)
            return result
示例#5
0
    def get_context(self):
        consumers = []

        for channels, consumer, filters, prefix in get_routes(
                DEFAULT_CHANNEL_LAYER):
            if any((in_debug(channel) for channel in channels)):
                name = name_that_thing(consumer)
                if name in 'channels.routing.null_consumer' or is_no_debug(
                        consumer):
                    continue
                consumers.append({
                    'name': name,
                    'channels': channels,
                    'prefix': filters_to_string(prefix),
                    'filters': filters_to_string(filters),
                    'group': get_consumer_group(name),
                })
        return {
            'consumers': consumers,
            'profile': get_setting_value('PROFILE_CONSUMERS'),
        }