Understanding DiscourseEvent

(Salman, Freelance Developer) #1

I’m just looking at DiscourseEvent and I want to make sure I understand how it works.

It looks like a very simple in-memory hash, and for a given key (which is an specific event), it takes an array of functions to call.

  1. I can’t seem to find where an event gets consumed in the codebase, or where a function is added to the array of functions to call when the event fires. Can someone point me to an example?

  2. Was this created for internal use or is this just for plugins?
    Example, when a post gets approved or disapproved, lots of things happen under the covers. Is this event used internally for things like this?

  3. Say my discourse install gets shutdown and exactly when it was being shutdown, it was in a middle of a transaction or it was doing a series of tasks that is now broken mid-way. Is there a way to re-calculate everything to fix what might be half-baked/broken?

(Robin Ward) #2

It is meant for plugins, which is why you can’t find many places in our codebase where DiscourseEvent.on is called. However, there is one, for emoji, which remains because emoji was originally a plugin that we integrated into the master codebase.

Regarding the shutdown question – that is a tricky one and I am not sure. If the transaction is at a database level, there are only two possible states: the transaction finishes or it is rolled back. There is no need to complete it. However, other operations might act a bit funky if aborted half way through. Having said that, I’ve been working on this codebase for years and killed many many processes and don’t recall ever messing it up in that way.

Automation based on user sign ups