[django-channels] Issues with model post_save and Group.Send()

130 views
Skip to first unread message

Mattias Olsson

unread,
Jun 19, 2017, 10:28:23 AM6/19/17
to Django users
Hi all,

I'm having some issues with integrating automatic group messaging based on a post save signal from Django models.

I'm currently running a Graphene environment (graphene-python.org) to support a webapp running React/Relay. In this setup we're performing Mutations which will manipulate data, and return a payload based on the data that was mutated. During a data mutation process I'm saving a model, which triggers a post_save, which in turn seems to hijack the process and terminate it, before the mutation can return its payload.

I have a simple JsonWebsocketConsumer to handle my connections.
class MyConsumers(JsonWebsocketConsumer):
    channel_session_user = True

    strict_ordering = True

    method_mapping = {
        "websocket.connect": "connect",
        "websocket.receive": "receive",
        "websocket.disconnect": "disconnect",
    }

    def connection_groups(self, **kwargs):
      return ["all-clients", ]

    def connect(self, message, **kwargs):
      # Accepts connections and returns {'Accepted': Trie}
      subscription_manager.SubscriptionManager.connect_endpoint(message)

    def receive(self, content, **kwargs):
      # Registers groups and stores data in redis cache.
      subscription_manager.SubscriptionManager.receive_endpoint(content)

    def disconnect(self, message, **kwargs):
      ...

A mutation comes in through urls as a normal http-request (through the /graphql match):
urlpatterns = [
  ...
  url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=settings.DEBUG))),
  url(r'^admin/', admin.site.urls),
  url(r'^support/', include(support_urls)),
  url(r'^django-rq/', include('django_rq.urls')),
]

The routing is as simple as can be:
subscription_routing = [
    route_class(consumers.QuantumConsumers)
]

channel_routing = [
    include(subscription_routing, path=r"^/ws/subscriptions"),
]

The post save is attached through a util on each model:
class Ping(models.Model):
    support_session = models.OneToOneField(SupportSession, related_name="ping")
    client_ping = models.DateTimeField(...)
    support_ping = models.DateTimeField(...)

# Sets up a post_save
ModelSignalReceiver.connect_receivers(Ping)

Finally, a mutation is a simple method that executes a save:
class PingMutation(graphene.ClientIDMutation):

    class Input:
        support_session_id = graphene.ID()

    ping = graphene.Field(PingNode)

    @classmethod
    def mutate_and_get_payload(cls, args, context, info):
        session_id = from_global_id(args.get('support_session_id'))[1]
        pings = support_models.Ping.objects.get(support_session__id=session_id)

        pings.support_last_ping = datetime.datetime.now()

        # I can see this debug message, before the save.
        logger.debug('SupportPingMutation:pre-save')

        # Will trigger the signal...
        pings.save()

        # The code never reaches this point.
        logger.debug('SupportPingMutation:post-save')

        return cls(ping=pings)


Any ideas on why the process gets hijacked? Should the post_save which triggers a consumer response not be dispatched?

Thank you in advance.

Andrew Godwin

unread,
Jun 19, 2017, 10:33:16 AM6/19/17
to django...@googlegroups.com
What's the traceback when the code exits? There has to be some reason it stops there, and whatever your Python code is running in should be able to tell you why (traceback, status code, anything)

Without that, I can't help, as it could be anything - your code or Django's.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2ae1ece5-24ff-4754-9d21-b8207117292e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mattias Olsson

unread,
Jun 20, 2017, 2:59:13 AM6/20/17
to Django users
There is none - all I can see is that the logging I placed att he very end of the post_save method logs, but the logger.debug('SupportPingMutation:post-save'that I placed after the .save() never runs. After the channels message, the request is stale, and just freezes the server.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andrew Godwin

unread,
Jun 20, 2017, 1:40:54 PM6/20/17
to django...@googlegroups.com
Well, I need to know even a vague shape of what's happening to be able to help - is it getting stuck in an infinite loop? Is it running out of memory? Without this sort of information, I won't be able to find the problem, and I don't have time to go through the entire codebase line by line.

Andrew

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages