<@ULVA73B9P> I have three deployments that run on ...
# ask-marvin
k
@Marvin I have three deployments that run on three separate work pools. I want to trigger a fourth deployment after the other three deployments finish their runs. How can I do this?
m
To trigger a fourth deployment after the completion of three other deployments, you can utilize Prefect's event-driven capabilities. Here's a concise approach to achieve this: 1. Event Triggers: Prefect allows you to trigger a downstream deployment when an upstream deployment completes by using event triggers. You can set up a trigger that listens for the completion event of your three initial deployments and then initiates the fourth deployment. 2. Automation with Prefect: Use the Prefect automations to set up this event-based workflow. You can define a trigger that listens for
prefect.flow-run.Completed
events from your three deployments and then initiates the fourth deployment. 3. Prefect Serve: Ensure that the
serve()
process is running to listen for scheduled deployment runs. This process will handle the event listening and triggering. For more details, you can refer to the following resources: - Prefect Automations and Triggers - Prefect Deployment Documentation This setup will allow you to automate the flow of your deployments seamlessly. If you have further questions or need more detailed guidance, feel free to ask.
k
@Marvin I'm setting up the automation through Prefect Cloud's UI. Which template should I use?
m
When setting up automation through Prefect Cloud's UI, you can use the automation feature to trigger actions based on events. Here's how you can proceed: 1. Create a New Automation: On the Automations page in Prefect Cloud, select the "+" icon to create a new automation. 2. Configure Trigger Conditions: Set up a trigger condition that listens for the completion of your three initial deployments. This can be done by selecting the appropriate event type, such as
prefect.flow-run.Completed
. 3. Define Actions: Specify the action to start the fourth deployment once the trigger condition is met. You can also add additional actions like sending notifications if needed. 4. Use Jinja2 Templates: If you need to define custom events or notifications, you can write templates using Jinja2. This allows you to customize the event name and resource labels. For more detailed guidance, refer to the Prefect Automations and Triggers documentation. This setup will allow you to automate the triggering of your fourth deployment seamlessly after the completion of the first three.
k
@Marvin Can you write the JSON I can use in the trigger for this automation?
m
To create the JSON for a trigger in Prefect Cloud automation, you'll want to define the trigger condition and the action. Here's a basic example of what the JSON might look like for triggering a deployment after three others complete:
Copy code
json
{
  "name": "Trigger Fourth Deployment",
  "description": "Triggers the fourth deployment after the first three complete.",
  "trigger": {
    "type": "AND",
    "conditions": [
      {
        "type": "flow_run_state",
        "state": "Completed",
        "flow_name": "FirstDeploymentFlow"
      },
      {
        "type": "flow_run_state",
        "state": "Completed",
        "flow_name": "SecondDeploymentFlow"
      },
      {
        "type": "flow_run_state",
        "state": "Completed",
        "flow_name": "ThirdDeploymentFlow"
      }
    ]
  },
  "actions": [
    {
      "type": "start_flow_run",
      "flow_name": "FourthDeploymentFlow"
    }
  ]
}
### Explanation: - Trigger Conditions: The
trigger
section uses an "AND" condition to ensure all three specified deployments have completed. - Actions: The
actions
section specifies starting the fourth deployment flow. Make sure to replace
"FirstDeploymentFlow"
,
"SecondDeploymentFlow"
,
"ThirdDeploymentFlow"
, and
"FourthDeploymentFlow"
with the actual names of your deployments. You can adapt this JSON to fit any additional requirements or specific configurations you might have in your Prefect Cloud setup.
k
@Marvin Explain what this trigger will do:
Copy code
{
  "type": "compound",
  "triggers": [
    {
      "type": "event",
      "match": {},
      "match_related": {},
      "after": [],
      "expect": [
        "prefect.flow-run.Completed"
      ],
      "for_each": [
        "prefect.flow-run.941f8c36-a650-4294-a8fc-9b927c89c8e3"
      ],
      "posture": "Reactive",
      "threshold": 1,
      "within": 0
    }
  ],
  "require": "all",
  "within": 900
}
m
This JSON configuration describes a trigger setup for a Prefect automation. Here's a breakdown of what it does: 1. Type:
"compound"
- This indicates that the trigger is a composite of multiple conditions or sub-triggers. However, in this case, there's only one sub-trigger specified. 2. Triggers: An array containing a single trigger configuration. - Type:
"event"
- This specifies that the trigger is based on an event occurring. - Match:
{}
- No specific criteria are set for matching events directly. - Match_related:
{}
- No criteria for matching related resources. - After:
[]
- No specified conditions that must occur before this trigger is activated. - Expect:
["prefect.flow-run.Completed"]
- The trigger expects an event of type
prefect.flow-run.Completed
. This means it is looking for the completion of a flow run. - For_each:
["prefect.flow-run.941f8c36-a650-4294-a8fc-9b927c89c8e3"]
- The trigger is specifically monitoring the completion of the flow run with ID
941f8c36-a650-4294-a8fc-9b927c89c8e3
. - Posture:
"Reactive"
- The trigger is set to react to the specified event when it occurs. - Threshold:
1
- The trigger will activate after the specified event has occurred once. - Within:
0
- The trigger will react immediately to the event occurrence. 3. Require:
"all"
- All specified conditions (in this case, just one) must be met for the trigger to activate. 4. Within:
900
- The trigger must activate within 900 seconds (15 minutes) of the specified conditions being met. ### Summary: This trigger is set to activate a specified action when a particular flow run (identified by its ID) completes. It requires the completion event to occur within 15 minutes of checking the conditions, and it will trigger immediately upon detecting this event.
k
@Marvin What if I change
for_each
to
["prefect.flow.810aba28-c79a-4526-8415-1e2edf40bd84"]
?
m
If you change the
for_each
field to
["prefect.flow.810aba28-c79a-4526-8415-1e2edf40bd84"]
, the trigger will now be set to monitor the completion of any flow run associated with the specific flow identified by the flow ID
810aba28-c79a-4526-8415-1e2edf40bd84
. ### Key Differences: - Scope: Previously, the trigger was monitoring a specific flow run (a single instance of a flow execution) with a unique run ID. Now, it will monitor all runs of the flow identified by the flow ID. - Behavior: The trigger will activate whenever any run of the specified flow completes, rather than waiting for a specific run to complete. This modification broadens the scope of the trigger from a single flow run to any run of the specified flow, allowing it to react to multiple instances of flow execution.
k
@Marvin Is it a problem if I don't specify anything for
match
or
after
?
m
Not specifying anything for
match
or
after
in your trigger configuration is not inherently a problem, but it does have implications on how your trigger behaves: 1. `match`: - Leaving this empty (
{}
) means that the trigger will not filter events based on specific attributes or criteria. It will react to any event that matches the broader conditions you've set (like the
expect
and
for_each
fields). - This is suitable if you want the trigger to activate for any occurrence of the specified event type without additional filtering. 2. `after`: - An empty
after
array (
[]
) indicates that there are no prerequisite conditions or events that must occur before this trigger can activate. - This means the trigger is ready to react immediately when the specified event occurs, without waiting for other conditions. ### Summary: - Flexibility: By not specifying
match
or
after
, your trigger is more flexible and will activate based on the core conditions you define (like
expect
and
for_each
). - Use Case: This setup is beneficial if you want a straightforward trigger that activates whenever the specified event occurs, without requiring complex dependencies or filters. If your use case requires more refined control over when the trigger should activate, you might consider adding conditions to
match
or dependencies to
after
. Otherwise, leaving them empty is perfectly valid for a simpler automation setup.
k
@Marvin Evaluate this trigger. I want it to trigger when any flow run completes across each of three deployments.
Copy code
{
  "type": "compound",
  "triggers": [
    {
      "type": "event",
      "match": {},
      "match_related": {},
      "after": [],
      "expect": [
        "prefect.flow-run.Completed"
      ],
      "for_each": [
        "prefect.flow.8eb80e02-0caa-4355-bf4a-8db868fc2eaf"
      ],
      "posture": "Reactive",
      "threshold": 1,
      "within": 0
    },
    {
      "type": "event",
      "match": {},
      "match_related": {},
      "after": [],
      "expect": [
        "prefect.flow-run.Completed"
      ],
      "for_each": [
        "prefect.flow.2b14fcbc-b1d6-4aa1-ac28-599621ee981c"
      ],
      "posture": "Reactive",
      "threshold": 1,
      "within": 0
    },
    {
      "type": "event",
      "match": {},
      "match_related": {},
      "after": [],
      "expect": [
        "prefect.flow-run.Completed"
      ],
      "for_each": [
        "prefect.flow.71ea212b-3495-4a2d-8d39-fb91a95eaab2"
      ],
      "posture": "Reactive",
      "threshold": 1,
      "within": 0
    }
  ],
  "require": "all",
  "within": 900
}
m
This trigger configuration is set up to activate when a flow run completes for each of the three specified deployments. Here's an evaluation of its components: 1. Type:
"compound"
- This indicates that the trigger is composed of multiple sub-triggers, each of which must be satisfied for the compound trigger to activate. 2. Triggers: Three separate event triggers are defined, each monitoring a different flow. - Expect: Each trigger expects the event
"prefect.flow-run.Completed"
, meaning it will activate when a flow run completes. - For_each: Each trigger is set to monitor a specific flow by its ID: -
"prefect.flow.8eb80e02-0caa-4355-bf4a-8db868fc2eaf"
-
"prefect.flow.2b14fcbc-b1d6-4aa1-ac28-599621ee981c"
-
"prefect.flow.71ea212b-3495-4a2d-8d39-fb91a95eaab2"
- Posture:
"Reactive"
- The triggers will react to the event as soon as it occurs. - Threshold:
1
- Each trigger will activate after detecting the specified event once. - Within:
0
- Each trigger will react immediately upon the event occurrence. 3. Require:
"all"
- This means all three triggers must be satisfied (i.e., a flow run must complete for each of the three specified flows) for the compound trigger to activate. 4. Within:
900
- The entire compound trigger must activate within 900 seconds (15 minutes) of starting to evaluate the conditions. ### Summary: - Functionality: This setup ensures that the trigger activates only when a flow run completes for each of the three specified deployments within a 15-minute window. - Use Case: It is well-suited for scenarios where you need to ensure that all specified flows have completed before taking a subsequent action. This setup should meet your requirement of triggering when any flow run completes across each of the three deployments.