Skip to content

Commit

Permalink
fix(lock): fix early release of lock
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore03109 committed Mar 31, 2024
1 parent 1c4b7ea commit 8f811e8
Showing 1 changed file with 65 additions and 57 deletions.
122 changes: 65 additions & 57 deletions src/middleware/routeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const attachRollbackRouteHandlerWrapper: RouteWrapper<
const { growthbook } = req

if (!growthbook) {
console.log("in error")
next(new Error("GrowthBook instance is undefined."))
return
}
Expand All @@ -151,6 +152,7 @@ export const attachRollbackRouteHandlerWrapper: RouteWrapper<
if (!isGitAvailable) return
try {
await lock(siteName)
console.log(`locked ${siteName}`)
} catch (err) {
next(err)
return
Expand Down Expand Up @@ -180,12 +182,14 @@ export const attachRollbackRouteHandlerWrapper: RouteWrapper<

if (results.isErr()) {
await unlock(siteName)
console.log(`unlocked ${siteName}`)
next(results.error)
return
}
const [stagingResult, stagingLiteResult] = results.value
if (!stagingResult.sha || !stagingLiteResult.sha) {
await unlock(siteName)
console.log(`unlocked ${siteName}`)
return
}

Expand All @@ -205,12 +209,14 @@ export const attachRollbackRouteHandlerWrapper: RouteWrapper<

if (result.isErr()) {
await unlock(siteName)
console.log(`unlocked ${siteName}`)
next(result.error)
return
}

if (!result.value.sha) {
await unlock(siteName)
console.log(`unlocked ${siteName}`)
return
}

Expand All @@ -237,6 +243,7 @@ export const attachRollbackRouteHandlerWrapper: RouteWrapper<
originalStagingCommitSha = currentStgCommitSha
} catch (err) {
await unlock(siteName)
console.log(`unlocked ${siteName}`)
logger.error(
`Failed to rollback repo ${siteName}: ${JSON.stringify(err)}`
)
Expand All @@ -245,78 +252,79 @@ export const attachRollbackRouteHandlerWrapper: RouteWrapper<
}
}

Promise.resolve(routeHandler(req, res, next)).catch(async (err: Error) => {
try {
if (shouldUseGitFileSystem) {
await backOff(
() =>
convertNeverThrowToPromise(
gitFileSystemService.rollback(
siteName,
originalStagingCommitSha,
STAGING_BRANCH
)
),
backoffOptions
)

if (shouldCheckStagingLite) {
// for quickie sites
Promise.resolve(routeHandler(req, res, next))
.then(async () => {
await unlock(siteName)
console.log(`unlocked ${siteName}`)
})
.catch(async (err: Error) => {
try {
if (shouldUseGitFileSystem) {
await backOff(
() =>
convertNeverThrowToPromise(
gitFileSystemService.rollback(
siteName,
originalStagingLiteCommitSha,
STAGING_LITE_BRANCH
originalStagingCommitSha,
STAGING_BRANCH
)
),
backoffOptions
)
}

await backOff(() => {
let pushRes = gitFileSystemService.push(
siteName,
STAGING_BRANCH,
true
)
if (originalStagingLiteCommitSha) {
pushRes = pushRes.andThen(() =>
gitFileSystemService.push(siteName, STAGING_LITE_BRANCH, true)
if (shouldCheckStagingLite) {
// for quickie sites
await backOff(
() =>
convertNeverThrowToPromise(
gitFileSystemService.rollback(
siteName,
originalStagingLiteCommitSha,
STAGING_LITE_BRANCH
)
),
backoffOptions
)
}

return convertNeverThrowToPromise(pushRes)
}, backoffOptions)
} else {
// Github flow
await backOff(
() =>
revertCommit(
originalStagingCommitSha,
await backOff(() => {
let pushRes = gitFileSystemService.push(
siteName,
accessToken,
STAGING_BRANCH
),
backoffOptions
STAGING_BRANCH,
true
)
if (originalStagingLiteCommitSha) {
pushRes = pushRes.andThen(() =>
gitFileSystemService.push(siteName, STAGING_LITE_BRANCH, true)
)
}

return convertNeverThrowToPromise(pushRes)
}, backoffOptions)
} else {
// Github flow
await backOff(
() =>
revertCommit(
originalStagingCommitSha,
siteName,
accessToken,
STAGING_BRANCH
),
backoffOptions
)
}
} catch (retryErr) {
await unlock(siteName)
console.log(`unlocked ${siteName}`)
logger.error(
`Failed to rollback repo ${siteName}: ${JSON.stringify(retryErr)}`
)
next(retryErr)
return
}
} catch (retryErr) {
await unlock(siteName)
logger.error(
`Failed to rollback repo ${siteName}: ${JSON.stringify(retryErr)}`
)
next(retryErr)
return
}
await unlock(siteName)
next(err)
})

try {
await unlock(siteName)
} catch (err) {
next(err)
}
console.log(`unlocked ${siteName}`)
next(err)
})
}

0 comments on commit 8f811e8

Please sign in to comment.