I can’t find a way to attach an hook to an asset m...
# ask-community
r
I can’t find a way to attach an hook to an asset materialisation. I’ve created the job for the asset with
define_asset_job
and then I inject the job in a
ScheduleDefinition
which I use in the repository. I’ve also found this but maybe there’s a workaround
o
hi @Riccardo Tesselli! do you mind sharing a bit more about what you'd like the hook to do? we do have a concept of an
@asset_sensor
, which will kick off some arbitrary code (or even another job) whenever an asset materialization event occurs for a given key.
r
I would like to have a failure hook which is triggered when the asset materialisation fails
o
right now, there's not built-in support for asset failure hooks, there is a concept of op failure hooks: https://docs.dagster.io/concepts/ops-jobs-graphs/op-hooks#op-hooks. so if you had a hook defined, you could do something like this:
Copy code
@op
def do_asset_computation(input):
    ...

my_asset = AssetsDefinition.from_op(
    do_asset_computation.with_hooks({some_hook_def}),
    keys_by_input_name={"input": AssetKey("input")},
    keys_by_output_name={"result": AssetKey("some_asset")},
)
which is pretty roundabout but should work.
an alternative solution might just be to do something like
Copy code
@asset
def my_asset():
    result = None
    try:
        # ... do stuff
        result = "something"
    finally:
        # things went as expected
        if result is not None:
            return result
        else:
            # ... do some error-related stuff
also, if you will only ever execute the asset via the specific job you've defined, you could just set up a run_status_sensor to alert if that job fails
r
@owen thanks!!!