Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/features/webhook/dropbox/lib/webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { generateToken } from '@/lib/copilot/generateToken'
import User from '@/lib/copilot/models/User.model'
import { DropboxClient } from '@/lib/dropbox/DropboxClient'
import logger from '@/lib/logger'
import { withRetry } from '@/lib/withRetry'
import { handleChannelFileChanges, processDropboxChanges } from '@/trigger/processFileSync'

const DEBOUNCE_WINDOW_MS = 5 * 60 * 1000 // 5 minutes
Expand Down Expand Up @@ -112,9 +113,11 @@ export class DropboxWebhook {
await db.update(dropboxConnections).set({ lastWebhookSyncedAt: new Date() }).where(conditions)
}

// Refactor below code. Move the function to DropboxClient file and call it from here.
async getDropboxFileMetadata(filePath: string, dbxClient: Dropbox) {
return await dbxClient.filesGetMetadata({
path: filePath,
return await withRetry((path: string) => dbxClient.filesGetMetadata({ path }), [filePath], {
minTimeout: 3000,
maxTimeout: 12000,
})
}

Expand Down
36 changes: 12 additions & 24 deletions src/lib/withRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import pRetry from 'p-retry'
import type { StatusableError } from '@/errors/BaseServerError'
import { sleep } from '@/utils/sleep'

const RETRYABLE_STATUS_CODES = new Set<number>([
httpStatus.TOO_MANY_REQUESTS,
httpStatus.INTERNAL_SERVER_ERROR,
httpStatus.BAD_GATEWAY,
httpStatus.SERVICE_UNAVAILABLE,
httpStatus.GATEWAY_TIMEOUT,
])

export const withRetry = async <Args extends unknown[], R>(
fn: (...args: Args) => Promise<R>,
args: Args,
Expand Down Expand Up @@ -63,18 +71,8 @@ export const withRetry = async <Args extends unknown[], R>(

onFailedAttempt: (error: { error: unknown; attemptNumber: number; retriesLeft: number }) => {
if (error.error instanceof DropboxResponseError) {
const errorStatus = error.error.status
if (
errorStatus !== httpStatus.TOO_MANY_REQUESTS &&
errorStatus !== httpStatus.INTERNAL_SERVER_ERROR
)
return
}

if (
(error.error as StatusableError).status !== httpStatus.TOO_MANY_REQUESTS &&
(error.error as StatusableError).status !== httpStatus.INTERNAL_SERVER_ERROR
) {
if (!RETRYABLE_STATUS_CODES.has(error.error.status)) return
} else if (!RETRYABLE_STATUS_CODES.has((error.error as StatusableError).status)) {
return
}
console.warn(
Expand All @@ -84,21 +82,11 @@ export const withRetry = async <Args extends unknown[], R>(
},
shouldRetry: (error: { error: unknown; attemptNumber: number; retriesLeft: number }) => {
if (error.error instanceof DropboxResponseError) {
const errorStatus = error.error.status
return (
errorStatus === httpStatus.TOO_MANY_REQUESTS ||
errorStatus === httpStatus.INTERNAL_SERVER_ERROR
)
return RETRYABLE_STATUS_CODES.has(error.error.status)
}

// Typecasting because Copilot doesn't export an error class
const err = error.error as StatusableError

// Retry only if statusCode indicates a ratelimit or Internal Server Error
return (
err.status === httpStatus.TOO_MANY_REQUESTS ||
err.status === httpStatus.INTERNAL_SERVER_ERROR
)
return RETRYABLE_STATUS_CODES.has((error.error as StatusableError).status)
},
},
)
Expand Down
Loading