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

Is not possible to notify a channel from another? #83

Open EduardoJM opened 1 month ago

EduardoJM commented 1 month ago

Hi! Thanks for this project.

I got a strange behaviour. For example, i have two channels, called ChatProcess and ChatProcessFinished and two listeners on the format:

import time
import pgpubsub
from .channels import ChatProcess, ChatProcessFinished

@pgpubsub.listener(ChatProcess)
def test_a(chatlog_id: int):
    print(f'Question is created: {chatlog_id}')
    time.sleep(3)
    print("Finishing!!!!")
    pgpubsub.notify(
        'chat.chat_log.channels.ChatProcessFinished',
        chatlog_id=chatlog_id,
    )

@pgpubsub.listener(ChatProcessFinished)
def test_b(chatlog_id: int):
    print(f'Question is finished: {chatlog_id}')
Then, in the logs i got this:
2024-10-15 17:42:17,933 INFO Restarting process
2024-10-15 17:42:18,174 INFO Listening on chat.chat_log.channels.ChatProcess

2024-10-15 17:42:18,175 INFO Listening on chat.chat_log.channels.ChatProcessFinished

2024-10-15 17:42:18,175 INFO Listening for notifications... 

2024-10-15 17:42:33,373 INFO Processing notification for chat.chat_log.channels.ChatProcess

Question is created: 37
Finishing!!!!
2024-10-15 17:42:36,375 INFO Notifying channel chat.chat_log.channels.ChatProcessFinished with payload {"kwargs": {"chatlog_id": 37}}

The ChatProcessFinished is not processed. But if i notify the two channels from the application (in a view, for example), the both is processed (then the ChatProcessFinished is processed two times, one for the view and one for the test_a() listener function:

2024-10-15 17:47:15,501 INFO Restarting process
2024-10-15 17:47:15,743 INFO Listening on chat.chat_log.channels.ChatProcess

2024-10-15 17:47:15,743 INFO Listening on chat.chat_log.channels.ChatProcessFinished

2024-10-15 17:47:15,744 INFO Listening for notifications... 

2024-10-15 17:47:21,126 INFO Processing notification for chat.chat_log.channels.ChatProcess

Question is created: 38
Finishing!!!!
2024-10-15 17:47:24,127 INFO Notifying channel chat.chat_log.channels.ChatProcessFinished with payload {"kwargs": {"chatlog_id": 38}}
2024-10-15 17:47:24,127 INFO Processing notification for chat.chat_log.channels.ChatProcessFinished

Question is finished: 38
2024-10-15 17:47:24,128 INFO Processing notification for chat.chat_log.channels.ChatProcessFinished

Question is finished: 38

This is a bug or i missed some thing on the process?

PaulGilmartin commented 2 days ago

@EduardoJM Thanks for your interest in this library! This definitely seems like a bug. It should be possibly to notify another channel from within a listener function as you have. I have never tried it myself though, so I guess something goes wrong somewhere. Hopefully we can make a fix for it in the future. If you have any ideas for a fix I'd be happy to review a PR.