Hi everyone. Is there a good way to gracefully ca...
# ask-community
d
Hi everyone. Is there a good way to gracefully cancel a flows next scheduled run? I have flows scheduled every 5 minutes and when I check a change in, I want the flow to finish the current run, but stop after that so I can execute a git pull and re-deploy the dask workers, etc. Is there a good way to do that, other than watching the session and killing the process when the current flow state is "waiting on next scheduled start time"?
....something that would "finish current run on keyboard interrupt" would even work in my case..
n
Hi @David N ! Are you using Prefect Server or Cloud? Re-registering the flow will always remove scheduled runs but allow any currently in the
Running
state to finish.
d
We're using server and not cloud.
n
Great! So this strategy should work: Whenever you make a change you could either: • Turn the schedule off • Pull your infra changes • Turn the schedule back on or • Pull your infra changes and, in concert, push a new version of the flow (by calling
flow.register()
again, no code changes needed)
d
I really appreciate the info. I'll try to go back and read the docs again, but I think we skip this register() step?
Copy code
with Flow('flow-name', schedule=schedules.IntervalSchedule(interval=timedelta(seconds=300))) as flow:
    t1=func()
    t2=func2(t1)
flow.run()
then we execute the python scipt, the process runs "forever", and kicks the flow off every 300 secs.
n
Ohh ok, I see now. You're just running straight from Prefect Core, you don't have the database and UI spun up with Prefect Server, am I understanding correctly?
d
sorry yes, not prefect server, I lead you wrong.
n
No worries! Give me a few to check if there's a best-practices way to accomplish that with Core-only
👍 1
j
AFAICT there isn't a good way to do this builtin to core. You could hack this together with a custom schedule though (probably a
filter
or
clock
, see https://docs.prefect.io/core/concepts/schedules.html#complex-schedules). You'd want the schedule to respond to some external event (perhaps an os signal, perhaps it starts an http server and waits for a ping, perhaps it watches a file on disk for changes, up to you). When notified, it'd filter any newly scheduled events. Something like (pseudocode):
Copy code
def myfilter(event):
    if shouldnt_schedule_more_runs():
        return False
    return True

schedule = Schedule(..., filters=[myfilter])
upvote 2
👍 1
d
Damn, thats brilliant.
Thanks so much, I so appreciate the help from both of you