<@ULVA73B9P> I'm trying to setup a notification sy...
# ask-marvin
m
@Marvin I'm trying to setup a notification system in Prefect 3.2.8 using a custom webhook block. The block has a Webhook URL (let's say myurl/notify) where I accept POST requests like myurl/notify?run_id:x and I've set into the block the field Query Params {"run_id":"{{ flow_run.id }}"} however, when sending the notification I get the following error: 095750.753 | WARNING | prefect.server.events.actions - Action failed: 'The notification block was invalid: KeyError("params/Placeholder(full_match=\'{{ flow_run.id }}\', name=\'flow_run.id\', type=<PlaceholderType.STANDARD: \'standard\'>)")'
m
It seems you're encountering an issue with using placeholders in your webhook block's query parameters in Prefect 3.2.8. The error indicates that the placeholder
{{ flow_run.id }}
is not being resolved correctly, leading to a
KeyError
. Here's a concise explanation of the problem and potential solutions: 1. Placeholder Usage: Prefect 3.x uses placeholders for dynamic values, but there might be a mismatch in how placeholders are being resolved in your webhook configuration. The error suggests that
flow_run.id
is not being recognized as a valid placeholder. 2. Webhook Configuration: Ensure that the placeholder syntax is supported in the context you are using it. You might need to verify if the webhook block supports dynamic placeholders in query parameters as expected. 3. Common Gotchas: Prefect 3.x has several changes and gotchas compared to previous versions. For example, the use of placeholders and how data is passed around might differ. Ensure that your configuration aligns with the latest practices in Prefect 3.x. To resolve the issue, you might want to: - Double-check the configuration of your webhook block and ensure that placeholders are used correctly. - Consider using Prefect's automation or event system to trigger webhooks, which might offer more robust handling of dynamic values. - Refer to the Prefect 3.x documentation on webhook triggers for more details on how to properly configure and use webhooks. If the issue persists, you may need to explore alternative ways to pass dynamic data, such as using Prefect's automation features to handle such scenarios.
m
@Marvin can you guide me into a basic example in which I receive notification from prefect on my custom webhook when a flow run is completed?
m
To set up a basic notification system in Prefect 3.2.8 using a custom webhook when a flow run is completed, follow these steps: 1. Create a Flow: First, define your flow using Prefect's
@flow
decorator. 2. Define a Webhook: You need to create a webhook that will be triggered upon the completion of the flow. This can be done through the Prefect Cloud UI or CLI. The webhook will have a unique URL where it will receive POST requests. 3. Use
on_completion
Hook
: Utilize the
on_completion
hook in your flow to trigger the webhook. This hook allows you to specify a function that runs when the flow completes. 4. Send Notification: In the
on_completion
function, send a POST request to your custom webhook URL. Here is a simple example:
Copy code
python
from prefect import flow, task, get_run_logger
import requests

@task
def my_task():
    logger = get_run_logger()
    <http://logger.info|logger.info>("Executing task...")
    return "Task completed!"

@flow
def my_flow():
    result = my_task()
    return result

# Define a function to be called on flow completion
def notify_on_completion(flow_run):
    webhook_url = "<https://myurl/notify>"
    payload = {"run_id": flow_run.id}
    response = <http://requests.post|requests.post>(webhook_url, json=payload)
    if response.status_code == 200:
        print("Notification sent successfully.")
    else:
        print("Failed to send notification.")

# Attach the on_completion hook
my_flow.on_completion(notify_on_completion)

# Execute the flow
my_flow()
### Key Points: - Flow Definition: Use the
@flow
decorator to define your flow. - Webhook URL: Replace
<https://myurl/notify>
with your actual webhook URL. - Payload: Customize the payload as needed. Here, it's a simple dictionary with the
run_id
. - Error Handling: Ensure to handle errors in the POST request appropriately. For more details, refer to the Prefect documentation on webhook triggers.
m
@Marvin I want to setup the notification system from my prefect server GUI, not in a programmatic way
m
To set up a notification system using a custom webhook through the Prefect Server GUI, follow these steps: 1. Access Prefect Server: Log in to your Prefect Server dashboard. 2. Navigate to Automations: - Go to the "Automations" section in the dashboard. This is where you can configure triggers and actions for notifications. 3. Create a New Automation: - Click on "Add Automation" to create a new automation rule. - Choose a trigger for your automation. For a flow run completion, you might select a trigger based on the flow run state, such as "Completed". 4. Configure the Trigger: - Specify the conditions under which the trigger should activate. For example, you can trigger an action when a flow run enters the "Completed" state. 5. Add an Action: - After setting up the trigger, define the action that should occur. In this case, the action will be sending a notification to your custom webhook. - Select "Send a Webhook" as the action type and enter the webhook URL (e.g., `https://myurl/notify`). 6. Set Webhook Parameters: - Configure any necessary parameters or headers for the webhook request. If you need to include dynamic data like
flow_run.id
, use the appropriate templating syntax supported by Prefect. 7. Save the Automation: - Review your settings and save the automation. It will now automatically send notifications to your webhook whenever the specified trigger conditions are met. For more detailed guidance, you can refer to the Prefect documentation on setting up automations which provides step-by-step instructions on using the Prefect UI for such configurations.