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

fix(mongo): retry only 3 times if ensuring indexes fails #3272

Merged
merged 7 commits into from
Jul 2, 2022
11 changes: 8 additions & 3 deletions packages/mongodb/src/MongoSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoDriver> {
await Promise.all(promises);
}

async ensureIndexes(options: EnsureIndexesOptions = {}, retryLimit = 3): Promise<void> {
async ensureIndexes(options: EnsureIndexesOptions = {}): Promise<void> {
options.ensureCollections ??= true;
options.retryLimit ??= 3;

if (options.ensureCollections) {
await this.createSchema({ ensureIndexes: false });
Expand Down Expand Up @@ -103,11 +104,14 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoDriver> {
const skipIndexes = res.filter(r => r.status === 'fulfilled').map((r: Dictionary, id) => ({ collection: promises[id][0], indexName: r.value }));
const collectionsWithFailedIndexes = res.filter(r => r.status === 'rejected').map((r: Dictionary, id) => promises[id][0]);
await this.dropIndexes({ skipIndexes, collectionsWithFailedIndexes });
if (retryLimit === 1) {
if (options.retryLimit === 1) {
B4nan marked this conversation as resolved.
Show resolved Hide resolved
const failedIndexes = res.filter(r => r.status === 'rejected').map((r: Dictionary, id) => `${promises[id][0]} - ${r.reason}`);
throw new Error(`Failed to create indexes: ${failedIndexes.join(', ')}`);
}
await this.ensureIndexes({ retry: collectionsWithFailedIndexes }, retryLimit - 1);
await this.ensureIndexes({
B4nan marked this conversation as resolved.
Show resolved Hide resolved
retry: collectionsWithFailedIndexes,
retryLimit: options.retryLimit - 1,
});
}
}

Expand Down Expand Up @@ -185,4 +189,5 @@ export interface CreateSchemaOptions {
export interface EnsureIndexesOptions {
ensureCollections?: boolean;
retry?: boolean | string[];
retryLimit?: number;
}