mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-06-09 09:04:20 +02:00
Fix gsheet api request saturation by increasing waiting time between requests
This commit is contained in:
parent
1e10f773fb
commit
d8e7bb9f70
@ -11,8 +11,8 @@ export { SheetNames } from "./localDb"
|
|||||||
|
|
||||||
const CRED_PATH = path.resolve(process.cwd(), "access/gsheets.json")
|
const CRED_PATH = path.resolve(process.cwd(), "access/gsheets.json")
|
||||||
|
|
||||||
const REMOTE_UPDATE_DELAY = 40000
|
const REMOTE_UPDATE_DELAY = 80000
|
||||||
const DELAY_AFTER_QUERY = 2000
|
const DELAY_BETWEEN_ATTEMPTS = 10000
|
||||||
|
|
||||||
let creds: string | undefined | null
|
let creds: string | undefined | null
|
||||||
|
|
||||||
@ -233,7 +233,6 @@ export class Sheet<
|
|||||||
await tryNTimesVoidReturn(async () => {
|
await tryNTimesVoidReturn(async () => {
|
||||||
// Load sheet into an array of objects
|
// Load sheet into an array of objects
|
||||||
const rows = await sheet.getRows()
|
const rows = await sheet.getRows()
|
||||||
await delayDBAccess()
|
|
||||||
if (!rows[0]) {
|
if (!rows[0]) {
|
||||||
throw new Error(`No column types defined in sheet ${this.name}`)
|
throw new Error(`No column types defined in sheet ${this.name}`)
|
||||||
}
|
}
|
||||||
@ -258,8 +257,6 @@ export class Sheet<
|
|||||||
if (!row) {
|
if (!row) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
await sheet.addRow(stringifiedRow)
|
await sheet.addRow(stringifiedRow)
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
await delayDBAccess()
|
|
||||||
} else {
|
} else {
|
||||||
const keys = Object.keys(stringifiedRow)
|
const keys = Object.keys(stringifiedRow)
|
||||||
const sameCells = _.every(keys, (key: keyof Element) => {
|
const sameCells = _.every(keys, (key: keyof Element) => {
|
||||||
@ -273,8 +270,6 @@ export class Sheet<
|
|||||||
})
|
})
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
await row.save()
|
await row.save()
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
await delayDBAccess()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,8 +281,6 @@ export class Sheet<
|
|||||||
if (rows[rowToDelete]) {
|
if (rows[rowToDelete]) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
await rows[rowToDelete].delete()
|
await rows[rowToDelete].delete()
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
await delayDBAccess()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -304,7 +297,6 @@ export class Sheet<
|
|||||||
await tryNTimesVoidReturn(async () => {
|
await tryNTimesVoidReturn(async () => {
|
||||||
// Load sheet into an array of objects
|
// Load sheet into an array of objects
|
||||||
const rows = (await sheet.getRows()) as StringifiedElement[]
|
const rows = (await sheet.getRows()) as StringifiedElement[]
|
||||||
await delayDBAccess()
|
|
||||||
const elements: Element[] = []
|
const elements: Element[] = []
|
||||||
if (!rows[0]) {
|
if (!rows[0]) {
|
||||||
throw new Error(`No column types defined in sheet ${this.name}`)
|
throw new Error(`No column types defined in sheet ${this.name}`)
|
||||||
@ -354,7 +346,9 @@ export class Sheet<
|
|||||||
await doc.loadInfo()
|
await doc.loadInfo()
|
||||||
return doc.sheetsByTitle[this.sheetName]
|
return doc.sheetsByTitle[this.sheetName]
|
||||||
},
|
},
|
||||||
() => null
|
() => null,
|
||||||
|
20,
|
||||||
|
DELAY_BETWEEN_ATTEMPTS / 5
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -593,15 +587,11 @@ function parseDate(value: string): Date {
|
|||||||
return new Date(+matchDate[1], +matchDate[2] - 1, +matchDate[3])
|
return new Date(+matchDate[1], +matchDate[2] - 1, +matchDate[3])
|
||||||
}
|
}
|
||||||
|
|
||||||
async function delayDBAccess(): Promise<void> {
|
|
||||||
return new Promise((resolve) => setTimeout(resolve, DELAY_AFTER_QUERY))
|
|
||||||
}
|
|
||||||
|
|
||||||
async function tryNTimes<T>(
|
async function tryNTimes<T>(
|
||||||
func: () => Promise<T> | T,
|
func: () => Promise<T> | T,
|
||||||
failFunc?: () => Promise<T> | T,
|
failFunc?: () => Promise<T> | T,
|
||||||
repeatCount = 5,
|
repeatCount = 5,
|
||||||
delayBetweenAttempts = 2000
|
delayBetweenAttempts = DELAY_BETWEEN_ATTEMPTS
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
try {
|
try {
|
||||||
return await func()
|
return await func()
|
||||||
@ -625,7 +615,7 @@ async function tryNTimes<T>(
|
|||||||
async function tryNTimesVoidReturn(
|
async function tryNTimesVoidReturn(
|
||||||
func: () => Promise<void> | void,
|
func: () => Promise<void> | void,
|
||||||
repeatCount = 5,
|
repeatCount = 5,
|
||||||
delayBetweenAttempts = 2000
|
delayBetweenAttempts = DELAY_BETWEEN_ATTEMPTS
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
return tryNTimes(func, () => undefined, repeatCount, delayBetweenAttempts)
|
return tryNTimes(func, () => undefined, repeatCount, delayBetweenAttempts)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user