-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(elasticsearch-plugin): Use WorkerService
- Loading branch information
1 parent
16ab03d
commit 7df2b9c
Showing
5 changed files
with
110 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 69 additions & 91 deletions
160
packages/elasticsearch-plugin/src/elasticsearch-index.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,98 @@ | ||
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. | ||
*/ | ||
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); | ||
}, | ||
}); | ||
}, | ||
}); | ||
} | ||
|
||
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, | ||
}); | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters