3

Here is my dead simple flow:

from prefect import flow
import datetime



@flow
def firstflow(inreq):
  retval={}
  retval['type']=str(type(retval))
  retval['datetime']=str(datetime.datetime.now())
  print(retval)

  return retval

I run prefect orion and prefect agent.

Make a trigger using web ui (deployments run) ... the agent succesfully pull and do the job.

My question is how to do the trigger using just curl? Note : I already read http://127.0.0.1:4200/docs. but my lame brain couldn't find how to do it.

note:

  1. Lets say my flow id is : 7ca8a456-94d7-4aa1-80b9-64894fdca93b
  2. Parameters I want to be processed is {'msg':'Hello world'}

blindly Tried with

curl -X POST -H 'Content-Type: application/json' http://127.0.0.1:4200/api/flow_runs \
-d '{"flow_id": "7ca8a456-94d7-4aa1-80b9-64894fdca93b", "parameters": {"msg": "Hello World"}, "tags": ["test"]}'

but prefect orion say

INFO:     127.0.0.1:53482 - "POST /flow_runs HTTP/1.1" 307 Temporary Redirect

Sincerely

-bino-

Bino Oetomo
  • 571
  • 1
  • 10
  • 23

1 Answers1

1

It's certainly possible to do it via curl but it might be painful especially if your flow has parameters. There's much easier way to trigger a flow that will be tracked by the backend API - run the flow Python script and it will have exactly the same effect. This is because the (ephemeral) backend API of Prefect 2.0 is always active in the background and all flow runs, even those started from a terminal, are tracked in the backend.

Regarding curl, it looks like you are missing the trailing slash after flow_runs. Changing your command to this one should work:

curl -X POST -H 'Content-Type: application/json' http://127.0.0.1:4200/api/flow_runs/ \
-d '{"flow_id": "7ca8a456-94d7-4aa1-80b9-64894fdca93b", "parameters": {"msg": "Hello World"}, "tags": ["test"]}'

The route which might be more helpful, though, is this one - it will create a flow run from a deployment and set it into a scheduled state - the default state is pending, which would cause the flow run to be stuck. This should work directly:

curl -X POST -H 'Content-Type: application/json' \
http://127.0.0.1:4200/api/deployments/your-uuid/create_flow_run \
-d '{"name": "curl", "state": {"type": "SCHEDULED"}}'
Anna Geller
  • 1,653
  • 7
  • 10
  • I really appreciate your help. But how to pass parameters to that 2nd curl statements? I tried --> curl -v -X POST -H 'Content-Type: application/json' http://127.0.0.1:4200/api/deployments/dd0561c5-d457-4608-8af5-11ec80927ef6/create_flow_run -d '{"parameters":{"name":"John Doe"}, "state": {"type": "SCHEDULED"}}' but my flow did not receive that parameters. – Bino Oetomo Aug 17 '22 at 00:13