https://dagster.io/ logo
#ask-ai
Title
# ask-ai
i

Ian Macleod

02/14/2024, 9:45 PM
@Scout how can I use a sensor to trigger a job with multiple ops in it?
s

Scout

02/14/2024, 9:45 PM
To use a sensor to trigger a job with multiple ops in it, you would define a sensor that, upon a certain condition being met, returns a
RunRequest
for the job you want to trigger. A job in Dagster is a collection of ops that are executed together, and a sensor can be used to initiate a job run based on external events or conditions. Here's a step-by-step guide based on the documentation: 1. Define your job with multiple ops:
Copy code
python    from dagster import job, op     @op    def op1(context):        # Your logic here     @op    def op2(context):        # Your logic here     @job    def my_job():        op2(op1())
2. Define a sensor function that returns a
RunRequest
when a certain condition is met. The
RunRequest
should specify the job name that you want to trigger:
Copy code
python    from dagster import sensor, RunRequest     @sensor(job=my_job)    def my_sensor(context):        if <condition>:  # Replace <condition> with your actual trigger condition            return RunRequest(                run_key="unique-key-for-this-run",  # A unique identifier for this run request                run_config={  # Optional: The run config for the job                    "ops": {                        "op1": {"config": {"key": "value"}},                        "op2": {"config": {"key": "value"}},                    }                },            )
Replace
<condition>
with the logic that determines when to trigger the job. The
run_key
is a unique identifier for the run request to prevent duplicate runs. The
run_config
is optional and allows you to specify configuration for the ops within the job. 3. Add the sensor to your repository definition so that Dagster knows about it and can start monitoring for the condition to trigger the job. When the condition specified in the sensor is met, the sensor will return a
RunRequest
for
my_job
, which includes the multiple ops defined within it. Dagster will then initiate a run of
my_job
with the provided configuration. Remember that sensors are periodically checked by the Dagster daemon, so make sure that the Dagster daemon is running in your environment to enable sensor execution.
25 Views