Remove defaults and init

This commit is contained in:
Adria Navarro 2024-03-05 14:19:05 +01:00
parent b94d28b7d6
commit 8d87850765
2 changed files with 20 additions and 35 deletions

View File

@ -7,8 +7,6 @@ import { JobQueue, createQueue } from "../queue"
import * as context from "../context" import * as context from "../context"
import * as dbUtils from "../db" import * as dbUtils from "../db"
const DEFAULT_WRITE_RATE_MS = 10000
let CACHE: BaseCache | null = null let CACHE: BaseCache | null = null
async function getCache() { async function getCache() {
if (!CACHE) { if (!CACHE) {
@ -29,33 +27,27 @@ export const docWritethroughProcessorQueue = createQueue<ProcessDocMessage>(
JobQueue.DOC_WRITETHROUGH_QUEUE JobQueue.DOC_WRITETHROUGH_QUEUE
) )
let _init = false docWritethroughProcessorQueue.process(async message => {
export const init = () => { const { tenantId, cacheKeyPrefix } = message.data
if (_init) { await context.doInTenant(tenantId, async () => {
return const lockResponse = await locks.doWithLock(
} {
docWritethroughProcessorQueue.process(async message => { type: LockType.TRY_ONCE,
const { tenantId, cacheKeyPrefix } = message.data name: LockName.PERSIST_WRITETHROUGH,
await context.doInTenant(tenantId, async () => { resource: cacheKeyPrefix,
const lockResponse = await locks.doWithLock( ttl: 15000,
{ },
type: LockType.TRY_ONCE, async () => {
name: LockName.PERSIST_WRITETHROUGH, await persistToDb(message.data)
resource: cacheKeyPrefix, console.log("DocWritethrough persisted", { data: message.data })
ttl: 15000,
},
async () => {
await persistToDb(message.data)
}
)
if (!lockResponse.executed) {
console.log(`Ignoring redlock conflict in write-through cache`)
} }
}) )
if (!lockResponse.executed) {
console.log(`Ignoring redlock conflict in write-through cache`)
}
}) })
_init = true })
}
export async function persistToDb({ export async function persistToDb({
dbName, dbName,
@ -97,11 +89,7 @@ export class DocWritethrough {
private cacheKeyPrefix: string private cacheKeyPrefix: string
constructor( constructor(db: Database, docId: string, writeRateMs: number) {
db: Database,
docId: string,
writeRateMs: number = DEFAULT_WRITE_RATE_MS
) {
this.db = db this.db = db
this._docId = docId this._docId = docId
this.writeRateMs = writeRateMs this.writeRateMs = writeRateMs

View File

@ -5,7 +5,6 @@ import _ from "lodash"
import { import {
DocWritethrough, DocWritethrough,
docWritethroughProcessorQueue, docWritethroughProcessorQueue,
init,
} from "../docWritethrough" } from "../docWritethrough"
import InMemoryQueue from "../../queue/inMemoryQueue" import InMemoryQueue from "../../queue/inMemoryQueue"
@ -45,8 +44,6 @@ describe("docWritethrough", () => {
}, {} as Record<string, any>) }, {} as Record<string, any>)
} }
beforeAll(() => init())
beforeEach(async () => { beforeEach(async () => {
resetTime() resetTime()
documentId = structures.uuid() documentId = structures.uuid()