https://prefect.io logo
d

Dekel R

11/20/2022, 1:54 PM
Another question regarding slack notifications. I saw this guide https://discourse.prefect.io/t/sending-notifications-in-cloud-1-0-using-automations-cloud-2-0-slack-webhook-blocks-and-notifications/1315 What about sending slack notifications on a local run? In Prefect 1, I added the “slack_webhook_url” to my local “/.prefect/config.toml” file - is there any solution for Prefect 2? Thanks.
1
t

Taylor Curran

11/21/2022, 12:46 AM
Hi Dekel, you should still be able to use the slack webhook block on local runs.
👀 1
gratitude thank you 1
I would create a slack webhook block and load (instantiate) the block in your flow code.
Copy code
from prefect import flow
from prefect.blocks.notifications import SlackWebhook

@flow
def my_flow():    
    task_result_1 = follows_large_computation.submit()
    if task_result_1.get_state().type != "COMPLETED":

        print("Task Get State != Completed")

        slack_webhook_block = SlackWebhook.load("prefect-notifications")
        slack_webhook_block.notify("Task Failure from Prefect!")
^something like this
👀 1
d

Dekel R

11/21/2022, 9:26 AM
Hey, thanks for the response! I do the same thing but inside a task - (the task is calling this function)
Copy code
def send_slack_alert(messege: str):
    slack_webhook_block = SlackWebhook.load("flows-status-slack-   notifications")
    slack_webhook_block.notify(messege)
And on a local run I get this -
Copy code
{ValueError}Unable to find block document named flows-status-slack-notifications for block type slack-webhook
See the attached block, I don’t get how this is suppose to work on local runs - this block is configured at our cloud account - the developer may be or may not be authenticated to our cloud account. This is why I’m asking for similar functionality such as configuring a “conf.toml” file on local runs.
t

Taylor Curran

11/21/2022, 2:11 PM
Hi Dekel, If you authenticate via the CLI, even for local runs, the flow will have access to blocks in cloud.
You could also define the blocks in python for local executions, however authenticating is recommended, since authenticating to the cloud still allows you to have fully local runs
d

Dekel R

11/21/2022, 6:20 PM
I see, Can you please provide a relevant link for this option? Both defining blocks for local execution and using blocks on a local run - I couldn’t find any reference. I think the local configuration is the better option - we have multiple developers, and they might be logged off. Thanks
t

Taylor Curran

11/21/2022, 11:05 PM
👀 1
d

Dekel R

12/08/2022, 10:00 AM
Hey @Taylor Curran - commenting about this issue again I Pre-configured slack blocks in 3 different envs - dev, staging and production. The developers in our team work on dev and should be able to run their flows locally without any authentication (authentication and deployments are managed by CI - the developer only pushed to git). So I’m looking for a solution similar to the one we had with Prefect 1 - configuring slack secrets locally so a “dev” run will work. I couldn’t find a solution in the link you attached - am I missing something?
t

Taylor Curran

12/08/2022, 3:51 PM
You could define the block in python locally and never save it to the workspace.
I’m not sure I fully understand what you are referring to. Do you want a certain slack webhook block value for when the flow executes in dev and then a different webhook value for when the flow executes in prod?
d

Dekel R

12/08/2022, 9:28 PM
No, let me clarify: I configured 3 slack webhooks blocks - 1 per environment (dev, stage and prod). When a flow is running as part of a Prefect deployment (scheduled by Prefect cloud) everything works fine cause the flow code can get the slack webhook (again - its saved as a block). Before deploying the flow we develop it on a local machine (the developer’s personal mac) - the developer isn’t authenticated to Prefect, so the slack webhook block is not accessible. In Prefect 1 we could save the dev slack webhook in “.prefect/config.toml” and then run the flow in “dev” mode locally, testing the usage of slack webhooks. How can we do it in Prefect 2? I hope it’s clearer now. Thanks
t

Taylor Curran

12/09/2022, 3:22 PM
Thanks for clarifying Dekel, that makes sense: For development that is not authenticated to any cloud workspace, you can spin up a local instance of orion and save blocks to the orion instance.
prefect orion start
I will make things easier if you define the slack webhook block in Python versus in the UI like shown below:
make_blocks.py
Copy code
from prefect.blocks.notifications import SlackWebhook

local_webhook = SlackWebhook(url=dev_slack_webhook_url)
local_webhook.save("my_local_webhook")
^Save to either a local instance of orion then execute the flow
d

Dekel R

12/09/2022, 4:03 PM
Hey, So I have to run a local Prefect instance to do that? I’m trying to avoid adding more steps for our developers since some of them are Data scientists, so the development process should be as simple and seamless as possible. The previous solution with Prefect 1 was ideal since it was a one-time configuration that made the slack functionality work locally without spawning a local Prefect instance (One of the things that made us pick Prefect over Airflow was the ability to test code locally without spawning a local docker/any instance of Prefect).
t

Taylor Curran

12/09/2022, 4:42 PM
I’m learning that you don’t need to start orion for this to work
You should just need to switch to the local profile, run the make_blocks.py and those blocks should be saved to their local profile indefinitely.
@Dekel R this should still be a one-time configuration. I was incorrect, you should not need to run a local Prefect instance to save a block locally.
d

Dekel R

12/12/2022, 4:17 PM
Hey, I found a solution for now using “prefects support” help -
Copy code
from src.configurations import ENV

def send_slack_alert(messege: str):
    try:
        if ENV != 'LOCAL':
            slack_webhook_block = SlackWebhook.load("flows-status-slack-notifications")
        else:
            slack_webhook_block = SlackWebhook(url=os.environ.get("SLACK_WEBHOOK_URL"))
        slack_webhook_block.notify(messege)
    except Exception as e:
        <http://logger.info|logger.info>("Local run couldn't get slack webhook")
Both DEV and LOCAL have the same configuration in our case. The DEV slack_webhook_url will get injected locally by using a .env file. I think this solution is the most effortless for our Data Science team since they only need to add the webhook to their .env file.
👍 1
4 Views