Skip to content

Commit

Permalink
refactor: use less memory during list sync
Browse files Browse the repository at this point in the history
  • Loading branch information
adalinesimonian committed Mar 7, 2024
1 parent 8f0c886 commit 56f5cd9
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions packages/uib-ord-sync/src/providers/article-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,49 +64,43 @@ export class ArticleSyncService {
);

const idSet: Set<number> = new Set();
let queuedTotal = 0;

const promises = articleList.reduce((promises, rawMetadata, index) => {
for (let index = 0; index < articleList.length; index++) {
const rawMetadata = articleList[index];
const metadata = convertRawArticleMetadata(rawMetadata);
const existing = existingMetadata.get(metadata.articleId);
idSet.add(metadata.articleId);

!prod &&
process.stdout.write(
`[${dictionary}] Processing article ${index} of ${articleList.length}\r`,
`[${dictionary}] Processing article ${index + 1} of ${articleList.length}\r`,
);

// skip if the article is already up to date
if (
existing &&
existing.revision === metadata.revision &&
Math.abs(existing.updatedAt.getTime() - metadata.updatedAt.getTime()) <
1000 // allow for 1s error margin due to millisecond parsing precision
1000 // allow for 1 second difference due to millisecond precision
) {
return promises;
continue;
}

// skip if the article is already in the queue
if (jobs.has(metadata.articleId)) {
return promises;
continue;
}

// queue the article fetch job
promises.push(
(async () => {
await queue.add(JobQueue.FetchArticle, {
dictionary,
metadata,
});
this.#logger.verbose(
`[${dictionary}] Queued article fetch for ${metadata.articleId} (${metadata.primaryLemma})`,
);
})(),
);
await queue.add(JobQueue.FetchArticle, {
dictionary,
metadata,
});

return promises;
}, [] as Promise<void>[]);
queuedTotal++;

await Promise.all(promises);
this.#logger.verbose(
`[${dictionary}] Queued article fetch for ${metadata.articleId} (${metadata.primaryLemma})`,
);
}

// remove articles that no longer exist
for (const [id, metadata] of existingMetadata) {
Expand All @@ -121,7 +115,7 @@ export class ArticleSyncService {
}

this.#logger.log(
`[${dictionary}] Queued ${promises.length} article fetch jobs`,
`[${dictionary}] Queued ${queuedTotal} article fetch jobs`,
);
}

Expand Down

0 comments on commit 56f5cd9

Please sign in to comment.