User-configurable signalling mechanism in Django
-
16-09-2019 - |
Question
The question here is whether something like this already exists or, if not, whether there's a better way to achieve it than what I describe below.
I need to allow an arbitrary Principal (User, Group, Site Admin) to add Event Sink
s (like email addresses, Webhook URLs, etc.) to the system (through the web interface) and, for each one, specify which kinds of <Event Source, Event Type>
should be sent to it. Since I'm doing this for ReviewBoard, I'll give a concrete example with a hypothetical implementation:
- John creates a new
event_sink
(a webhook), identified bypostbin1
; - John specifies that
postbin1
will receive events of typepublish
onReviewRequest
s (a class-levelsubscription
-- thesource_id
is unspecified); - When a new
review_request
is created, theEvent Manager
, lists (through a JOIN withsubscriptions
) allevent_sinks
interested inReviewRequest
s and creates a (instance-level)subscription
binding them as a listener to their particularevent_type
(the name of a django signal) of interest and to the specificreview_request
; - When that
review_request
gets published, theEvent Manager
(who listens to thepublish
signal) lists allevent_sinks
interested in thatreview_request
instance and thepublish
signal and dispatches the signal parameters to theirsink
method. - The Webhook
event_sink
marshals the data however pleases it and POSTs it to its URL.
This is the schema I thought of: alt text http://bayimg.com/image/aadgoaacd.jpg
I'm about to start implementing this myself, but I just want to make sure I'm not reinventing the wheel. Couldn't find anything on Google. Ready-to-use package names, half-way package names that will help me, and/or criticism to my DIY approach are all welcome.
Solution
django-notification by James Tauber gets you a good chunk of the way there. I would love to see that app mature if you're willing to work off of that codebase it would be great.
OTHER TIPS
I once built something very similar, but as a small, REST app (in TurboGears).
Never quite got the performance and reliability up to where I could use it for anything serious. And it was designed before webhooks started to take root, so I'd probably take that into consideration now.
As I'm also working in Django now, I'll be curious to see what you come up with. I think your design is about right, and this kind of event dispatcher is very useful and worth working on.
Isn't this a case for pub/sub? Maybe something like that