From 4b2d36ac5e332dc7ec9732dbcabb562668e6cd58 Mon Sep 17 00:00:00 2001 From: zhgchgli Date: Sat, 20 Aug 2022 17:31:13 +0800 Subject: [PATCH] bug fix --- .version | 2 +- lib/GoogleAPI.rb | 21 ++++++++++++++------- lib/Processors/GoogleSheetProcessor.rb | 12 +++++++++--- lib/Processors/SlackProcessor.rb | 1 + 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/.version b/.version index 6261a05..d5e98f7 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -1.3.1 \ No newline at end of file +1.3.2 \ No newline at end of file diff --git a/lib/GoogleAPI.rb b/lib/GoogleAPI.rb index ca57296..c3b16ea 100644 --- a/lib/GoogleAPI.rb +++ b/lib/GoogleAPI.rb @@ -45,14 +45,21 @@ def request(url, method = "GET", data = nil, retryCount = 0) result = JSON.parse(response) if !result["error"].nil? - if retryCount >= 10 - raise "Could not connect to #{tokenURI}, key id: #{keyID}, error message: #{response}" + # Quota exceeded for quota metric 'Write requests' and limit 'Write requests per minute per user + if result["error"]["code"] == 429 + puts "[GoogleAPI] Reached Rate Limited, sleep 30 secs..." + sleep(30) + return request(url, method, data, 0) else - @token = generateJWT() - message = "JWT Invalid, retry. (#{retryCount + 1})" - logger.logWarn(message) - puts "[GoogleAPI] #{message}" - return request(url, method, data, retryCount + 1) + if retryCount >= 10 + raise "Could not connect to #{tokenURI}, key id: #{keyID}, error message: #{response}" + else + @token = generateJWT() + message = "JWT Invalid, retry. (#{retryCount + 1})" + logger.logWarn(message) + puts "[GoogleAPI] #{message}" + return request(url, method, data, retryCount + 1) + end end else return result diff --git a/lib/Processors/GoogleSheetProcessor.rb b/lib/Processors/GoogleSheetProcessor.rb index bbd3af0..6acb0ce 100644 --- a/lib/Processors/GoogleSheetProcessor.rb +++ b/lib/Processors/GoogleSheetProcessor.rb @@ -73,6 +73,7 @@ def processReviews(reviews, platform) end end + values = [] filterReviews.each do |review| cols = [] formatValues.each do |formatValue| @@ -89,10 +90,15 @@ def processReviews(reviews, platform) cols.append(formatValue) end - - puts "[GoogleSheetProcessor] insert #{cols} to #{sheetID}-#{sheetName}" + values.append(cols) + end - googleAPI.request("https://sheets.googleapis.com/v4/spreadsheets/#{sheetID}/values/#{sheetName}:append?valueInputOption=RAW", "POST", {:values => [cols]}) + page = 1 + limit = 500 + values.each_slice(limit) do |value| + puts "[GoogleSheetProcessor] Insert rows(#{page}/#{(values.length/limit).ceil + 1}) to #{sheetID}-#{sheetName}" + page += 1 + googleAPI.request("https://sheets.googleapis.com/v4/spreadsheets/#{sheetID}/values/#{sheetName}!A1:append?valueInputOption=RAW", "POST", {:values => value}) end return reviews diff --git a/lib/Processors/SlackProcessor.rb b/lib/Processors/SlackProcessor.rb index 40547d1..e78a992 100644 --- a/lib/Processors/SlackProcessor.rb +++ b/lib/Processors/SlackProcessor.rb @@ -82,6 +82,7 @@ def processReviews(reviews, platform) logger.logError(payload) logger.logError(result) if result[:message] == "ratelimited" + puts "[SlackProcessor] Reached Rate Limited, sleep 1 sec..." sleep(1) pendingPayloads.insert(0, payload) end