Hey all! I am using prefect cli over cloud, and am...
# ask-community
p
Hey all! I am using prefect cli over cloud, and am trying to get slack notifications working on an individual deployment level. I have it working on the flow level, but when multiple deployments reuse the same flow logic, it doesn't always send to the right slack channel. Is it possible to add in a notification to send on failure direct into the deployment.yaml? Cheers!
1
n
hi @Paige Fuller by this
multiple deployments reuse the same flow logic
do you mean that you're making many deployments from the same flow? if so, an
on_failure
hook that sends to the correct slack channel might be the easiest way to go
p
yep, for example a dbt flow where we just change the tag. So the flow is the same, the deployment yaml just passes in a different tag parameter. We have an on_failure on the flow, but two deployments using that same flow might need to alert a different teams channel - hope I've explained that better this time!
n
gotcha, so ultimately you just need to decide which slack channel based on the
tag
you pass into your flow? or how do you decide
p
to be fair, now that you've said it, I suppose I could pass in the webhook block in as a parameter to the function... and now I feel stupid lol
n
🙂 there's always a lot of different ways to do stuff usually I like passing the names of blocks around. so maybe you could go create a webhook block for each channel (if you're using the slack specific one) and pass in the name or you could use the generic webhook one for everything and pass in what you need to call that
❤️ 1
p
thanks! This has been very helpful!
n
catjam
p
@Nate me again! potentially silly question, can I trigger a slack notification to the passed in webhook on failure from within the flow, or does it need to be in the decorator?
n
hi @Paige Fuller - sorry (I still need coffee) I dont think I understand the question - can you clarify what you mean?
p
I think I am confusing myself 😞 At the moment, we have a flow with a decorator that on_failure send a slack notification to a fairly hardcoded slack webhook. We now have separate webhooks for each channel we want to send to. If i want to edit the deployment.yml to pass in the webhook block name as a parameter to the flow, is there a good way to send the notifications from inside the flow decorated function? Or can the on_failure only be triggered from the decorator? I still have done a terrible job explaining that but hopefully it makes sense??
n
no worries! I would do something like this
Copy code
from prefect import flow
from prefect.blocks.notifications import SlackWebhook
from prefect.blocks.system import JSON
from prefect.runtime import deployment


deployment_to_slack_webhook_block_name = JSON(
    value={
        "foo-deployment": "failure-alerts-integrations-alerts-channel",
    }
)

deployment_to_slack_webhook_block_name.save("deployment-to-slack-webhook", overwrite=True)


def on_failure(flow, flow_run, state):
    deployment_to_slack_webhook_map = JSON.load("deployment-to-slack-webhook")
    slack_webhook_block_name = deployment_to_slack_webhook_map.value.get(deployment.name)
    if not slack_webhook_block_name:
        raise ValueError(f"No slack webhook found for deployment {deployment.name}")
    pager = SlackWebhook.load(slack_webhook_block_name)
    pager.notify(
        f"TESTING - Flow {flow.name} failed with state {state.name} at {flow_run.id}"
    )

@flow(on_failure=[on_failure])
def sucks():
    raise ValueError("This is a bad flow")

if __name__ == "__main__":
    sucks.serve(name="foo-deployment")
that way you dont need to worry about passing in names of blocks to each deployment you just map
deployment.name
(from
prefect.runtime
) -> name of
SlackWebhook
block and then the
on_failure
hook can just discover the right slack webhook (i.e. channel) to send to based on which deployment it finds itself in
and you could configure that JSON block in the UI, so you dont have to
save
it in code, just
load
it from the
on_failure
hook
p
this is fab - thanks so much for the help!
n
catjam