https://prefect.io logo
Title
d

Dan Wise

11/10/2022, 11:18 AM
Hi, am struggling to find a standard way to handle flow state changes in Prefect 2. the state handler approach in v1 is deprecated. I need to define a decorator I assume which applies the flow and then applies some kind of state handler on top of that. Does anyone have some code samples on how to do that please?
1
c

Christopher Boyd

11/10/2022, 3:03 PM
Hi Dan, I think notifications are what you are looking for? https://docs.prefect.io/ui/notifications/
k

Khuyen Tran

11/10/2022, 4:04 PM
This might be something you are looking for: https://khuyentran1401.github.io/prefect-alert/
Right now this collection only supports alerting on failure, but we plan to support handling any states in the future
d

Dan Wise

11/10/2022, 4:34 PM
Thanks. I tried prefect-alert and it works fine when running as per the example directly from a script. However when I deploy then run with an agent the decorator is not triggered.
Also SlackWebhook uses the old incoming webhook alerts. We use messages posted via an app using the slack client so was wondering it a notification type will be created for firing messages to Slack that way
Also, in the alert_on_failure decorator, you define the block_type to be an object instance of a AppriseNotificationBlock but I believe you are expecting a class instance
And thanks @Christopher Boyd I have seen Notifications. But I was looking for a decorator much like @Khuyen Tran was referring to.
k

Khuyen Tran

11/10/2022, 4:49 PM
When I deploy then run with an agent the decorator is not triggered
Which version of prefect-alert are you in?
We use messages posted via an app using the slack client so was wondering it a notification type will be created for firing messages to Slack that way
You mean creating notification using something that is not a notification block?
In the alert_on_failure decorator, you define the block_type to be an object instance of a AppriseNotificationBlock but I believe you are expecting a class instance
Could you be more specific about that you mean by object instance and class instance?
d

Dan Wise

11/10/2022, 4:55 PM
I'm on 0.1.3 of prefect-alert
def alert_on_failure(block_type: AppriseNotificationBlock, block_name: str):
This indicates block_type should be e.g. an instance of a SlackWebhook or similar derived class
According to your type hint
However it actually want you to pass a class instance so SlackWebhook not SlackWebhook("my-hook-name")
So the type hint is telling you one thing but the actuality is you want a different type passed to the hint
This is highlighted nicely in PyCharm for instance
Of course doesn't stop the code from working
To fix the type hint, use:def alert_on_failure(block_type:Type[AppriseNotificationBlock], block_name: str):
k

Khuyen Tran

11/10/2022, 7:41 PM
Thanks for the feedback. I’ll make some changes to prefect-alert and keep you updated
:gratitude-thank-you: 1