<@ULVA73B9P> I want to trigger deployment after my...
# marvin-in-the-wild
d
@Marvin I want to trigger deployment after my custom condition is met (I check whether a table in a database is updated today). How to do it using Automations available in Prefect 3.0?
m
Ah, the joy of triggering tasks based on database updates. I'm sure your database table is thrilled to be at the center of attention. Let's dive in. First, I need to know which version of Prefect you're using. Are you using Prefect 2.x or 3.x? Once you confirm, I'll search the appropriate documentation for how to set up Automations to trigger deployments based on custom conditions in Prefect 3.0.
😁 1
d
@Marvin Prefect 3.x
m
Alright, here’s the lowdown. Apparently, Automations are the Swiss Army knives of Prefect 3.x, allowing actions to be triggered by custom events or conditions in your flows. Here's a brief summary for your scenario: 1. Trigger Condition: This can be an event like a change in your database table. 2. Custom Event: You’d need to define a custom trigger event in Python to check if the table has been updated today. 3. Automation Setup: On the Automations page, you create a new automation with your custom trigger and define the action to execute the deployment. For more details on setting up these automations and triggers, you can visit the official Prefect Automations Documentation. Have fun automating the mundane tasks while your database tables bask in their glory. Source: - Trigger Actions on Events
d
@Marvin Could you provide code example?
m
Alright, now that GitHub didn't provide much help, let me provide you an example of how you could set this up. Let's assume you have a function that checks if the table has been updated today. You'd create a custom event to trigger the deployment based on this condition. ### Step 1: Define the Custom Event First, create a custom event in Python.
Copy code
python
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!
d
@Marvin Is it possible to combine in deployment both a schedule and a trigger? E.g. I want to run the deployment only on Monday not earlier than 11:00.
m
It appears that combining schedules and custom triggers in Prefect 3.0 is indeed possible, although it's a bit nuanced. ### Combining Schedule and Custom Trigger You can define a custom trigger and combine it with a schedule using a sequence of triggers. The setup would look like this: 1. Scheduled Deployment: Ensure that your deployment is scheduled to run on Mondays no earlier than 11:00 AM. 2. Custom Trigger: Define a custom trigger for your condition. Here's how you can create and combine these triggers:
Copy code
python
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 Triggers
n
marvin confabulated some syntax here
E.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"
d
I mean if the table is ready before 11:00, then deployment should wait 11:00; if at 11:00 it isn't ready, then run deployment as soon the table is ready. Now I achieve it with retries for a task that checks the table, but it seems that Automations provide more elegant way.
n
the most common way to use events here would be give the downstream deployment a
trigger
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 webhook
🤝 1
then you dont need a schedule