Will Sargent
07/17/2025, 2:36 PMMarvin
07/17/2025, 2:36 PMMarvin
07/17/2025, 2:37 PMpython
from prefect import flow
from prefect.events import emit_event
from datetime import datetime
@flow
def notification_flow(message: str):
# Emit an event that can trigger automations
event = emit_event(
event="notification.requested", # Event name - use a descriptive name
resource={
"name": "notification-system", # Identifier for the resource
"type": "notification" # Type of resource
},
payload={
"message": message,
"timestamp": datetime.utcnow().isoformat()
}
)
return event
# Example usage
if __name__ == "__main__":
notification_flow("This is a test notification")
To set up the automations to handle this event:
1. In Prefect Cloud, navigate to the Automations page
2. Create a new automation
3. Set the trigger to watch for events where:
- Event name equals "notification.requested"
4. Add actions for:
- Email notification
- ntfy notification
A few important notes:
- The event name (notification.requested
in this example) can be any string you choose, but it's good practice to use dot-notation to create a hierarchy
- The resource
dictionary helps identify the source/context of the event
- The payload
can contain any JSON-serializable data you want to pass to your automations
- Automations are configured in Prefect Cloud and can respond to these events with various actions
For ntfy specifically, you'll need to configure the ntfy block in Prefect Cloud with your ntfy topic URL. The same goes for email configuration - you'll need to set up the email block with your SMTP settings.
Would you like me to provide more specific details about setting up the automation rules in Prefect Cloud, or would you like to see examples of other types of events you could emit?Will Sargent
07/17/2025, 2:40 PMMarvin
07/17/2025, 2:40 PMMarvin
07/17/2025, 2:41 PMpython
from prefect import flow
from prefect.events import emit_event
from datetime import datetime, timedelta
from prefect.deployments import Deployment
from prefect.server.schemas.schedules import IntervalSchedule
@flow
def monitoring_flow(check_name: str, threshold: float = 0.9):
# Simulate some monitoring check
current_value = 0.95 # This would be your actual monitoring logic
if current_value > threshold:
# Emit an event when threshold is exceeded
emit_event(
event="monitoring.threshold.exceeded",
resource={
"name": check_name,
"type": "monitoring-check"
},
payload={
"check_name": check_name,
"current_value": current_value,
"threshold": threshold,
"timestamp": datetime.utcnow().isoformat()
}
)
return current_value
# Deploy the flow with a schedule
if __name__ == "__main__":
deployment = Deployment.build_from_flow(
flow=monitoring_flow,
name="scheduled-monitoring",
schedule=IntervalSchedule(interval=timedelta(minutes=5)),
parameters={"check_name": "system-health", "threshold": 0.9}
)
deployment.apply()
To set this up in your environment:
1. First, deploy the flow:
bash
prefect deploy
2. In Prefect Cloud, create an automation:
- Trigger: When an event with name "monitoring.threshold.exceeded" occurs
- Actions:
- Send an email notification
- Send an ntfy notification
- Optionally trigger another flow
3. Start a worker to handle the scheduled runs:
bash
prefect worker start -p your-work-pool
This pattern is particularly useful for:
- System monitoring where you want regular checks with notification capabilities
- Scheduled data quality checks that need to alert on failures
- Regular health checks that should trigger notifications or remediation flows
- Batch processing jobs that need to notify on completion or errors
The advantage of using events in this way is that:
1. Your flow remains focused on its core logic
2. The notification/action logic is handled separately through automations
3. You can easily add or modify reactions to events without changing the flow code
4. Multiple different automations can react to the same event
While I couldn't find a specific demo project in the official documentation, this pattern is commonly used in monitoring and alerting scenarios. The Prefect team has mentioned this pattern in their blog posts and documentation as a recommended approach for decoupling monitoring logic from notification handling.
Would you like me to expand on any particular aspect of this pattern, such as:
1. More complex event payload structures?
2. Setting up specific types of automations?
3. Adding additional conditions or triggers to the events?Will Sargent
07/17/2025, 2:43 PMMarvin
07/17/2025, 2:43 PMMarvin
07/17/2025, 2:44 PMWill Sargent
07/17/2025, 2:45 PMMarvin
07/17/2025, 2:45 PMMarvin
07/17/2025, 2:45 PM