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

refactor: bullmq #4261

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: update
  • Loading branch information
czy88840616 committed Jan 11, 2025
commit 9590ef25401e28d97576935350a37833be3f6b81
16 changes: 2 additions & 14 deletions packages/bullmq/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import { ConnectionOptions } from 'bullmq';
import { BullMQConfig } from './dist/index';
export * from './dist/index';
export { Job } from 'bullmq';
import { IQueueOptions, IWorkerOptions } from './dist/index';

declare module '@midwayjs/core/dist/interface' {
// bullmq 新引入了 worker 作为执行任务的实例,一个队列 queue 和 worker 中 connection, prefix 必须一致才能正常执行
// 所以 config 中 connection, prefix 单独配置
// eslint-disable-next-line
interface MidwayConfig {
bullmq?: {
connection: ConnectionOptions;
prefix?: string;
defaultQueueOptions?: IQueueOptions;
defaultWorkerOptions?: IWorkerOptions;
clearRepeatJobWhenStart?: boolean;
contextLoggerFormat?: (info: any) => string;
};
bullmq?: BullMQConfig;
}
}
4 changes: 2 additions & 2 deletions packages/bullmq/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@midwayjs/bullmq",
"version": "1.0.0",
"description": "midway component for bullmq",
"version": "0.0.1",
"description": "midway component for bullMQ",
"main": "dist/index.js",
"typings": "index.d.ts",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions packages/bullmq/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const bullmq = {
prefix: '{midway-bullmq}',
import { BullMQConfig } from '../interface';

export const bullmq: BullMQConfig = {
defaultPrefix: '{midway-bullmq}',
defaultQueueOptions: {
defaultJobOptions: {
removeOnComplete: 3,
Expand Down
26 changes: 25 additions & 1 deletion packages/bullmq/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@midwayjs/core';
import * as DefaultConfig from './config/config.default';
import { BullMQFramework } from './framework';
import { BULLMQ_QUEUE_KEY } from './constants';
import { BULLMQ_FLOW_PRODUCER_KEY, BULLMQ_QUEUE_KEY, BULLMQ_WORKER_KEY } from './constants';

@Configuration({
namespace: 'bullmq',
Expand Down Expand Up @@ -36,6 +36,30 @@ export class BullConfiguration {
return this.framework.getQueue(meta.queueName);
}
);

this.decoratorService.registerPropertyHandler(
BULLMQ_WORKER_KEY,
(
propertyName,
meta: {
queueName: string;
}
) => {
return this.framework.getWorker(meta.queueName);
}
);

this.decoratorService.registerPropertyHandler(
BULLMQ_FLOW_PRODUCER_KEY,
(
propertyName,
meta: {
producerName: string;
}
) => {
return this.framework.getFlowProducer(meta.producerName);
}
);
}

async onReady() {
Expand Down
2 changes: 2 additions & 0 deletions packages/bullmq/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// task
export const BULLMQ_QUEUE_KEY = 'bullmq:queue';
export const BULLMQ_PROCESSOR_KEY = 'bullmq:processor';
export const BULLMQ_WORKER_KEY = 'bullmq:worker';
export const BULLMQ_FLOW_PRODUCER_KEY = 'bullmq:flow-producer';
28 changes: 22 additions & 6 deletions packages/bullmq/src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import {
Scope,
ScopeEnum,
} from '@midwayjs/core';
import { IQueueOptions, IWorkerOptions } from './interface';
import { BULLMQ_PROCESSOR_KEY, BULLMQ_QUEUE_KEY } from './constants';
import { JobsOptions } from 'bullmq';
import {
BULLMQ_FLOW_PRODUCER_KEY,
BULLMQ_PROCESSOR_KEY,
BULLMQ_QUEUE_KEY,
BULLMQ_WORKER_KEY,
} from './constants';
import { QueueOptions, WorkerOptions, JobsOptions } from 'bullmq';

export function Processor(
queueName: string,
jobOptions?: JobsOptions,
workerOptions?: IWorkerOptions,
queueOptions?: IQueueOptions
workerOptions?: WorkerOptions,
queueOptions?: QueueOptions
): ClassDecorator {
return function (target: any) {
saveModule(BULLMQ_PROCESSOR_KEY, target);
Expand All @@ -23,8 +27,8 @@ export function Processor(
{
queueName,
jobOptions,
queueOptions,
workerOptions,
queueOptions,
},
target
);
Expand All @@ -38,3 +42,15 @@ export function InjectQueue(queueName: string): PropertyDecorator {
queueName,
});
}

export function InjectWorker(queueName: string): PropertyDecorator {
return createCustomPropertyDecorator(BULLMQ_WORKER_KEY, {
queueName,
});
}

export function InjectFlowProducer(producerName: string): PropertyDecorator {
return createCustomPropertyDecorator(BULLMQ_FLOW_PRODUCER_KEY, {
producerName,
});
}
Loading