Nick McGoye
05/21/2024, 3:27 PMMarvin
05/21/2024, 3:28 PMpython
import requests
api_url = "<https://api.prefect.io/{workspace_id}/automations/>"
data = {
"name": "Event Driven Redeploy",
"description": "Programmatically created an automation to redeploy a flow based on an event",
"enabled": "true",
"trigger": {
"after": ["prefect.flow-run.Running"],
"expect": ["prefect.flow-run.Completed"],
"for_each": ["prefect.resource.id"],
"posture": "Proactive",
"threshold": 1,
"within": "60" # time interval to check for the event
},
"actions": [
{
"type": "run-deployment",
"source": "selected",
"deployment_id": "YOUR-DEPLOYMENT-ID",
"parameters": {} # any parameters you want to pass to the deployment
}
],
"owner_resource": "string"
}
headers = {"Authorization": f"Bearer {PREFECT_API_KEY}"}
response = <http://requests.post|requests.post>(api_url, headers=headers, json=data)
print(response.json())
Replace the "YOUR-DEPLOYMENT-ID"
, "prefect.flow-run.Running"
, "prefect.flow-run.Completed"
, and time within accordingly with your specific use case. For authentication, use your PREFECT_API_KEY
. This code will create an automation that checks if a flow run has completed within a certain time frame.
Additionally, you may write Python code to check the results from an API request and then decide whether to initiate a Prefect flow run depending on the token expiry information. Prefect doesn't have built-in mechanisms for these kinds of long-interval checks, so you might need to use an external scheduler or a continuously running process that periodically checks the token's status.
Also, note the Async Basic Example which showcases the structure of how your flow might look with async capabilities, should your API interaction require it. However, it does not specifically detail the triggering based on an API request.
Remember to tailor your implementation to your specific requirements, considering the different intervals at which you'll need to renew the token.Bianca Hoch
05/21/2024, 3:30 PMschedule_time
to schedule a flow run (which you can set depending on the token expiry date)Nick McGoye
05/21/2024, 3:33 PMMarvin
05/21/2024, 3:33 PM