【问题标题】:How to check elasticsearch snapshot status every X seconds using while loop in python?如何在python中使用while循环每X秒检查一次elasticsearch快照状态?
【发布时间】:2019-05-14 14:43:09
【问题描述】:

我正在尝试配置运行快照的状态检查 python 代码,当状态为“IN_PROGRESS”或“STARTED”时,该代码将继续执行并退出并为每个其他状态。 例如,在 BASH 中它会是这样的:

while [ "$SNAP_STATUS" == "IN_PROGRESS" ] || [ "$SNAP_STATUS" == "STARTED" ] && [ "$counter" -lt 150 ]
do
    sleep 60
    SNAP_STATUS=$(curl -s -X GET "$ELASTIC_SERVER:9200/_snapshot/$REPOSITORY/$SNAPSHOT/_status"?pretty | jq .snapshots[].state -r)
    echo "SNAPSHOT NAME: $SNAPSHOT - STATUS: $SNAP_STATUS"
    counter=$(( $counter + 1 ))
    echo $counter
done

if [[ "$SNAP_STATUS" == "SUCCESS" ]] ; then
    echo "YAY!"
else
    echo "BOO..."
fi

【问题讨论】:

    标签: python bash elasticsearch snapshot


    【解决方案1】:

    希望我理解正确:

    import requests
    from time import sleep
    
    your_snapshot_url = ""
    snap_status = "IN_PROGRESS"
    counter = 0
    while snap_status == "IN_PROGRESS" or (snap_status == "STARTED" and counter < 150):
        sleep(60)
        req = requests.get(your_snapshot_url).content
        snap_status = req['snapshots']['state'] # Replace with whatever you need to parse the state
        counter += 1
        print(counter)
    
    if snap_status == "SUCCESS":
        print("YAY!")
    else:
        print("BOO...")
    

    这将每 60 秒向 your_snapshot_url 发出一个请求,同时每个周期递增计数器。 当 snap_status 如果不是 IN_PROGRESS 或已启动但计数器小于 150 时,while 循环将退出

    退出时,如果快照状态为成功,您会收到“YAY”打印,否则会收到“BOO”

    【讨论】:

    • 谢谢@alin-iuga,但我的意思是使用elasticsearch包
    猜你喜欢
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    相关资源
    最近更新 更多