Skip to content

Commit

Permalink
fix gcp api error handling for single message errors
Browse files Browse the repository at this point in the history
  • Loading branch information
var77 committed May 5, 2024
1 parent c7436fe commit 96a873d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/hosting/gcp_apis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ def initialize
def self.check_errors(response)
body = JSON.parse(response.body)

errors = body.fetch("error", {}).fetch("errors", [])
if errors.size > 0
error = body.fetch("error", {})
errors = error.fetch("errors", [])

if errors.size > 0 || !error["message"].nil?
Clog.emit("Error received from GCP APIs") { {body: body} }
fail errors[0]["message"]
fail (errors.size > 0) ? errors[0]["message"] : error["message"]
end
end

Expand Down
21 changes: 17 additions & 4 deletions misc/misc_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,20 @@ def self.kill_query(resource_name, pid)
LanternResource[name: resource_name].representative_server.run_query("SELECT pg_terminate_backend(#{pid})")
end

def self.task_logs(resource_name, task_name)
LanternResource[name: resource_name].representative_server.vm.sshable.cmd("common/bin/daemonizer --logs #{task_name}")
def self.task_logs(resource_name, task_name, poll: false)
loop do
puts LanternResource[name: resource_name].representative_server.vm.sshable.cmd("common/bin/daemonizer --logs #{task_name}")
break if !poll
sleep(2)
end
end

def self.task_status(resource_name, task_name, poll: false)
loop do
puts LanternResource[name: resource_name].representative_server.vm.sshable.cmd("common/bin/daemonizer --check #{task_name}")
break if !poll
sleep(2)
end
end

def self.query_on_db(vm, db_name, query)
Expand All @@ -85,7 +97,7 @@ def self.reindex_all_concurrently(resource_name, disable_indexes: false)
command_list = []
all_dbs.each do |db|
db_name = db.strip
indexes = MiscOperations.query_on_db(serv.vm, db_name, "SELECT i.relname::text
indexes = MiscOperations.query_on_db(serv.vm, db_name, "SELECT n.nspname::text || '.' || i.relname
FROM pg_class t
JOIN pg_index ix ON t.oid = ix.indrelid
JOIN pg_class i ON i.oid = ix.indexrelid
Expand All @@ -96,7 +108,8 @@ def self.reindex_all_concurrently(resource_name, disable_indexes: false)
if !indexes.empty?
queries = []
indexes.each {
queries.push("REINDEX INDEX CONCURRENTLY #{_1};")
schema, idx = _1.split(".")
queries.push("REINDEX INDEX CONCURRENTLY \\\"#{schema}\\\".\\\"#{idx}\\\";")
if disable_indexes
queries.push("UPDATE pg_index SET indisvalid = false, indisready = false WHERE indexrelid = quote_ident('#{_1}')::regclass::oid;")
end
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/hosting/gcp_apis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
}.to raise_error "test error"
end

it "throws error from response if one error" do
expect {
described_class.check_errors(OpenStruct.new({body: JSON.dump({error: {message: "permission error", errors: []}})}))
}.to raise_error "permission error"
end

it "throws error from errors if both defined" do
expect {
described_class.check_errors(OpenStruct.new({body: JSON.dump({error: {message: "permission error", errors: [{message: "test error"}]}})}))
}.to raise_error "test error"
end

it "does not throw error" do
expect {
described_class.check_errors(OpenStruct.new({body: JSON.dump({error: {errors: []}})}))
Expand Down

0 comments on commit 96a873d

Please sign in to comment.