|
|
@@ -1,15 +1,34 @@
|
|
|
import { Inject, Injectable, OnModuleDestroy } from '@nestjs/common';
|
|
|
import { ClientProxy } from '@nestjs/microservices';
|
|
|
-import { ID, Job, JobService, Logger, Product, ProductVariant, RequestContext, VENDURE_WORKER_CLIENT } from '@vendure/core';
|
|
|
+import {
|
|
|
+ ID,
|
|
|
+ Job,
|
|
|
+ JobReporter,
|
|
|
+ JobService,
|
|
|
+ Logger,
|
|
|
+ Product,
|
|
|
+ ProductVariant,
|
|
|
+ RequestContext,
|
|
|
+ WorkerService,
|
|
|
+} from '@vendure/core';
|
|
|
|
|
|
-import { Message } from './constants';
|
|
|
import { ReindexMessageResponse } from './indexer.controller';
|
|
|
+import { ReindexMessage, UpdateProductOrVariantMessage, UpdateVariantsByIdMessage } from './types';
|
|
|
|
|
|
@Injectable()
|
|
|
-export class ElasticsearchIndexService implements OnModuleDestroy {
|
|
|
+export class ElasticsearchIndexService {
|
|
|
+ constructor(private workerService: WorkerService, private jobService: JobService) {}
|
|
|
|
|
|
- constructor(@Inject(VENDURE_WORKER_CLIENT) private readonly client: ClientProxy,
|
|
|
- private jobService: JobService) {}
|
|
|
+ reindex(ctx: RequestContext): Job {
|
|
|
+ return this.jobService.createJob({
|
|
|
+ name: 'reindex',
|
|
|
+ singleInstance: true,
|
|
|
+ work: async reporter => {
|
|
|
+ Logger.verbose(`sending reindex message`);
|
|
|
+ this.workerService.send(new ReindexMessage({ ctx })).subscribe(this.createObserver(reporter));
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Updates the search index only for the affected entities.
|
|
|
@@ -17,16 +36,18 @@ export class ElasticsearchIndexService implements OnModuleDestroy {
|
|
|
updateProductOrVariant(ctx: RequestContext, updatedEntity: Product | ProductVariant) {
|
|
|
return this.jobService.createJob({
|
|
|
name: 'update-index',
|
|
|
- work: async () => {
|
|
|
- if (updatedEntity instanceof Product) {
|
|
|
- return this.client.send(Message.UpdateProductOrVariant, { ctx, productId: updatedEntity.id })
|
|
|
- .toPromise()
|
|
|
- .catch(err => Logger.error(err));
|
|
|
- } else {
|
|
|
- return this.client.send(Message.UpdateProductOrVariant, { ctx, variantId: updatedEntity.id })
|
|
|
- .toPromise()
|
|
|
- .catch(err => Logger.error(err));
|
|
|
- }
|
|
|
+ work: reporter => {
|
|
|
+ const data =
|
|
|
+ updatedEntity instanceof Product
|
|
|
+ ? { ctx, productId: updatedEntity.id }
|
|
|
+ : { ctx, variantId: updatedEntity.id };
|
|
|
+ this.workerService.send(new UpdateProductOrVariantMessage(data)).subscribe({
|
|
|
+ complete: () => reporter.complete(true),
|
|
|
+ error: err => {
|
|
|
+ Logger.error(err);
|
|
|
+ reporter.complete(false);
|
|
|
+ },
|
|
|
+ });
|
|
|
},
|
|
|
});
|
|
|
}
|
|
|
@@ -34,87 +55,44 @@ export class ElasticsearchIndexService implements OnModuleDestroy {
|
|
|
updateVariantsById(ctx: RequestContext, ids: ID[]) {
|
|
|
return this.jobService.createJob({
|
|
|
name: 'update-index',
|
|
|
- work: async reporter => {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- Logger.verbose(`sending reindex message`);
|
|
|
- let total: number | undefined;
|
|
|
- let duration = 0;
|
|
|
- let completed = 0;
|
|
|
- this.client.send<ReindexMessageResponse>(Message.UpdateVariantsById, { ctx, ids })
|
|
|
- .subscribe({
|
|
|
- next: response => {
|
|
|
- if (!total) {
|
|
|
- total = response.total;
|
|
|
- }
|
|
|
- duration = response.duration;
|
|
|
- completed = response.completed;
|
|
|
- const progress = Math.ceil((completed / total) * 100);
|
|
|
- reporter.setProgress(progress);
|
|
|
- },
|
|
|
- complete: () => {
|
|
|
- resolve({
|
|
|
- success: true,
|
|
|
- indexedItemCount: total,
|
|
|
- timeTaken: duration,
|
|
|
- });
|
|
|
- },
|
|
|
- error: (err) => {
|
|
|
- Logger.error(JSON.stringify(err));
|
|
|
- resolve({
|
|
|
- success: false,
|
|
|
- indexedItemCount: 0,
|
|
|
- timeTaken: 0,
|
|
|
- });
|
|
|
- },
|
|
|
- });
|
|
|
- });
|
|
|
+ work: reporter => {
|
|
|
+ Logger.verbose(`sending reindex message`);
|
|
|
+ this.workerService
|
|
|
+ .send(new UpdateVariantsByIdMessage({ ctx, ids }))
|
|
|
+ .subscribe(this.createObserver(reporter));
|
|
|
},
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- reindex(ctx: RequestContext): Job {
|
|
|
- return this.jobService.createJob({
|
|
|
- name: 'reindex',
|
|
|
- singleInstance: true,
|
|
|
- work: async reporter => {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- Logger.verbose(`sending reindex message`);
|
|
|
- let total: number | undefined;
|
|
|
- let duration = 0;
|
|
|
- let completed = 0;
|
|
|
- this.client.send<ReindexMessageResponse>(Message.Reindex, { ctx })
|
|
|
- .subscribe({
|
|
|
- next: response => {
|
|
|
- if (!total) {
|
|
|
- total = response.total;
|
|
|
- }
|
|
|
- duration = response.duration;
|
|
|
- completed = response.completed;
|
|
|
- const progress = Math.ceil((completed / total) * 100);
|
|
|
- reporter.setProgress(progress);
|
|
|
- },
|
|
|
- complete: () => {
|
|
|
- resolve({
|
|
|
- success: true,
|
|
|
- indexedItemCount: total,
|
|
|
- timeTaken: duration,
|
|
|
- });
|
|
|
- },
|
|
|
- error: (err) => {
|
|
|
- Logger.error(JSON.stringify(err));
|
|
|
- resolve({
|
|
|
- success: false,
|
|
|
- indexedItemCount: 0,
|
|
|
- timeTaken: 0,
|
|
|
- });
|
|
|
- },
|
|
|
- });
|
|
|
+ private createObserver(reporter: JobReporter) {
|
|
|
+ let total: number | undefined;
|
|
|
+ let duration = 0;
|
|
|
+ let completed = 0;
|
|
|
+ return {
|
|
|
+ next: (response: ReindexMessageResponse) => {
|
|
|
+ if (!total) {
|
|
|
+ total = response.total;
|
|
|
+ }
|
|
|
+ duration = response.duration;
|
|
|
+ completed = response.completed;
|
|
|
+ const progress = Math.ceil((completed / total) * 100);
|
|
|
+ reporter.setProgress(progress);
|
|
|
+ },
|
|
|
+ complete: () => {
|
|
|
+ reporter.complete({
|
|
|
+ success: true,
|
|
|
+ indexedItemCount: total,
|
|
|
+ timeTaken: duration,
|
|
|
});
|
|
|
},
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- onModuleDestroy(): any {
|
|
|
- this.client.close();
|
|
|
+ error: (err: any) => {
|
|
|
+ Logger.error(JSON.stringify(err));
|
|
|
+ reporter.complete({
|
|
|
+ success: false,
|
|
|
+ indexedItemCount: 0,
|
|
|
+ timeTaken: 0,
|
|
|
+ });
|
|
|
+ },
|
|
|
+ };
|
|
|
}
|
|
|
}
|