Needs Approval

Adding manual approval steps to Prefect flows

Jenny Grange
The Prefect Blog
Published in
4 min readSep 15, 2020

--

We’ve introduced some great new features to Prefect Cloud and Server in the last few weeks. This blog post guides you through one of those — the ability to make a task require manual approval before it runs.

Prefect’s mission to eliminate negative engineering doesn’t always involve new automations — sometimes it means introducing hooks for manual intervention as well! For example, imagine you have a workflow that needs monitoring and you don’t want it to run a certain task until you’ve checked the status of things. Or you want somebody else on your team to check your work before it runs. What do you do?

With Prefect you can use a manual_only trigger to configure your task to enter a Paused state and await your (or a team-mate’s) approval. If you’re using Prefect Cloud or the latest (0.13.1+) version of Prefect Server, you can then easily approve it from the UI (or our GraphQL API), and I’m going to show you how!

Writing Your Flow

Triggers are a feature in Prefect Core that let you set conditions for whether a task should or should not run. The default trigger rule is that a task can run only if its upstream dependencies have all run successfully (note that currently, all upstream dependencies must finish — whether successfully or not — before a task is even considered for running).

Most trigger functions decide whether to run based on the states of upstream tasks. The manual_only trigger is different in that it does not depend on what happened upstream and instead always sends the task into a Paused state.

You can see more about triggers in the Prefect docs but here’s a very simple example of a flow that uses a manual_only trigger:

Once you’ve written your code, you can test it locally using flow.run(). It will ask for approval in the terminal. Once your flow is ready, you can register and run your flow using a locally deployed Prefect Sever (0.13.1 and above) or Prefect Cloud.

Then what?

When you start this flow run, it will begin walking through the dependency graph and run with any tasks whose trigger conditions have been met. Once it reaches your task with a manual_only trigger, the task run will enter a Paused state and will stay in that state until you approve it (which places your run into a Resume state).

In the UI, approving your task is simply a matter of pressing the “Approve” button by your task in the flow run page:

or on the task run page:

The task will then enter a Resume state until it is picked up by your agent and run.

How do I know if a task needs approval?

If a task is waiting for approval, you will get a message in your notification inbox in the Prefect UI:

If you want to get external alerts, such as an email or a slack notification, you could add a state-handler to your task:

What about the other tasks?

Unless they are set as downstream dependencies of your task with a manual_only trigger, other tasks will run when you start the flow run. If you want more tasks to pause, you could add more than one manual_only trigger or, where appropriate, set the manual_only trigger task to be upstream of your other task(s):

We hope that manual_only triggers and the ability to approve those tasks in the UI makes your work easier and cuts down on your negative engineering.

If you’d like to be able to request approval in other ways or if there are other features you’d love to see in Prefect Server or Prefect Cloud, please send us your questions and feedback — we appreciate the opportunity to work with all of you!

Happy Engineering!

— The Prefect Team

--

--