diff --git a/lib/actions/google/drive/sheets/google_sheets.d.ts b/lib/actions/google/drive/sheets/google_sheets.d.ts index ad1d023c..4f9682aa 100644 --- a/lib/actions/google/drive/sheets/google_sheets.d.ts +++ b/lib/actions/google/drive/sheets/google_sheets.d.ts @@ -25,6 +25,6 @@ export declare class GoogleSheetsAction extends GoogleDriveAction { retriableFileList(drive: Drive, options: any, attempt: number, webhookId: string): Promise; flush(buffer: sheets_v4.Schema$BatchUpdateSpreadsheetRequest, sheet: Sheet, spreadsheetId: string, webhookId: string): Promise>; flushRetry(buffer: sheets_v4.Schema$BatchUpdateSpreadsheetRequest, sheet: Sheet, spreadsheetId: string, webhookId: string): Promise>; - protected delay(time: number): Promise; + delay(time: number): Promise; protected sheetsClientFromRequest(redirect: string, tokens: Credentials): Promise; } diff --git a/lib/actions/google/drive/sheets/google_sheets.js b/lib/actions/google/drive/sheets/google_sheets.js index 9ff3d5f4..12efd8da 100644 --- a/lib/actions/google/drive/sheets/google_sheets.js +++ b/lib/actions/google/drive/sheets/google_sheets.js @@ -14,9 +14,10 @@ const http_errors_1 = require("../../../../error_types/http_errors"); const Hub = require("../../../../hub"); const parse = require("csv-parse"); const googleapis_1 = require("googleapis"); +const promises_1 = require("timers/promises"); const winston = require("winston"); const utils_1 = require("../../../../error_types/utils"); -const action_response_1 = require("../../../../hub/action_response"); +const hub_1 = require("../../../../hub"); const google_drive_1 = require("../google_drive"); const MAX_REQUEST_BATCH = process.env.GOOGLE_SHEETS_WRITE_BATCH ? Number(process.env.GOOGLE_SHEETS_WRITE_BATCH) : 4096; const MAX_ROW_BUFFER_INCREASE = 6000; @@ -75,7 +76,7 @@ class GoogleSheetsAction extends google_drive_1.GoogleDriveAction { catch (e) { this.sanitizeGaxiosError(e); const errorType = (0, utils_1.getHttpErrorType)(e, this.name); - let error = (0, action_response_1.errorWith)(errorType, `${LOG_PREFIX} ${e.toString()}`); + let error = (0, hub_1.errorWith)(errorType, `${LOG_PREFIX} ${e.toString()}`); if (e.code && e.errors && e.errors[0] && e.errors[0].message) { error = Object.assign(Object.assign({}, error), { http_code: e.code, message: `${errorType.description} ${LOG_PREFIX} ${e.errors[0].message}` }); } @@ -446,9 +447,7 @@ class GoogleSheetsAction extends google_drive_1.GoogleDriveAction { } delay(time) { return __awaiter(this, void 0, void 0, function* () { - yield new Promise((resolve) => { - setTimeout(resolve, time); - }); + return yield (0, promises_1.setTimeout)(time).catch((_) => { winston.error("setTimeout throw!"); }); }); } sheetsClientFromRequest(redirect, tokens) { diff --git a/src/actions/google/drive/sheets/google_sheets.ts b/src/actions/google/drive/sheets/google_sheets.ts index 12de8a16..1aa648d2 100644 --- a/src/actions/google/drive/sheets/google_sheets.ts +++ b/src/actions/google/drive/sheets/google_sheets.ts @@ -5,9 +5,10 @@ import * as parse from "csv-parse" import { Credentials } from "google-auth-library" import { drive_v3, google, sheets_v4 } from "googleapis" import { GaxiosPromise } from "googleapis-common" +import {setTimeout} from "timers/promises" import * as winston from "winston" import { getHttpErrorType } from "../../../../error_types/utils" -import { Error, errorWith } from "../../../../hub/action_response" +import { Error, errorWith } from "../../../../hub" import { GoogleDriveAction } from "../google_drive" import Drive = drive_v3.Drive import Sheet = sheets_v4.Sheets @@ -444,10 +445,8 @@ export class GoogleSheetsAction extends GoogleDriveAction { throw `Max retries attempted` } - protected async delay(time: number) { - await new Promise((resolve) => { - setTimeout(resolve, time) - }) + async delay(time: number) { + return await setTimeout(time).catch((_) => { winston.error("setTimeout throw!")}) } protected async sheetsClientFromRequest(redirect: string, tokens: Credentials) { diff --git a/src/actions/google/drive/sheets/test_google_sheets.ts b/src/actions/google/drive/sheets/test_google_sheets.ts index 252feeb4..e97c32cb 100644 --- a/src/actions/google/drive/sheets/test_google_sheets.ts +++ b/src/actions/google/drive/sheets/test_google_sheets.ts @@ -538,6 +538,15 @@ describe(`${action.constructor.name} unit tests`, () => { }) }) + describe("delay", () => { + it("will block on await delay", async () => { + const start = Date.now() + await action.delay(100) + const end = Date.now() + chai.expect(end - start).to.be.greaterThanOrEqual(10) + }) + }) + describe("flushRetry", () => { it("will retry until the MAX_RETRY_LIMIT is reached", (done) => { const delayStub = sinon.stub(action as any, "delay")