确认 Watch API

编辑

确认一个 watch 可以让你手动节流 watch 的 action 的执行。

请求

编辑

PUT _watcher/watch/<watch_id>/_ack

PUT _watcher/watch/<watch_id>/_ack/<action_id>

前提条件

编辑
  • 你必须拥有 manage_watcher 集群权限才能使用此 API。有关更多信息,请参阅 安全权限

描述

编辑

一个 action 的确认状态存储在 status.actions.<id>.ack.state 结构中。

如果指定的 watch 当前正在执行,此 API 将返回错误。这样做的原因是防止从 watch 执行中覆盖 watch 状态。

路径参数

编辑
<action_id>
(可选,列表)要确认的 action ID 的逗号分隔列表。如果你省略此参数,则将确认 watch 的所有 action。
<watch_id>
(必需,字符串)watch 的标识符。

示例

编辑

为了演示,让我们创建一个新的 watch

resp = client.watcher.put_watch(
    id="my_watch",
    trigger={
        "schedule": {
            "yearly": {
                "in": "february",
                "on": 29,
                "at": "noon"
            }
        }
    },
    input={
        "simple": {
            "payload": {
                "send": "yes"
            }
        }
    },
    condition={
        "always": {}
    },
    actions={
        "test_index": {
            "throttle_period": "15m",
            "index": {
                "index": "test"
            }
        }
    },
)
print(resp)
const response = await client.watcher.putWatch({
  id: "my_watch",
  trigger: {
    schedule: {
      yearly: {
        in: "february",
        on: 29,
        at: "noon",
      },
    },
  },
  input: {
    simple: {
      payload: {
        send: "yes",
      },
    },
  },
  condition: {
    always: {},
  },
  actions: {
    test_index: {
      throttle_period: "15m",
      index: {
        index: "test",
      },
    },
  },
});
console.log(response);
PUT _watcher/watch/my_watch
{
  "trigger" : {
    "schedule" : {
      "yearly" : { "in" : "february", "on" : 29, "at" : "noon" }
    }
  },
  "input": {
    "simple": {
      "payload": {
        "send": "yes"
      }
    }
  },
  "condition": {
    "always": {}
  },
  "actions": {
    "test_index": {
      "throttle_period": "15m",
      "index": {
        "index": "test"
      }
    }
  }
}

当你调用 获取 Watch API 时,watch 的当前状态及其 action 的状态将与 watch 定义一起返回。

resp = client.watcher.get_watch(
    id="my_watch",
)
print(resp)
const response = await client.watcher.getWatch({
  id: "my_watch",
});
console.log(response);
GET _watcher/watch/my_watch

新创建的 watch 的 action 状态为 awaits_successful_execution

{
  "found": true,
  "_seq_no": 0,
  "_primary_term": 1,
  "_version": 1,
  "_id": "my_watch",
  "status": {
    "version": 1,
    "actions": {
      "test_index": {
        "ack": {
          "timestamp": "2015-05-26T18:04:27.723Z",
          "state": "awaits_successful_execution"
        }
      }
    },
    "state": ...
  },
  "watch": ...
}

当 watch 执行并且条件匹配时,ack.state 的值将更改为 ackable。让我们强制执行 watch 并再次获取它以检查状态

resp = client.watcher.execute_watch(
    id="my_watch",
    record_execution=True,
)
print(resp)

resp1 = client.watcher.get_watch(
    id="my_watch",
)
print(resp1)
const response = await client.watcher.executeWatch({
  id: "my_watch",
  record_execution: true,
});
console.log(response);

const response1 = await client.watcher.getWatch({
  id: "my_watch",
});
console.log(response1);
POST _watcher/watch/my_watch/_execute
{
  "record_execution" : true
}

GET _watcher/watch/my_watch

并且 action 现在处于 ackable 状态

{
  "found": true,
  "_id": "my_watch",
  "_seq_no": 1,
  "_primary_term": 1,
  "_version": 2,
  "status": {
    "version": 2,
    "actions": {
      "test_index": {
        "ack": {
          "timestamp": "2015-05-26T18:04:27.723Z",
          "state": "ackable"
        },
        "last_execution" : {
          "timestamp": "2015-05-25T18:04:27.723Z",
          "successful": true
        },
        "last_successful_execution" : {
          "timestamp": "2015-05-25T18:04:27.723Z",
          "successful": true
        }
      }
    },
    "state": ...,
    "execution_state": "executed",
    "last_checked": ...,
    "last_met_condition": ...
  },
  "watch": ...
}

现在我们可以确认它了

resp = client.watcher.ack_watch(
    watch_id="my_watch",
    action_id="test_index",
)
print(resp)

resp1 = client.watcher.get_watch(
    id="my_watch",
)
print(resp1)
const response = await client.watcher.ackWatch({
  watch_id: "my_watch",
  action_id: "test_index",
});
console.log(response);

const response1 = await client.watcher.getWatch({
  id: "my_watch",
});
console.log(response1);
PUT _watcher/watch/my_watch/_ack/test_index
GET _watcher/watch/my_watch
{
  "found": true,
  "_id": "my_watch",
  "_seq_no": 2,
  "_primary_term": 1,
  "_version": 3,
  "status": {
    "version": 3,
    "actions": {
      "test_index": {
        "ack": {
          "timestamp": "2015-05-26T18:04:27.723Z",
          "state": "acked"
        },
        "last_execution" : {
          "timestamp": "2015-05-25T18:04:27.723Z",
          "successful": true
        },
        "last_successful_execution" : {
          "timestamp": "2015-05-25T18:04:27.723Z",
          "successful": true
        }
      }
    },
    "state": ...,
    "execution_state": "executed",
    "last_checked": ...,
    "last_met_condition": ...
  },
  "watch": ...
}

确认 action 会节流该 action 的进一步执行,直到其 ack.state 重置为 awaits_successful_execution。当不满足 watch 的条件时(该条件评估为 false)会发生这种情况。

你可以通过为 actions 参数分配一个逗号分隔的 action ID 列表来确认多个 action

resp = client.watcher.ack_watch(
    watch_id="my_watch",
    action_id="action1,action2",
)
print(resp)
const response = await client.watcher.ackWatch({
  watch_id: "my_watch",
  action_id: "action1,action2",
});
console.log(response);
POST _watcher/watch/my_watch/_ack/action1,action2

要确认 watch 的所有 action,只需省略 actions 参数即可

resp = client.watcher.ack_watch(
    watch_id="my_watch",
)
print(resp)
const response = await client.watcher.ackWatch({
  watch_id: "my_watch",
});
console.log(response);
POST _watcher/watch/my_watch/_ack

响应看起来像获取 watch 响应,但仅包含状态

{
  "status": {
    "state": {
      "active": true,
      "timestamp": "2015-05-26T18:04:27.723Z"
    },
    "last_checked": "2015-05-26T18:04:27.753Z",
    "last_met_condition": "2015-05-26T18:04:27.763Z",
    "actions": {
      "test_index": {
        "ack" : {
          "timestamp": "2015-05-26T18:04:27.713Z",
          "state": "acked"
        },
        "last_execution" : {
          "timestamp": "2015-05-25T18:04:27.733Z",
          "successful": true
        },
        "last_successful_execution" : {
          "timestamp": "2015-05-25T18:04:27.773Z",
          "successful": true
        }
      }
    },
    "execution_state": "executed",
    "version": 2
  }
}