Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⌛ Await for bucketIdsByBagId to be initialized #276

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mail-scheduler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export async function main() {
main()
.then(() => {
console.log('Email delivery finished')
process.exit(0)
})
.catch((err) => {
console.error('Email delivery failed', err)
process.exit(1)
})
27 changes: 18 additions & 9 deletions src/server-extension/resolvers/AssetsResolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ export class DistributionBucketsCache {
this.logger = rootLogger.child('buckets-cache')
}

public init(intervalMs: number): void {
public async init(intervalMs: number): Promise<void> {
this.logger.info(`Initializing distribution buckets cache with ${intervalMs}ms interval...`)
this.updateLoop(intervalMs)
.then(() => {
/* Do nothing */
})
.catch((err) => {
console.error(err)
process.exit(-1)
try {
await new Promise<void>((resolve) => {
this.updateLoop(intervalMs, resolve).catch((err) => {
throw err
})
})
} catch (err) {
console.error(err)
process.exit(-1)
}
}

public getBucketsByBagId(bagId: string): DistributionBucketCachedData[] {
Expand All @@ -49,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 @@ -63,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
2 changes: 1 addition & 1 deletion src/server-extension/resolvers/AssetsResolver/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function getAssetUrls(

if (!distributionBucketsCache) {
distributionBucketsCache = new DistributionBucketsCache()
distributionBucketsCache.init(6000)
await distributionBucketsCache.init(6000)
}

const buckets = distributionBucketsCache.getBucketsByBagId(bagId)
Expand Down
Loading