阻塞直到所有 EC2 实例都处于特定状态?

发布于 2025-01-03 13:38:18 字数 1442 浏览 1 评论 0原文

我编写了一个函数,该函数应该阻塞,直到列表中的所有实例都不处于特定状态为止,所以这就是我所拥有的:

private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
    int numAchievedState = 0;

    while (numAchievedState != instancesId.size()) {

        numAchievedState = 0;

        DescribeInstanceStatusRequest describeRequest = new DescribeInstanceStatusRequest().withInstanceIds(instancesId);
        DescribeInstanceStatusResult instanceStatus = ec2.describeInstanceStatus(describeRequest);

        for (InstanceStatus status : instanceStatus.getInstanceStatuses()) {
            if (status.getInstanceState().getName().equals(state.toString())) {
                numAchievedState++;
            }
        }

        try {
            Thread.sleep(15000);
        } catch (InterruptedException ex) {
            Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

调用上述函数的代码是这样的:

public void startInstace(List<String> instancesId) {

    StartInstancesRequest startRequest = new StartInstancesRequest(instancesId);
    ec2.startInstances(startRequest);

    waitForInstanceState(instancesId, InstanceStateName.Running);

    System.out.println("The instance has been started");


}

问题是有时描述实例请求返回 null对于 InstanceStatus ,我认为永远不会发生,因为亚马逊中的实例总是有一个状态?此外,当在 instanceRun 函数中使用时,它可以工作,但当 InstanceStateName 设置为 Stopped 运行时,它不会。不会引发异常或错误。

I wrote a functions which is supposed to block until all instances in a list are not in a particular state, so here is what I have:

private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
    int numAchievedState = 0;

    while (numAchievedState != instancesId.size()) {

        numAchievedState = 0;

        DescribeInstanceStatusRequest describeRequest = new DescribeInstanceStatusRequest().withInstanceIds(instancesId);
        DescribeInstanceStatusResult instanceStatus = ec2.describeInstanceStatus(describeRequest);

        for (InstanceStatus status : instanceStatus.getInstanceStatuses()) {
            if (status.getInstanceState().getName().equals(state.toString())) {
                numAchievedState++;
            }
        }

        try {
            Thread.sleep(15000);
        } catch (InterruptedException ex) {
            Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

The code that invokes the above function is like that:

public void startInstace(List<String> instancesId) {

    StartInstancesRequest startRequest = new StartInstancesRequest(instancesId);
    ec2.startInstances(startRequest);

    waitForInstanceState(instancesId, InstanceStateName.Running);

    System.out.println("The instance has been started");


}

The thing is sometimes the describe instance request returns null for InstanceStatus which I think should never happen since an instance in amazon always have a state? Also when used in the instanceRun function it works, when run with InstanceStateName set to Stopped it doesn't. No exceptions or errors are thrown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭了丶谁疼 2025-01-10 13:38:18

以下工作完成了:

 private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
        int numAchievedState = 0;

        while (numAchievedState != instancesId.size()) {

            try {
                Thread.sleep(15000);
            } catch (InterruptedException ex) {
                Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
            }

            numAchievedState = 0;

            DescribeInstancesRequest describeInstance = new DescribeInstancesRequest().withInstanceIds(instancesId);
            DescribeInstancesResult describeResult = ec2.describeInstances(describeInstance);
            List<Reservation> reservations = describeResult.getReservations();

            //different instances might be in different reservation requests
            //so we need to traverse those
            for (Reservation reservation : reservations) {
                List<Instance> instances = reservation.getInstances();
                for (Instance instance : instances) {
                    if (instance.getState().getName().equals(state.toString())) {
                        numAchievedState++;
                    }
                }
            }
        }
    }

The following did the job:

 private void waitForInstanceState(List<String> instancesId, InstanceStateName state) {
        int numAchievedState = 0;

        while (numAchievedState != instancesId.size()) {

            try {
                Thread.sleep(15000);
            } catch (InterruptedException ex) {
                Logger.getLogger(AmazonLibrary.class.getName()).log(Level.SEVERE, null, ex);
            }

            numAchievedState = 0;

            DescribeInstancesRequest describeInstance = new DescribeInstancesRequest().withInstanceIds(instancesId);
            DescribeInstancesResult describeResult = ec2.describeInstances(describeInstance);
            List<Reservation> reservations = describeResult.getReservations();

            //different instances might be in different reservation requests
            //so we need to traverse those
            for (Reservation reservation : reservations) {
                List<Instance> instances = reservation.getInstances();
                for (Instance instance : instances) {
                    if (instance.getState().getName().equals(state.toString())) {
                        numAchievedState++;
                    }
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文