https://prefect.io logo
Title
s

Sylvain Hazard

11/23/2021, 10:45 AM
Hey ! Are tasks results available in flow-level state handlers ? I'd like to use those to generate some kind of report and send it to Slack.
a

Anna Geller

11/23/2021, 11:01 AM
@Sylvain Hazard did you know about the Artifacts API? It allows you to publish arbitrary markdown reports directly from your tasks. There is an open PR that documents this feature in in detail. Regarding state handler, you could do something as shown below, but I believe it would HAVE to be a task-level state handler because you need to know which task_slug or task_run_id you want to get the results from:
import json
import requests
import prefect
from prefect import task, Flow
from prefect.backend.flow_run import FlowRunView


def send_report_on_success(task, old_state, new_state):
    if new_state.is_successful():
        result = new_state.result
        <http://requests.post|requests.post>(url="webhook_url", data=json.dumps({"text": result}))
    return new_state


@task(state_handlers=[send_report_on_success])
def return_some_data():
    return "Some result"


with Flow(name="state-handler-demo-flow") as flow:
    result = return_some_data()
s

Sylvain Hazard

11/23/2021, 11:06 AM
I saw about the Artifacts API but since the link in the UI sent me to a 404 page, I thought it was a little bit too early to use it 😅 It looks like it might be what I'm looking for, thanks ! Is there a way to retrieve the artifacts one written ? e.g. one task writes a markdown report and another one reads it and sends it to Slack ?
a

Anna Geller

11/23/2021, 11:10 AM
What Artifacts API is using is just one (possibly large) string that will be later rendered in the UI. You can have one task that generates this markdown string and creating the artifact (
prefect.backend.artifacts.create_markdown_artifact(" # Header \n Hi Sylvain! ")
), but also returning it and passing to another task that may e.g. send it via Slack to someone.
s

Sylvain Hazard

11/23/2021, 11:16 AM
Alright, makes sense. Thanks !