any way to programmatically kill a flow that is ru...
# ask-community
n
any way to programmatically kill a flow that is running for more than a certain period of time? I am aware of SLA automations but want to do it code way. Please suggest. Thanks!
v
Copy code
client = Client()

where = {
    'name': {'_like': 'flow_name_example%'},
    'status': {}
}

flow_run_query = {
    'query': {
        with_args('flow_run', {'where': where}): {
            'id': True,
        },
    },
}

result = client.graphql(flow_run_query)
flow_runs = result['data']['flow_run']

for flow_run in flow_runs:
    client.cancel_flow_run(flow_run.id)
just modify query to get flow that runs more than expected
n
can this be done from with in the flow itself?
or there has to be a poller doing this on a regular basis?
v
If you want to kill flow itself, probably it is better to use task timeout
n
we are using a version 0.14.10 which doesnt allow that option
n
we are using a version 0.14.10 which doesnt allow that option
how to get runtime of a flow?
use python current time and start time from flow_run?
v
It is code for 0.14.10
n
i tried timeout
but it gave option not availabl
so not using it
v
It is strange
in that case you can use Prefect Client
I used created as start_time field is null, I don't know why
n
`ok thanks! will try out and get back in case of any issues!
v
check Task class if timeout is there
it should be more elegant solution
n
ok sure
k
timeout is the more elegant solution, but not guaranteed when the flow is running somewhere else (Dask). Dask itself has no mechanism to cancel work already happening on the cluster. It’s very hard in Python to cancel a process happening, especially is the process is on another machine. you can also wrap the code Vadym proposed inside a state handler to cancel the Flow from within itself based on some condition of a task. There is no built-in Prefect Signal to end a run immediately
🙌 1
n
yes, i just tried it. The tasks wouldnt even start running with the timeout parameter. Working on scavenger script to kill flows using the logic mentioned by vadym above!
k
You can do:
Copy code
client.cancel_flow_run(prefect.context.flow_run_id)
n
out flows seem to be getting stuck in one task and not getting out
so not sure how to handle with state handler?
k
Ahh for that automation is really the best way because it’s driven by Cloud
n
yeah, there are like 150 flows!
🙂
hi, how to get the name of flow which is registered to a project using graphql?
name seems to be the run name - i want to use the name of the flow as it is registered
v
flow_run has flow, flow has name
Copy code
query {
      flow_run {
        id,
        created,
        flow {
          name
        }
      }
    }
n
ok..