How to ignore error and continue with rest of the

2019-07-27 12:08发布

Some background. I want to delete AWS Redshift cluster and the process takes more than 30 minute. So this is what I want to do:

  1. Start the deletion
  2. Every 1 minute, check the cluster status (it should be “deleting”)
  3. When the cluster is deleted, the command would fail (because it cannot find the cluster anymore). So log some message and continue with rest of the script

This is the command I run in a while loop to check the cluster status after I start the deletion:

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

Above command will get me cluster status as deleting while the deletion process continues. But once the cluster is completely deleted, then the command itself will fail as it cannot find the cluster blahblahblah.

Here is the error from command once the cluster is deleted:

/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)

I agree with this error. But this makes my script exit abruptly. So I want to log a message saying The cluster is deleted....continuing and continue with my script.

I tried below settings

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

I also tried couple of suggestion mentioned at https://www.ruby-forum.com/topic/133876 But this is not working. My script exits once above command fails to find the cluster.

Questions: How to ignore the error, print my own message saying “The cluster is deleted....continuing” and continue with the script ?

Thanks.

2条回答
The star\"
2楼-- · 2019-07-27 12:35
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
查看更多
劳资没心,怎么记你
3楼-- · 2019-07-27 12:42

@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.

查看更多
登录 后发表回答