Skip to content

Commit

Permalink
feat(core): Use batching when updating collection filters
Browse files Browse the repository at this point in the history
Solves server becoming unresponsive when updating a collection affecting a large number of variants
  • Loading branch information
michaelbromley committed Jun 3, 2019
1 parent 40c5946 commit 325b807
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,20 @@ export class FulltextSearchService implements SearchService {

async updateVariantsById(ctx: RequestContext, ids: ID[]) {
if (ids.length) {
this.taskQueue.push(async () => {
const updatedVariants = await this.connection.getRepository(ProductVariant).findByIds(ids, {
const BATCH_SIZE = 100;
const batches = Math.ceil(ids.length / BATCH_SIZE);
for (let i = 0; i < batches; i++) {
const begin = i * BATCH_SIZE;
const end = begin + BATCH_SIZE;
Logger.verbose(`Updating ids from index ${begin} to ${end}`);
const batch = ids.slice(begin, end);
const updatedVariants = await this.connection.getRepository(ProductVariant).findByIds(batch, {
relations: this.variantRelations,
});
await this.saveSearchIndexItems(ctx, updatedVariants);
});
this.taskQueue.push(async () => {
await this.saveSearchIndexItems(ctx, updatedVariants);
});
}
}
}

Expand Down
18 changes: 7 additions & 11 deletions packages/core/src/service/services/collection.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { OnModuleInit } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm';
import {
CollectionBreadcrumb,
ConfigurableOperation,
CreateCollectionInput,
MoveCollectionInput,
UpdateCollectionInput,
} from '@vendure/common/lib/generated-types';
import { ConfigurableOperation, CreateCollectionInput, MoveCollectionInput, UpdateCollectionInput } from '@vendure/common/lib/generated-types';
import { pick } from '@vendure/common/lib/pick';
import { ROOT_COLLECTION_NAME } from '@vendure/common/lib/shared-constants';
import { ID, PaginatedList } from '@vendure/common/lib/shared-types';
Expand Down Expand Up @@ -58,9 +52,11 @@ export class CollectionService implements OnModuleInit {
});
for (const collection of collections) {
const affectedVariantIds = await this.applyCollectionFilters(collection);
this.eventBus.publish(
new CollectionModificationEvent(event.ctx, collection, affectedVariantIds),
);
if (affectedVariantIds.length) {
this.eventBus.publish(
new CollectionModificationEvent(event.ctx, collection, affectedVariantIds),
);
}
}
});
}
Expand Down Expand Up @@ -349,8 +345,8 @@ export class CollectionService implements OnModuleInit {
...ancestorFilters,
...(collection.filters || []),
]);
await this.connection.getRepository(Collection).save(collection);
const postIds = collection.productVariants.map(v => v.id);
await this.connection.getRepository(Collection).save(collection, { chunk: Math.ceil(collection.productVariants.length / 500) });

const preIdsSet = new Set(preIds);
const postIdsSet = new Set(postIds);
Expand Down

0 comments on commit 325b807

Please sign in to comment.