-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Add DB-based buffer storage support to DefaultJobQueuePlugin
Relates to #1137
- Loading branch information
1 parent
6f4a89f
commit f26ad4b
Showing
6 changed files
with
116 additions
and
28 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
25 changes: 0 additions & 25 deletions
25
packages/core/src/job-queue/job-buffer/sql-job-buffer-storage-strategy.ts
This file was deleted.
Oops, something went wrong.
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
18 changes: 18 additions & 0 deletions
18
packages/core/src/plugin/default-job-queue-plugin/job-record-buffer.entity.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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DeepPartial } from '@vendure/common/lib/shared-types'; | ||
import { Column, Entity } from 'typeorm'; | ||
|
||
import { VendureEntity } from '../../entity/base/base.entity'; | ||
import { JobConfig } from '../../job-queue/types'; | ||
|
||
@Entity() | ||
export class JobRecordBuffer extends VendureEntity { | ||
constructor(input: DeepPartial<JobRecordBuffer>) { | ||
super(input); | ||
} | ||
|
||
@Column() | ||
bufferId: string; | ||
|
||
@Column('simple-json') | ||
job: JobConfig<any>; | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/core/src/plugin/default-job-queue-plugin/sql-job-buffer-storage-strategy.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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Injector } from '../../common/injector'; | ||
import { TransactionalConnection } from '../../connection/transactional-connection'; | ||
import { Job } from '../../job-queue/job'; | ||
import { JobBufferStorageStrategy } from '../../job-queue/job-buffer/job-buffer-storage-strategy'; | ||
import { JobConfig } from '../../job-queue/types'; | ||
|
||
import { JobRecordBuffer } from './job-record-buffer.entity'; | ||
|
||
/** | ||
* @description | ||
* This stores the buffered jobs in the database. | ||
*/ | ||
export class SqlJobBufferStorageStrategy implements JobBufferStorageStrategy { | ||
private connection: TransactionalConnection; | ||
|
||
init(injector: Injector) { | ||
this.connection = injector.get(TransactionalConnection); | ||
} | ||
|
||
async add(bufferId: string, job: Job): Promise<Job> { | ||
await this.connection.getRepository(JobRecordBuffer).save( | ||
new JobRecordBuffer({ | ||
bufferId, | ||
job: this.toJobConfig(job), | ||
}), | ||
); | ||
|
||
return job; | ||
} | ||
|
||
async bufferSize(bufferIds?: string[]): Promise<{ [bufferId: string]: number }> { | ||
const rows = await this.connection | ||
.getRepository(JobRecordBuffer) | ||
.createQueryBuilder('record') | ||
.select(`COUNT(*)`, 'count') | ||
.addSelect(`record.bufferId`, 'bufferId') | ||
.groupBy('record.bufferId') | ||
.getRawMany(); | ||
|
||
const result: { [bufferId: string]: number } = {}; | ||
for (const row of rows) { | ||
result[row.bufferId] = +row.count; | ||
} | ||
return result; | ||
} | ||
|
||
async flush(bufferIds?: string[]): Promise<{ [bufferId: string]: Job[] }> { | ||
const selectQb = this.connection.getRepository(JobRecordBuffer).createQueryBuilder('record'); | ||
if (bufferIds?.length) { | ||
selectQb.where(`record.bufferId IN (:...bufferIds)`, { bufferIds }); | ||
} | ||
const rows = await selectQb.getMany(); | ||
const result: { [bufferId: string]: Job[] } = {}; | ||
for (const row of rows) { | ||
if (!result[row.bufferId]) { | ||
result[row.bufferId] = []; | ||
} | ||
result[row.bufferId].push(new Job(row.job)); | ||
} | ||
const deleteQb = this.connection.rawConnection.createQueryBuilder().delete().from(JobRecordBuffer); | ||
if (bufferIds?.length) { | ||
deleteQb.where(`bufferId IN (:...bufferIds)`, { bufferIds }); | ||
} | ||
await deleteQb.execute(); | ||
return result; | ||
} | ||
|
||
private toJobConfig(job: Job<any>): JobConfig<any> { | ||
return { | ||
...job, | ||
data: job.data, | ||
id: job.id ?? undefined, | ||
}; | ||
} | ||
} |
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,5 +1,7 @@ | ||
export * from './default-search-plugin/default-search-plugin'; | ||
export * from './default-job-queue-plugin/default-job-queue-plugin'; | ||
export * from './default-job-queue-plugin/job-record-buffer.entity'; | ||
export * from './default-job-queue-plugin/sql-job-buffer-storage-strategy'; | ||
export * from './vendure-plugin'; | ||
export * from './plugin-common.module'; | ||
export * from './plugin-utils'; |