Skip to content

Commit 1cdae90

Browse files
author
Theodore Li
committed
Sleep only between workspaces, not during single workspace
1 parent 62219a6 commit 1cdae90

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

packages/db/scripts/migrate-block-api-keys-to-byok.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,16 @@ const postgresClient = postgres(CONNECTION_STRING, {
221221
const db = drizzle(postgresClient)
222222

223223
// ---------- Throttle ----------
224-
const BATCH_SIZE = 1000
224+
const WORKSPACE_BATCH_SIZE = 1000
225225
const SLEEP_MS = 30_000
226-
let requestCount = 0
227-
let lastWorkspaceId = ''
226+
let workspaceCount = 0
228227

229-
async function throttle(workspaceId?: string) {
230-
if (workspaceId) lastWorkspaceId = workspaceId
231-
requestCount++
232-
if (requestCount % BATCH_SIZE === 0) {
228+
async function throttleBetweenWorkspaces(workspaceId: string, total: number) {
229+
workspaceCount++
230+
console.log(` [${workspaceCount}/${total}] Done with workspace ${workspaceId}`)
231+
if (workspaceCount % WORKSPACE_BATCH_SIZE === 0) {
233232
console.log(
234-
` [THROTTLE] ${requestCount} DB requests — last workspace: ${lastWorkspaceId} — sleeping ${SLEEP_MS / 1000}s`
233+
` [THROTTLE] ${workspaceCount}/${total} workspaces — sleeping ${SLEEP_MS / 1000}s`
235234
)
236235
await new Promise((r) => setTimeout(r, SLEEP_MS))
237236
}
@@ -396,7 +395,6 @@ async function run() {
396395
sql`, `
397396
)})${userFilter}`
398397
)
399-
await throttle(workspaceId)
400398

401399
console.log(`[Workspace ${workspaceId}] ${blocks.length} blocks`)
402400

@@ -462,7 +460,6 @@ async function run() {
462460
.from(workspaceEnvironment)
463461
.where(sql`${workspaceEnvironment.workspaceId} = ${workspaceId}`)
464462
.limit(1)
465-
await throttle()
466463
if (wsEnvRows[0]) {
467464
wsEnvVars = (wsEnvRows[0].variables as Record<string, string>) || {}
468465
}
@@ -478,7 +475,6 @@ async function run() {
478475
sql`, `
479476
)})`
480477
)
481-
await throttle()
482478
for (const row of personalRows) {
483479
personalEnvCache.set(row.userId, (row.variables as Record<string, string>) || {})
484480
}
@@ -542,7 +538,6 @@ async function run() {
542538
target: [workspaceBYOKKeys.workspaceId, workspaceBYOKKeys.providerId],
543539
})
544540
.returning({ id: workspaceBYOKKeys.id })
545-
await throttle()
546541

547542
if (result.length === 0) {
548543
console.log(` [SKIP] BYOK already exists for provider "${providerId}"`)
@@ -558,6 +553,7 @@ async function run() {
558553
}
559554

560555
console.log()
556+
await throttleBetweenWorkspaces(workspaceId, workspaceIds.length)
561557
}
562558

563559
// 3. Summary

0 commit comments

Comments
 (0)