Skip to content

Commit

Permalink
Await for only the run of the update loop
Browse files Browse the repository at this point in the history
  • Loading branch information
thesan committed Dec 8, 2023
1 parent 6dc0b51 commit 00451d7
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/server-extension/resolvers/AssetsResolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export class DistributionBucketsCache {
public async init(intervalMs: number): Promise<void> {
this.logger.info(`Initializing distribution buckets cache with ${intervalMs}ms interval...`)
try {
await this.updateLoop(intervalMs)
await new Promise<void>((resolve) => {
this.updateLoop(intervalMs, resolve).catch((err) => {
throw err
})
})
} catch (err) {
console.error(err)
process.exit(-1)
Expand All @@ -47,8 +51,10 @@ export class DistributionBucketsCache {
})
}

private async updateLoop(intervalMs: number): Promise<void> {
private async updateLoop(intervalMs: number, onFirstRun: () => void): Promise<void> {
this.em = await globalEm
let isFirstRun = true

while (true) {
try {
this.logger.debug('Reloading distribution buckets and bags cache data...')
Expand All @@ -61,6 +67,11 @@ export class DistributionBucketsCache {
)
this.logger.debug(`Buckets cached: ${this.bucketsById.size}`)
this.logger.debug(`Bags cached: ${this.bucketIdsByBagId.size}`)

if (isFirstRun) {
isFirstRun = false
onFirstRun()
}
} catch (e) {
this.logger.error(`Cannot reload the cache: ${e instanceof Error ? e.message : ''}`)
}
Expand Down

0 comments on commit 00451d7

Please sign in to comment.