Skip to content

Commit fd28b97

Browse files
committed
Add retry when writing progress cache.
1 parent d292539 commit fd28b97

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

src/queryEngine.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const plugins = [
9090
]
9191
const QUERY_FREQ_MS = 60 * 1000
9292
const MAX_CONCURRENT_QUERIES = 3
93-
const BULK_FETCH_SIZE = 50
93+
const BULK_FETCH_SIZE = 500
9494
const snooze: Function = async (ms: number) =>
9595
await new Promise((resolve: Function) => setTimeout(resolve, ms))
9696

@@ -371,13 +371,31 @@ async function runPlugin(params: RunPluginParams): Promise<string> {
371371
})
372372
progressSettings.progressCache = result.settings
373373
progressSettings._id = progressCacheFileName
374-
await promiseTimeout(
375-
'dbProgress.insert',
376-
dbProgress.insert(progressSettings),
377-
log
378-
).catch(e => {
379-
throw new Error(`Error inserting progress: ${String(e)}`)
380-
})
374+
375+
// Attempt to insert progress with retry on conflict
376+
const maxAttempts = 2
377+
let attempt = 0
378+
while (attempt < maxAttempts) {
379+
attempt++
380+
try {
381+
await promiseTimeout(
382+
`dbProgress.insert (attempt ${attempt})`,
383+
dbProgress.insert(progressSettings),
384+
log
385+
)
386+
break
387+
} catch (e) {
388+
const err: any = e
389+
const isConflict = err.statusCode === 409 || err.error === 'conflict'
390+
if (isConflict && attempt < maxAttempts) {
391+
log(`[runPlugin] Document conflict detected, re-reading and retrying`)
392+
const updatedDoc = await dbProgress.get(progressCacheFileName)
393+
progressSettings._rev = updatedDoc._rev
394+
continue
395+
}
396+
throw new Error(`Error inserting progress: ${String(e)}`)
397+
}
398+
}
381399
// Returning a successful completion message
382400
const completionTime = (Date.now() - start) / 1000
383401
const successfulCompletionMessage = `[runPlugin] ${partnerId} Successful update in ${completionTime} seconds.`

0 commit comments

Comments
 (0)