Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorating a single function with multiple listeners does not work #62

Open
aktarmiah opened this issue Oct 27, 2023 · 2 comments
Open

Comments

@aktarmiah
Copy link

aktarmiah commented Oct 27, 2023

Tested in version 1.1.1

I have the following setup:

# channels.py
@dataclass
class MyChannel(TriggerChannel):
    model = MyTable
    lock_notifications = False

# listeners.py
@post_insert_listener(channels.MyChannel)
@post_save_listener(channels.MyChannel)
@post_delete_listener(channels.MyChannel)
class MyHandlerClass():
    def __init__(self, old, new) -> None:
        print("Triggered!!")

After a deleting a single record from MyTable my console shows

Triggered!!
Triggered!!
Triggered!!

I was expecting a single trigger (the delete trigger) but it seems like all the listeners are being triggered.
All migrations are up to date.

@PaulGilmartin
Copy link
Owner

Hmm, I actually don't think I've ever tried adding two listeners to a single function before. Thanks for raising, i'll have a look at it

@PaulGilmartin
Copy link
Owner

Had a quick look at it. I tried adding a second listener to one of the functions in pgpubsub/tests/listeners.py

@pgpubsub.post_save_listener(PostTriggerChannel)    # new     
@pgpubsub.post_delete_listener(PostTriggerChannel)
def email_author(old: Post, new: Post):
    author = Author.objects.get(pk=old.author_id)
    print(f'Emailing {author.name} to inform then post '
          f'{old.pk} has been edited or deleted.')
    email(author)

I can recreate the same issue you mentioned. In fact it looks like the top decorator, post_save_listener, is not registered at all either (adding it didn't even generate a migration to add the new trigger). Right now I'm not sure exactly why that would happen. Unfortunately I don't have any more time to look at this tonight. I will prioritise this for the next release, but not sure exactly when that will be yet. Please feel very free to submit a fix if you can think of one.

@PaulGilmartin PaulGilmartin changed the title Incorrectly triggers on TriggerChannel Decorating a single function with multiple listeners does not work Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants