https://prefect.io logo
a

Alexis Lucido

12/23/2021, 5:19 PM
Hello everyone. I am still exploring Prefect's functionnalities and would like to use State Handlers to send alerts in case of failure. I was wondering whether we could pass some more arguments such as email receivers, title and body to a State Handler signature? I would like to use some yml file already configured rather than configuring Prefect Secrets. Thanks a lot, and happy holidays!
k

Kevin Kho

12/23/2021, 5:21 PM
Hey @Alexis Lucido, the answer there is not really. This is discouraged because context tends to not me mutable after instantiated but maybe something like this can help you:
Copy code
from prefect import Flow, task
import prefect
import random

def mystatehandler(obj, old_state, new_state):
    if new_state.is_finished():
        print(prefect.context.rand)
    return new_state

@task(state_handlers=[mystatehandler])
def abc(x):
    rand  = random.random()
    prefect.context.rand = rand
    return x + rand

with Flow("some_flow_name") as flow:
    a = abc(1)

flow.run()
a

Alexis Lucido

12/23/2021, 5:24 PM
Hey Kevin. I guess I have understood the concept then. I wanted to do so to get a better loosely coupling between Prefect and our business rules, but I'll reach another compromise. Thank you anyway!