<@ULVA73B9P> How can I trigger dependent flow runs...
# ask-marvin
a
@Marvin How can I trigger dependent flow runs after a flow run has complete in Prefect 2.X
m
Oh, you're just itching to make one flow wait obediently for another to finish before it runs, aren't you? How thrilling. Let me find out how you can make your precious dependent flow runs happen. First, which version of Prefect are you using? 2.x or 3.x?
a
@Marvin 2.x
m
Oh, the joy of making one thing wait for another. In Prefect 2.x, you can use triggers to set up dependent flows. Here’s the general idea: 1. Automations: You can set up compound triggers to wait for multiple events before running a deployment.
Copy code
json
    {
        "type": "compound",
        "require": "all",
        "within": 3600,
        "triggers": [
            {
                "type": "event",
                "posture": "Reactive",
                "expect": ["prefect.block.remote-file-system.write_path.called"],
                "match_related": {
                    "prefect.resource.name": "parent-flow-1",
                    "prefect.resource.role": "flow"
                }
            },
            {
                "type": "event",
                "posture": "Reactive",
                "expect": ["prefect.block.remote-file-system.write_path.called"],
                "match_related": {
                    "prefect.resource.name": "parent-flow-2",
                    "prefect.resource.role": "flow"
                }
            }
        ]
    }
2. Scheduling: Prefect's scheduler can be configured to run flows based on time or events, which can indirectly help with dependency control. For more details, you can explore: - Automations: Automations Documentation - Schedules: Schedules Documentation Isn't it thrilling to be a master scheduler?
a
@Marvin how can I setup a flow to start running after another flow has succeeded in Python
m
Oh, you want to build a whole dependency chain in Python code? Fascinating. Let's figure out how to make one flow obediently wait for another to succeed before it starts in Prefect 2.x. Just what every code needs, more dependencies. Let's dig deeper, shall we? I'll search for a more specific example in the Prefect 2.x docs for running dependent flows. Ah, dependency chains in Python—so exciting. Here's how you can set up a flow to start running after another flow has successfully completed in Prefect 2.x. ### Using Event-Driven Automations You can set up Prefect to trigger flow runs based on events, like the successful completion of another flow. While doing this directly in Python code is not always straightforward, here's an example using automations:
Copy code
json
{
    "type": "event",
    "posture": "Reactive",
    "expect": ["prefect.flow-run.completed"],
    "match_related": {
        "prefect.resource.name": "parent-flow",
        "prefect.resource.role": "flow"
    }
}
### Using the Prefect Client in Python If you're adamant about doing this in Python code: 1. Run your primary flow and then use the
create_flow_run
method to launch the dependent flow after checking the state of the first flow. 2. Here's a simple example to illustrate the point:
Copy code
python
from prefect import flow, task, get_client
from prefect.states import Completed

@task
def task1():
    print("Task 1 complete.")

@task
def task2():
    print("Task 2 complete.")

@flow
def parent_flow():
    task1()

@flow
def child_flow():
    task2()

if __name__ == "__main__":
    # Trigger the parent flow
    parent_flow_result = parent_flow(return_state=True)

    # Check if parent flow succeeded
    if parent_flow_result.is_completed():
        client = get_client()
        client.create_flow_run("child_flow")
This is a simple representation. For a robust setup, you'd naturally add more error handling and client configuration. ### In Short - Automations: Use Prefect Automations to set up triggers based on events. - Direct Code: Use Prefect Client to monitor and trigger flows programmatically. For further details, you can check the official Automations Documentation and the Client Guide. Enjoy your dependency chains. They're like the gift that keeps on giving.
a
@Marvin how can I use Automations in Prefect cloud to trigger specific deployments when a flow run enters a completed state
m
Oh wonderful, more orchestration. Let's make those flows jump through hoops using Prefect Cloud's automations to trigger specific deployments upon flow completion. To set up automations in Prefect Cloud, you can create a custom automation rule based on the events emitted by your flow runs. Follow these steps: 1. Navigate to Automations: Go to the Automations tab in the Prefect Cloud UI. 2. Create a New Automation: Click on 'New Automation' to create an automation rule. 3. Define the Trigger: - Choose
Event
as the trigger type. - Set the trigger to react to the event
prefect.flow-run.completed
. 4. Set Up Conditions: - You can set conditions to match specific flows, for example:
Copy code
json
     {
        "type": "event",
        "posture": "Reactive",
        "expect": ["prefect.flow-run.completed"],
        "match_related": {
            "prefect.resource.name": "your-source-flow-name",
            "prefect.resource.role": "flow"
        }
     }
5. Define the Action: - Choose
Run Deployment
as the action type. - Specify the deployment you want to run when the flow completes. I'll search the Prefect 2.x docs for more detailed steps. Oh, Prefect Cloud automations—just the kind of fun that everyone looks forward to. Here's how you can set it up to trigger a specific deployment when a flow run enters a completed state. ### Steps to Set Up Automations in Prefect Cloud 1. Go to Automations: Navigate to the Automations tab in the Prefect Cloud UI. (You can only access this feature in Prefect Cloud). 2. Create a New Automation: Click on 'New Automation' to start creating a new rule. 3. Define the Trigger: - Choose
Event
as the trigger type. - Set the trigger to fire on the event
prefect.flow-run.completed
. 4. Set Up Conditions: - Add conditions to match the specific flow you care about:
Copy code
json
     {
         "type": "event",
         "posture": "Reactive",
         "expect": ["prefect.flow-run.completed"],
         "match_related": {
             "prefect.resource.name": "your-source-flow-name",
             "prefect.resource.role": "flow"
         }
     }
5. Define the Action: - Choose
Run Deployment
as the action type. - Specify the exact deployment you wish to run upon the successful completion of your flow. ### Example JSON for Automation Rule Below is an example JSON payload for setting up such an automation:
Copy code
json
{
    "name": "Trigger Deployment on Flow Completion",
    "type": "event",
    "posture": "Reactive",
    "expect": "prefect.flow-run.completed",
    "match_related": {
        "prefect.resource.name": "source-flow",
        "prefect.resource.role": "flow"
    },
    "actions": [
        {
            "type": "run_deployment",
            "parameters": {
                "deployment_id": "your-deployment-id"
            }
        }
    ]
}
### Resources for More Detailed Steps - Automations Documentation - Automations in Prefect Cloud With these steps, you should be able to set up those delightful automations and sit back while they handle the dependency chains for you. Enjoy the orchestration.
I will work on adding some docs on this tldr whether in yaml or python you can define triggers on your deployment that match on
prefect.flow-run.Completed
events from the upstream deployment
a
@Nate do triggers in prefect yaml apply to version 2.x?
n
both 2.x (most newer versions of 2.x anyways) and 3.x
i shared the 3.x docs bc imo they're easier to read, but the content on triggers should be consistent between versions
a
ah got it, thanks for clarifying!