Hello all, I am attempting to set up Slack Notific...
# prefect-community
m
Hello all, I am attempting to set up Slack Notifications for my flow and am following https://docs.prefect.io/core/advanced_tutorials/slack-notifications.html#installation-instructions. I have successfully installed the prefect app into my workspace and have been able to test the connection with success. However, I am unable to have it output information based on my actual flow. I receive the following error:
Copy code
Exception raised while calling state handlers: ClientError('400 Client Error: Bad Request for url: <http://host.docker.internal:4200/graphql>\n\nThe following error messages were provided by the GraphQL server:\n\n GRAPHQL_VALIDATION_FAILED: Cannot query field "secret_value" on type "Query".\n\nThe GraphQL query was:\n\n query($name: String!) {\n secret_value(name: $name)\n }\n\nThe passed variables were:\n\n {"name": "SLACK_WEBHOOK_URL"}\n')
what am I doing wrong? I have @task(state_handlers=[slack_notifier]) set for the task I am testing out the notification for.
k
Hey @Matthew Blau, Did you provide your
SLACK_WEBHOOK_URL
by some means, perhaps as a secret in your config.toml file? Just checking here first. If this isn’t the issue, then there may be some version mismatch - so I’d recommend updating to the latest version of Prefect or at least ensuring the flow you are submitting is a lesser version than your Server and Agent.
m
@Kyle Moon-Wright I have the webhook url set in the config.toml, yes. Would I need to restart prefect for it to be parsed, or will it parse it without needing to restart prefect?
k
Hmm, you may need to restart your Server instance, but I’m not sure - let me check. -> I believe updating your config.toml file for values like this shouldn’t necessitate a restart, but a restart could help as Server is a bit of a different beast.
m
@Kyle Moon-Wright I have restarted the server as well as adjusted the Slack Cloud hook to send a message on every status change. In Slack I get a message upon flow run scheduled, submitted for execution, running, and failed. Failed gives me the error message of
Copy code
[1 Feb 2021 3:21pm]: Exception raised while calling state handlers: ClientError('400 Client Error: Bad Request for url: <http://host.docker.internal:4200/graphql>\n\nThe following error messages were provided by the GraphQL server:\n\n GRAPHQL_VALIDATION_FAILED: Cannot query field "secret_value" on type "Query".\n\nThe GraphQL query was:\n\n query($name: String!) {\n secret_value(name: $name)\n }\n\nThe passed variables were:\n\n {"name": "SLACK_WEBHOOK_URL"}\n')
So it would appear that something is working, but is failing further down the chain of execution. I have the Slack Cloudhook set in the UI
Removing state_handler=[slack_notifier] from the @task decoration yields similar results, but this time the error in the logs is the error I expect it to be, but slack still gets a notification of "Some reference tasks failed"
I assume that I would need to do something more in depth than this to get the error message from the logs to appear in the Slack message, yes? And if so, where can I read for an example of this?
a
You can adapt the handler code from the prefect github repo to do things a little differently.
k
Yep agreed, check out this example of a custom
state_handler
using Slack rather than the
slack_notifier
.
m
I have adjusted the state handler to be
Copy code
def post_to_slack(task, old_state, new_state):
    my_secret = Secret("SLACK_WEBHOOK_URL").get()
    if new_state.is_retrying():
        msg = "Task {0} failed and is retrying at {1}".format(task, new_state.start_time)

        # replace URL with your Slack webhook URL
        <http://requests.post|requests.post>(my_secret, json={"text": msg})
    elif new_state.is_failed():
        msg = "Task {0} failed".format(task)

     # replace URL with your Slack webhook URL
        <http://requests.post|requests.post>(my_secret, json={"text": msg})

    return new_state
following that link you provided @Kyle Moon-Wright and I am still experiencing the error in my initial message. Not completely sure what I am doing wrong at present