Paige Fuller
03/19/2024, 10:52 AMNate
03/19/2024, 3:00 PMmultiple deployments reuse the same flow logicdo 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 goPaige Fuller
03/19/2024, 3:02 PMNate
03/19/2024, 3:03 PMtag
you pass into your flow? or how do you decidePaige Fuller
03/19/2024, 3:04 PMNate
03/19/2024, 3:05 PMPaige Fuller
03/19/2024, 3:06 PMNate
03/19/2024, 3:06 PMPaige Fuller
03/20/2024, 10:10 AMNate
03/20/2024, 1:55 PMPaige Fuller
03/20/2024, 5:36 PMNate
03/20/2024, 5:54 PMfrom 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")
Nate
03/20/2024, 5:56 PMdeployment.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 inNate
03/20/2024, 5:57 PMsave
it in code, just load
it from the on_failure
hookPaige Fuller
03/20/2024, 6:50 PMNate
03/20/2024, 7:09 PM