PrefectHQ / prefect

Prefect is a workflow orchestration framework for building resilient data pipelines in Python.
https://prefect.io
Apache License 2.0
15.98k stars 1.57k forks source link

Allow users to watch engine events #7787

Open felix-ht opened 1 year ago

felix-ht commented 1 year ago

First check

Prefect Version

2.x

Describe the current behavior

Currently the prefect engine is loaded as a module - all components of it are implemented as functions directly in the module. State is stored in the module as well. This certainly is the pythonic way of doing singeltons.

The issues is that this makes it hard if someone wants to trigger some action based on the actions of the engine.

So if i want to setup a hook that gets called whenever a flow/task is started i would have to monkey patch the engine. This is best avoided as this requires knowledge of implementation details, making it quite brittle as a consequence.

Describe the proposed behavior

Add add functions to the engine to register callbacks that fire as the engine does a certain actions - like starting a flow.

This callbacks should the get access to the context used in triggering the action.

Example Use

A usecase for this would be to do some custom logging for inputs of flows.

As long as this api is stable it would also be quite straightforward to implement things like an open linage integration.

https://github.com/OpenLineage/OpenLineage/issues/81

Additional context

This might be related of https://github.com/PrefectHQ/prefect/issues/7766 but on a lower level

zanieb commented 1 year ago

Hey! I'm interested in doing this during a refactor of the engine (which has definitely outgrown its original simple design) — but it's not going to happen quickly.