Merge pull request #13891 from Budibase/fix/app-migration-multi-tenant-failure

Fixing multi-tenant app migration issue
This commit is contained in:
Michael Drury 2024-06-07 23:03:02 +01:00 committed by GitHub
commit ca370ba214
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 43 deletions

View File

@ -21,6 +21,7 @@ let cleanupInterval: NodeJS.Timeout
async function cleanup() { async function cleanup() {
for (let queue of QUEUES) { for (let queue of QUEUES) {
await queue.clean(CLEANUP_PERIOD_MS, "completed") await queue.clean(CLEANUP_PERIOD_MS, "completed")
await queue.clean(CLEANUP_PERIOD_MS, "failed")
} }
} }

View File

@ -48,8 +48,6 @@ export async function checkMissingMigrations(
}, },
{ {
jobId: `${appId}_${latestMigration}`, jobId: `${appId}_${latestMigration}`,
removeOnComplete: true,
removeOnFail: true,
} }
) )

View File

@ -12,7 +12,8 @@ export async function processMigrations(
migrations: AppMigration[] migrations: AppMigration[]
) { ) {
console.log(`Processing app migration for "${appId}"`) console.log(`Processing app migration for "${appId}"`)
// have to wrap in context, this gets the tenant from the app ID
await context.doInAppContext(appId, async () => {
await locks.doWithLock( await locks.doWithLock(
{ {
name: LockName.APP_MIGRATION, name: LockName.APP_MIGRATION,
@ -20,8 +21,8 @@ export async function processMigrations(
resource: appId, resource: appId,
}, },
async () => { async () => {
await context.doInAppMigrationContext(appId, async () => {
try { try {
await context.doInAppMigrationContext(appId, async () => {
let currentVersion = await getAppMigrationVersion(appId) let currentVersion = await getAppMigrationVersion(appId)
const pendingMigrations = migrations const pendingMigrations = migrations
@ -53,13 +54,14 @@ export async function processMigrations(
}) })
currentVersion = id currentVersion = id
} }
})
} catch (err) { } catch (err) {
logging.logAlert("Failed to run app migration", err) logging.logAlert("Failed to run app migration", err)
throw err throw err
} }
})
} }
) )
console.log(`App migration for "${appId}" processed`) console.log(`App migration for "${appId}" processed`)
})
} }

View File

@ -1,9 +1,23 @@
import { queue } from "@budibase/backend-core" import { queue, logging } from "@budibase/backend-core"
import { Job } from "bull" import { Job } from "bull"
import { MIGRATIONS } from "./migrations" import { MIGRATIONS } from "./migrations"
import { processMigrations } from "./migrationsProcessor" import { processMigrations } from "./migrationsProcessor"
const appMigrationQueue = queue.createQueue(queue.JobQueue.APP_MIGRATION) const MAX_ATTEMPTS = 3
const appMigrationQueue = queue.createQueue(queue.JobQueue.APP_MIGRATION, {
jobOptions: {
attempts: MAX_ATTEMPTS,
removeOnComplete: true,
removeOnFail: true,
},
maxStalledCount: MAX_ATTEMPTS,
removeStalledCb: async (job: Job) => {
logging.logAlert(
`App migration failed, queue job ID: ${job.id} - reason: ${job.failedReason}`
)
},
})
appMigrationQueue.process(processMessage) appMigrationQueue.process(processMessage)
async function processMessage(job: Job) { async function processMessage(job: Job) {