<@U0667DNC02Y> Give me a sensor which start a run ...
# ask-ai
d
@Scout Give me a sensor which start a run if the asset with the name "test" is materialized
s
Certainly! Here's an example of an asset sensor that triggers a run when an asset with the key
test
is materialized:
Copy code
python from dagster import AssetKey, RunRequest, asset_sensor, SensorEvaluationContext  @asset_sensor(asset_key=AssetKey("test"), job=my_job) def my_asset_sensor(context: SensorEvaluationContext):     yield RunRequest(run_key=None)
In this example,
my_job
should be replaced with the actual job you want to trigger. The
RunRequest
object is yielded without a
run_key
or
run_config
, which means it will trigger a new run with the default configuration each time the sensor detects a new materialization for the asset with the key
test
. Please ensure that you have defined
my_job
elsewhere in your code, and it is the job you intend to run when the asset is materialized.
d
@Scout How do I orchestrate a graph after the assset with the name "trottel" is materialized
s
To orchestrate a graph after an asset with the name "trottel" is materialized, you can use an asset sensor similar to the one I provided earlier. The asset sensor will monitor for materializations of the asset with the specified key, and when it detects a new materialization, it will trigger the execution of a graph. Here's an example of how you might set up such an asset sensor:
Copy code
python from dagster import AssetKey, RunRequest, asset_sensor, SensorEvaluationContext  # Replace `my_graph` with the actual graph you want to orchestrate from my_project import my_graph  @asset_sensor(asset_key=AssetKey("trottel"), job=my_graph.to_job()) def trottel_asset_sensor(context: SensorEvaluationContext):     # You can access the materialization event and use it to customize the run request if needed     materialization_event = context.latest_materialization_records[0]      # Optionally, you can use the materialization event to pass specific configuration     # to the job being triggered, for example:     # run_config = {"solids": {"my_solid": {"config": {"param": materialization_event.dagster_event.event_specific_data}}}}      # Trigger the job without any custom run configuration     yield RunRequest(run_key=None)
In this example,
my_graph
should be replaced with the actual graph you want to orchestrate when the asset "trottel" is materialized. The
my_graph.to_job()
method converts the graph into a job, which is the executable unit in Dagster that can be triggered by the sensor. Please ensure that you have defined
my_graph
elsewhere in your code, and it represents the graph you intend to run. If you need to pass specific configuration to the job based on the materialization event, you can construct a
run_config
dictionary and pass it to the
RunRequest
.