Introducing Post Hooks
fal post hooks let you define Python scripts that don't need to write to the data warehouse.
Yesterday we introduced fal Python Data Models, which are much like regular dbt models but with Python contents. Users are encouraged to use Python models if their scripts need to write to their data warehouse. Because of this, each Python model requires calling the write_to_model
magic function. But what if a user wants to run a script that shouldn’t write to the data warehouse? For this purpose we’re releasing Post Hooks.
Say we want to send a Slack message right after we calculate a dbt model. We can have Python script send_slack_message.py
that gets model status and sends a message:
# send_slack_message.py
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
CHANNEL_ID = os.getenv("SLACK_BOT_CHANNEL")
SLACK_TOKEN = os.getenv("SLACK_BOT_TOKEN")
client = WebClient(token=SLACK_TOKEN)
message_text = f"Model: {context.current_model.name}. Status: {context.current_model.status}."
try:
response = client.chat_postMessage(
channel=CHANNEL_ID,
text=message_text
)
except SlackApiError as e:
# You will get a SlackApiError if "ok" is False
assert e.response["error"]
(for more information about making this script work with Slack, check out this blog post)
We turn this script into a post hook in our schema.yml
:
models:
- name: my_target_model
meta:
fal:
post-hook:
- send_slack_message.py
Now, running fal flow run
will run send_slack_message.py
after my_target_model
.
Overall, post hooks are very similar to the regular fal scripts, with one big exception: you’re not allowed to use write_to_model
or write_to_source
functions in these scripts.
In the future we will be transitioning away from the old fal "after" scripts and recommend using Python models and post hooks. This way, scripts that have to write to the data warehouse should be formatted as Python models, whereas everything else can be a post hook.
Feedback and comments are always welcome on our Discord server. You can also check out our Github repository, to give us a star or raise an issue.