PaulGilmartin / django-pgpubsub

A distributed task processing framework for Django built on top of the Postgres NOTIFY/LISTEN protocol.
Other
245 stars 12 forks source link

Multiple usage of the same channel #57

Closed aktarmiah closed 1 year ago

aktarmiah commented 1 year ago

I'm finding that I have to create multiple trigger based channels relating to the same model or I get an error. For example I'd like to trigger on insert and update for the same model but I need two separate channels for this.

Am I missing something

PaulGilmartin commented 1 year ago

@aktarmiah That should be supported, you just need to define your listener correctly. For the scenario you mentioned, there's a built in pgpubsub.post_save_listener which will trigger on both creations and updates. See django-pgpubsub/pgpubsub/tests /listeners.py which has some examples of channels which trigger on multiple actions

@pgpubsub.post_save_listener(ChildTriggerChannel)
def post_child_save(old: Child, new: Child):
    print(f'New child created {new}.')
PaulGilmartin commented 1 year ago

@aktarmiah Hope that helped :). Let me know, otherwise I will mark the issue as resolved.

aktarmiah commented 1 year ago

While the post_save_listener does indeed do the job of combining the create and update trigger, I'm still finding that I can't use the same channel elsewhere.

For eg

@post_save_listener(mychannel)
def foo(old, new):
    print("foo")

# Results in console error saying the channel is already being used
@post_save_listener(mychannel)
def bar(old, new):
    print("bar")
PaulGilmartin commented 1 year ago

@aktarmiah Ah, that does sound like a bug then. I will look at that for the next release, which I hope will be within the next few weeks.

PaulGilmartin commented 1 year ago

@aktarmiah I have merged a fix for this in https://github.com/Opus10/django-pgpubsub/commit/66c783d0a80bcf837124744aa7e70e0bb69ac646. I'll do a proper release to pypi alongside fixes for some of the other issues soon.