From 96a873d27a8e7495ca11c7146a651fa5cdb3bcaf Mon Sep 17 00:00:00 2001 From: Varik Matevosyan Date: Sun, 5 May 2024 11:10:44 -0700 Subject: [PATCH] fix gcp api error handling for single message errors --- lib/hosting/gcp_apis.rb | 8 +++++--- misc/misc_operations.rb | 21 +++++++++++++++++---- spec/lib/hosting/gcp_apis_spec.rb | 12 ++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/hosting/gcp_apis.rb b/lib/hosting/gcp_apis.rb index 59629ed46..c52983a36 100644 --- a/lib/hosting/gcp_apis.rb +++ b/lib/hosting/gcp_apis.rb @@ -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 diff --git a/misc/misc_operations.rb b/misc/misc_operations.rb index b3f656927..913e03590 100644 --- a/misc/misc_operations.rb +++ b/misc/misc_operations.rb @@ -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) @@ -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 @@ -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 diff --git a/spec/lib/hosting/gcp_apis_spec.rb b/spec/lib/hosting/gcp_apis_spec.rb index fdc102662..388633272 100644 --- a/spec/lib/hosting/gcp_apis_spec.rb +++ b/spec/lib/hosting/gcp_apis_spec.rb @@ -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: []}})}))