Dzmitry-S
08/30/2024, 1:37 PMMarvin
08/30/2024, 1:37 PMDzmitry-S
08/30/2024, 1:38 PMMarvin
08/30/2024, 1:38 PMDzmitry-S
08/30/2024, 1:39 PMMarvin
08/30/2024, 1:39 PMpython
from prefect import flow, task
from prefect.events import Event
# Mock function to check database table update
def check_table_updated():
# Your logic to check if the database table is updated today
return True # Suppose it returns True if updated, False otherwise
@task
def trigger_event_if_table_updated():
if check_table_updated():
Event('database.table.updated').emit()
@flow
def daily_check_flow():
trigger_event_if_table_updated()
### Step 2: Create the Automation in Prefect
Now, head over to the Prefect Cloud UI and set up the automation.
1. Create Automation: Go to the Automations page and create a new automation.
2. Set Trigger: Use the custom event database.table.updated
as the trigger.
3. Define Action: Set the action to trigger the specific deployment you want to run.
This way, your deployment will be triggered every time the specified condition (in this case, the database table being updated today) is met.
For more comprehensive details and customization, I suggest reviewing the official Prefect Automations Documentation.
That should transform your mundane table checks into more exciting automated actions. Enjoy!Dzmitry-S
08/30/2024, 1:44 PMMarvin
08/30/2024, 1:44 PMpython
from prefect import flow, task
from prefect.events import Event
# Mock function to check database table update
def check_table_updated():
# Your logic to check if the database table is updated today
return True # Suppose it returns True if updated, False otherwise
@task
def trigger_event_if_table_updated():
if check_table_updated():
Event('database.table.updated').emit()
@flow
def daily_check_flow():
trigger_event_if_table_updated()
from prefect.deployments import Deployment
from prefect.schedules import IntervalSchedule
from datetime import timedelta, datetime
# Define a schedule to run every week on Monday at 11:00 AM
schedule = IntervalSchedule(
interval=timedelta(weeks=1),
start_time=datetime.combine(datetime.today(), datetime.min.time()) + timedelta(
days=(0 - datetime.today().weekday()) % 7, # Move to next Monday
hours=11 # 11:00 AM
)
)
# Setup Deployment with schedule and custom triggers
deployment = Deployment(
flow=daily_check_flow,
name="weekly_check_with_custom_trigger",
schedule=schedule,
custom_triggers=[{
"type": "event",
"posture": "Reactive",
"expect": ["database.table.updated"]
}]
)
deployment.apply()
Here's what this does:
- Schedule: Ensures that daily_check_flow
runs every Monday at or after 11:00 AM.
- Custom Trigger: Sets up an event to check if the database table has been updated.
- Deployment: Combines both the schedule and the custom trigger to control the execution.
For more comprehensive details, you can review the official Custom Triggers Documentation.
Combining schedules and triggers can give you granular control over when and under what conditions your automated tasks should be executed. Have fun scheduling and triggering away!
Sources:
- Custom TriggersNate
08/30/2024, 6:12 PME.g. I want to run the deployment only on Monday not earlier than 11:00.i'd recommend a cron schedule that just runs weekly at 11, or do you mean something nuanced by "not earlier than 11"
Dzmitry-S
08/30/2024, 6:17 PMNate
08/30/2024, 6:20 PMtrigger
that will expect
table.ready
events and match
on your prefect.deployment.name
or something
you'd just have to find a way to emit the table.ready
event using python or a webhookNate
08/30/2024, 6:21 PM