Skip to content

feat: Add event_listeners module for event lifecycle support#203

Closed
morgan-wowk wants to merge 1 commit intoenums-refactorfrom
event-system
Closed

feat: Add event_listeners module for event lifecycle support#203
morgan-wowk wants to merge 1 commit intoenums-refactorfrom
event-system

Conversation

@morgan-wowk
Copy link
Copy Markdown
Collaborator

Provides a general-purpose publish/subscribe system with:

  • Event marker base class (enforced via TypeVar bound)
  • StatusTransitionEvent dataclass (frozen, kw_only) with typed
    ContainerExecutionStatus fields
  • subscribe(event_type, callback) keyed by event class
  • emit(event) dispatches to all subscribers by type(event)

Copy link
Copy Markdown
Collaborator Author

morgan-wowk commented Apr 8, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@morgan-wowk morgan-wowk force-pushed the event-system branch 2 times, most recently from f851682 to 38ecf70 Compare April 8, 2026 23:17
@morgan-wowk morgan-wowk changed the base branch from enums-refactor to graphite-base/203 April 8, 2026 23:59
@morgan-wowk morgan-wowk marked this pull request as ready for review April 9, 2026 00:35
@morgan-wowk morgan-wowk requested a review from Ark-kun as a code owner April 9, 2026 00:35
@morgan-wowk morgan-wowk changed the title feat: Add event_listeners module with typed StatusTransitionEvent feat: Add event_listeners module for event lifecycle support Apr 9, 2026
Provides a general-purpose publish/subscribe system with:
- Event marker dataclass base (kw_only=True) enforced via TypeVar bound
- subscribe(event_type, callback, asynchronous=True) keyed by event class;
  async subscribers run on a daemon thread, sync on the calling thread
- emit(event) dispatches to all subscribers by type(event)

Co-located tests cover sync/async dispatch, type isolation, and that
asynchronous=True is the default.
@morgan-wowk morgan-wowk changed the base branch from graphite-base/203 to enums-refactor April 9, 2026 19:15

_EventType = typing.TypeVar("_EventType", bound=Event)

_listeners: dict[type, list[_CallbackEntry]] = {}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - there is typing.Final which will have linting to make sure this variable can't be assigned. Not necessary needed here, but just an FYI when I create global variables.

@@ -0,0 +1,45 @@
import dataclasses
import threading
import typing
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google style guide for typing would be to not import it as a module, I believe we do that in other places in this repo as well.

https://google.github.io/styleguide/pyguide.html#31912-imports-for-typing

image.png

Comment thread cloud_pipelines_backend/event_listeners.py
if asynchronous:
threading.Thread(target=callback, args=(event,), daemon=True).start()
else:
callback(event)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe wrapping a try/catch here would be important to not break the emit for loop?

Add a new test for this too.

Copy link
Copy Markdown
Collaborator Author

@morgan-wowk morgan-wowk Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm...I guess that would help ensure at least the "async" listeners are guarenteed. But a synchronous listener should prevent any further action / subsequent synchronous listners ffrom executing.

Does that sound good? We add a try catch to ensure the async listeners are always called?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also just dispatch the asychronous listeners first instead of having a try catch

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

Successfully merging this pull request may close these issues.

2 participants