简体   繁体   English

无限循环等待变量值改变,每30秒检查一次mongo shell

[英]Infinite loop to wait for the variable value to change, and check every 30 seconds in mongo shell

Writing a bash script where I connect to mongo-shell.编写一个连接到 mongo-shell 的 bash 脚本。 I have the following output for rs.status() .我有以下 output 用于rs.status() I have to wait for one of the members to change the value of stateStr to Secondary .我必须等待其中一名成员将stateStr的值更改为Secondary In the output below, you can see, right now it says Not active .在下面的 output 中,您可以看到,现在它显示为Not active I want to write an infinite loop which will iterate through the all the members, and check the stateStr every 30 seconds until the there is a Secondary .我想编写一个无限循环,它将遍历所有成员,并每 30 秒检查一次stateStr ,直到有一个Secondary

"members" : [
  { "stateStr" : "Not active" },
  { "stateStr" : "PRIMARY" },
  { "stateStr" : "ARBITER" }
],

Here's what I have so far:这是我到目前为止所拥有的:

mongo --host ${other_host} ${ssl_mode} <<EOF
    while (true){
            for (i = 0; i <= 2; i++){
                    if (rs.status().members[i].stateStr == "SECONDARY") {
                            break;
                    }
            }
    }
    rs.status();
EOF

This however doesn't break out of the infinite loop.然而,这并没有打破无限循环。 And I want to check every 30 seconds.我想每 30 秒检查一次。 How do I do that?我怎么做?

mongo --host ${other_host} ${ssl_mode} << EOF
  while (rs.status().members.every(e => e.stateStr != "SECONDARY")) {
    sleep(1000);  
  }
  rs.status();
  EOF

Note that I also added a sleep , to avoid busy waiting.请注意,我还添加了一个sleep ,以避免忙于等待。


Update 1: If your mongo version is a little older, then you may not be able to use arrow functions and will need to spell it out like this:更新 1:如果您的 mongo 版本稍旧,那么您可能无法使用箭头功能,需要像这样拼写出来:

  while (rs.status().members.every(function(e) { return e.stateStr != "SECONDARY"; })) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM