Do not use lock
This commit is contained in:
parent
2b25f9f0cb
commit
4fe7e67dd5
|
@ -1,7 +1,6 @@
|
|||
import BaseCache from "./base"
|
||||
import { getDocWritethroughClient } from "../redis/init"
|
||||
import { AnyDocument, Database, LockName, LockType } from "@budibase/types"
|
||||
import * as locks from "../redis/redlockImpl"
|
||||
import { AnyDocument, Database } from "@budibase/types"
|
||||
|
||||
import { JobQueue, createQueue } from "../queue"
|
||||
import * as context from "../context"
|
||||
|
@ -17,7 +16,6 @@ async function getCache() {
|
|||
}
|
||||
|
||||
interface ProcessDocMessage {
|
||||
tenantId: string
|
||||
dbName: string
|
||||
docId: string
|
||||
cacheKeyPrefix: string
|
||||
|
@ -28,34 +26,8 @@ export const docWritethroughProcessorQueue = createQueue<ProcessDocMessage>(
|
|||
)
|
||||
|
||||
docWritethroughProcessorQueue.process(async message => {
|
||||
const { tenantId, cacheKeyPrefix } = message.data
|
||||
await context.doInTenant(tenantId, async () => {
|
||||
const lockResponse = await locks.doWithLock(
|
||||
{
|
||||
type: LockType.TRY_ONCE,
|
||||
name: LockName.PERSIST_WRITETHROUGH,
|
||||
resource: cacheKeyPrefix,
|
||||
ttl: 15000,
|
||||
},
|
||||
async () => {
|
||||
await persistToDb(message.data)
|
||||
console.log("DocWritethrough persisted", { data: message.data })
|
||||
}
|
||||
)
|
||||
|
||||
if (!lockResponse.executed) {
|
||||
if (
|
||||
lockResponse.reason !==
|
||||
locks.UnsuccessfulRedlockExecutionReason.LockTakenWithTryOnce
|
||||
) {
|
||||
console.error("Error persisting docWritethrough", {
|
||||
data: message.data,
|
||||
})
|
||||
throw "Error persisting docWritethrough"
|
||||
}
|
||||
console.log(`Ignoring redlock conflict in write-through cache`)
|
||||
}
|
||||
})
|
||||
await persistToDb(message.data)
|
||||
console.log("DocWritethrough persisted", { data: message.data })
|
||||
})
|
||||
|
||||
export async function persistToDb({
|
||||
|
@ -94,7 +66,6 @@ export class DocWritethrough {
|
|||
private db: Database
|
||||
private _docId: string
|
||||
private writeRateMs: number
|
||||
private tenantId: string
|
||||
|
||||
private cacheKeyPrefix: string
|
||||
|
||||
|
@ -103,7 +74,6 @@ export class DocWritethrough {
|
|||
this._docId = docId
|
||||
this.writeRateMs = writeRateMs
|
||||
this.cacheKeyPrefix = `${this.db.name}:${this.docId}`
|
||||
this.tenantId = context.getTenantId()
|
||||
}
|
||||
|
||||
get docId() {
|
||||
|
@ -117,7 +87,6 @@ export class DocWritethrough {
|
|||
|
||||
docWritethroughProcessorQueue.add(
|
||||
{
|
||||
tenantId: this.tenantId,
|
||||
dbName: this.db.name,
|
||||
docId: this.docId,
|
||||
cacheKeyPrefix: this.cacheKeyPrefix,
|
||||
|
|
|
@ -47,9 +47,7 @@ describe("docWritethrough", () => {
|
|||
beforeEach(async () => {
|
||||
resetTime()
|
||||
documentId = structures.uuid()
|
||||
await config.doInTenant(async () => {
|
||||
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
|
||||
})
|
||||
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
|
||||
})
|
||||
|
||||
it("patching will not persist if timeout does not hit", async () => {
|
||||
|
|
Loading…
Reference in New Issue