# Watcher - input - chain: conditional execution of a chain

**URL:** https://discuss.elastic.co/t/watcher-input-chain-conditional-execution-of-a-chain/318554
**Category:** Kibana
**Tags:** elastic-stack-alerting, painless
**Created:** [November 9, 2022, 3:24pm UTC](https://discuss.elastic.co/t/watcher-input-chain-conditional-execution-of-a-chain/318554 "2022-11-09T15:24:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nico33b\_nico33b](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nico33b_nico33b/32/113162_2.png) [@nico33b\_nico33b](https://discuss.elastic.co/u/nico33b_nico33b)
#### Post date: [November 9, 2022, 3:24pm UTC](https://discuss.elastic.co/t/watcher-input-chain-conditional-execution-of-a-chain/318554/1 "2022-11-09T15:24:31Z")

</div>

I'm using the chain feature in a watcher.  
I'm looking for a proper way to stop the execution of a specific input depending of the result of the previous one.

Let say, you have a 3 steps chain which do something like:

- search query on an index
- transform and adapt the result of the last result
- execute a msearch with http input request with payload provided by last transform

Sample code below to illustrate.

If the result on first\_search is empty, I don't want to execute the 3rd step of my inputs.  
Is there anyway to do this ?  
I know you can set a condition on an action, but I don't find a way to set a condition on input.

I was wondering if there was some solution to achieve this.

Thanks for your help.

```auto
{
  "trigger": {
    "schedule": {
      "interval": "10s"
    }
  },
  "input": {
    "chain": {
      "inputs": [
        {
          "first_search": {
            "search": {
              "request": {
                "search_type": "query_then_fetch",
                "indices": [
                  "my_index"
                ],
                "rest_total_hits_as_int": true,
                "body": {
                  "query": {
                    "bool": {
                      "filter": [
                        {"my wonderfull filter"},
                      ]
                    }
                  }
                  }
                }
              }
            }
          }
        },
        {
          "prepare_msearch_payload": {
            "transform": {
              "script": {
                "source": """
                      if (ctx.payload.first_search.hits != null) {
                                /* do some stuff to prepare data for next msearch */
                        } else { return null; }
                      """,
                "lang": "painless"
              }
            }
          }
        },
        {
          "last_msearch": {
            "http": {
              "request": {
                "scheme": "http",
                "host": "localhost",
                "port": 9200,
                "method": "post",
                "path": "my-wonderfull-index/_msearch",
                "params": {},
                "body": """{{#ctx.payload.prepare_msearch_payload._value}}{}
{{.}}
{{/ctx.payload.prepare_msearch_payload._value}}"""
              }
            }
          }
        }
      ]
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.last_msearch.hits.total": {
        "gte": 1
      }
    }
  },
  "actions": {
    "my-logging-action": {
      "logging": {
        "level": "info",
        "text": """ my log details"""
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Luca\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_b/32/92430_2.png) [@Luca\_B](https://discuss.elastic.co/u/Luca_B)
#### Post date: [November 25, 2022, 9:16am UTC](https://discuss.elastic.co/t/watcher-input-chain-conditional-execution-of-a-chain/318554/2 "2022-11-25T09:16:55Z")

</div>

Interrupting the chain is not supported. What you can do is to add a step in the chain of type transform and throw a RuntimeException. This will cause the next step in the chain to fail because of a null Payload.

```auto
"chain": {
  "inputs": [
       { "input_one": {} },
       {
          "skip_when": {
               "transform": {
                    "script": """
                         if (condition_to_skip) throw new RuntimeException('Crash the watcher');
                    """
                 }
           }
       },
       {"last_optional_input": {}}
   ]
}

```

The condition\_to\_skip it's up to you, something like -\> ctx.payload.first\_search._result\_field_.isEmpty()

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 23, 2022, 2:35am UTC](https://discuss.elastic.co/t/watcher-input-chain-conditional-execution-of-a-chain/318554/3 "2022-12-23T02:35:47Z")

</div>



---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 20, 2023, 2:35am UTC](https://discuss.elastic.co/t/watcher-input-chain-conditional-execution-of-a-chain/318554/4 "2023-01-20T02:35:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
