Skip to content

Commit

Permalink
chore: retry database cleanup task
Browse files Browse the repository at this point in the history
- Database cleanup task fails sporadically due to Deadline Exceeded error which happen due to various reasons including network issues they are safe to retry.
- This PR adds an exponential retry for the task.
  • Loading branch information
NivedhaSenthil committed Feb 23, 2024
1 parent c32d1e9 commit c4078c6
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions spanner/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ def cleanup_instance_resources
def cleanup_database_resources
return unless @instance

@test_database = @instance.database @database_id
@test_database&.drop
@test_database = @instance.database @restored_database_id
@test_database&.drop
with_retry "cleanup database" do
@test_database = @instance.database @database_id
@test_database&.drop
@test_database = @instance.database @restored_database_id
@test_database&.drop
end
end

def cleanup_backup_resources
Expand Down Expand Up @@ -247,4 +249,17 @@ def restore_database_from_backup

@test_database
end

def with_retry action, attempts: 5
@attempt_number ||= 0
yield
@attempt_number = nil
rescue Exception => e
@attempt_number += 1
puts "failed attempt #{@attempt_number} for #{action}"
sleep @attempt_number*@attempt_number
retry if @attempt_number < attempts
@attempt_number = nil
raise e
end
end

0 comments on commit c4078c6

Please sign in to comment.