Akash N
07/17/2025, 11:49 AMMarvin
07/17/2025, 11:49 AMMarvin
07/17/2025, 11:51 AMpython
from prefect.blocks.notifications import EmailNotification
email_block = EmailNotification(
email_server_credentials={
"username": "<mailto:your-email@example.com|your-email@example.com>",
"password": "your-password"
},
from_email="<mailto:your-email@example.com|your-email@example.com>"
)
email_block.save(name="email-notification", overwrite=True)
2. Then, you can create an automation with the following trigger configuration:
python
from prefect.server.schemas.automations import (
Automation,
EventTrigger,
SendNotification
)
trigger = EventTrigger(
name="Multiple Task Failures",
match={
"prefect.resource.type": "task",
"prefect.resource.state.name": "Failed"
},
threshold=5, # Number of failures
window_seconds=1800, # 30 minutes in seconds
)
action = SendNotification(
name="Send Email on Failures",
block_document="email-notification", # The name you saved your email block as
subject="Task Failure Alert",
body="Tasks in deployment have failed 5 times in the last 30 minutes."
)
automation = Automation(
name="Task Failure Email Alert",
trigger=trigger,
actions=[action]
)
3. Save and enable the automation:
python
from prefect.client import get_client
async def create_automation():
client = get_client()
await client.create_automation(automation)
# Run this in an async context
await create_automation()
Key points about this configuration:
- The trigger will fire when any task fails 5 times within a 30-minute window
- The window_seconds=1800
parameter sets the 30-minute time window (30 minutes × 60 seconds)
- The threshold=5
parameter sets the number of failures needed to trigger the notification
- The match
criteria specifies that we're looking for task failures specifically
You can also create automations through the Prefect UI, which might be easier for initial setup and testing.
Remember that you'll need to:
1. Have proper email server credentials configured
2. Create and save the email notification block before creating the automation
3. Ensure your Prefect server has network access to send emails
Would you like me to provide more details about any part of this setup, such as configuring the email server or customizing the notification message?