1

一些背景。我想删除 AWS Redshift 集群,这个过程需要 30 多分钟。所以这就是我想要做的:

  1. 开始删除
  2. 每 1 分钟检查一次集群状态(应该是“正在删除”)
  3. 删除集群时,该命令将失败(因为它无法再找到集群)。所以记录一些消息并继续脚本的其余部分

这是我在while开始删除后循环运行以检查集群状态的命令:

resp = redshift.client.describe_clusters(:cluster_identifier=>"blahblahblah")

deleting上面的命令将在删除过程继续时让我获得集群状态。但是一旦集群被完全删除,那么命令本身就会失败,因为它找不到集群blahblahblah

这是删除集群后来自命令的错误:

/var/lib/gems/1.9.1/gems/aws-sdk-1.14.1/lib/aws/core/client.rb:366:in `return_or_raise': Cluster blahblahblah not found. (AWS::Redshift::Errors::ClusterNotFound)

我同意这个错误。但这使我的脚本突然退出。所以我想记录一条消息The cluster is deleted....continuing并继续我的脚本。

我尝试了以下设置

resp = redshift.client.describe_clusters(:cluster_identifier=>"blahblahblah")
 || raise (“The cluster is deleted....continuing”)

我还尝试了https://www.ruby-forum.com/topic/133876中提到的一些建议, 但这不起作用。一旦上述命令找不到集群,我的脚本就会退出。

问题:如何忽略错误,打印我自己的消息“集群已删除....继续”并继续执行脚本?

谢谢。

4

2 回答 2

1

@NewAlexandria, I changed your code to look like below:

puts "Checking the cluster status"
begin
  resp = redshift.client.describe_clusters(:cluster_identifier=>"blahblahblah")
rescue AWS::Redshift::Errors::ClusterNotFound => cluster_exception
  puts "The cluster is deleted....continuing"
end
puts "seems like the cluster is deleted and does not exist"

OUTPUT:

Checking the cluster status
The cluster is deleted....continuing
seems like the cluster is deleted and does not exist

I changed the raise to puts in the line that immediately follows the rescue line in your response. This way I got rid of the RuntimeError that I mentioned in my comment above.

I do not know what are the implication of this. I do not even know whether this is the right way to do it. But It shows the error when the cluster is not found and then continues with the script.

Later I read a lot of articles on ruby exception/rescue/raise/throw.... but that was just too much for me to understand as I do not belong to programming background at all. So, if you could explain what is going on here, it will really helpful for me to get more confidence in ruby.

Thanks for your time.

于 2013-09-21T10:31:58.560 回答
1
def delete_clusters clusters=[]
  cluster.each do |target_cluster|
    puts "will delete #{target_clust}"

    begin 
      while (some_condition) do
        resp = redshift.client.describe_clusters(:cluster_identifier => target_clust)
        # break condition
      end
    rescue AWS::Redshift::Errors::ClusterNotFound => cluster_exception
      raise ("The cluster, #{target_clust} (#{cluster_excption.id}), is deleted....continuing")
    end

    puts "doing other things now"
    # ....
  end
end
于 2013-09-20T20:08:56.267 回答