-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: package bullmq * fix: use bullmq-logger --------- Co-authored-by: gqc <[email protected]>
- Loading branch information
Showing
22 changed files
with
640 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/bull/test/fixtures/base-app/src/task/limit.task.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,15 @@ | ||
import { App, Inject, sleep } from '@midwayjs/core'; | ||
import { Processor, Application } from '../../../../../src'; | ||
|
||
@Processor('limit',2,{},{limiter: { max: 3, duration: 1000 }}) | ||
export class QueueTask { | ||
@App() | ||
app: Application; | ||
|
||
@Inject() | ||
logger; | ||
|
||
async execute(params) { | ||
await sleep(3*1000) | ||
} | ||
} |
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 @@ | ||
# Change Log |
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,9 @@ | ||
# @midwayjs/bullmq | ||
|
||
this is a sub package for midway. | ||
|
||
Document: [https://midwayjs.org](https://midwayjs.org) | ||
|
||
## License | ||
|
||
[MIT]((https://github.com/midwayjs/midway/blob/master/LICENSE)) |
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,20 @@ | ||
import { ConnectionOptions } from 'bullmq'; | ||
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; | ||
}; | ||
} | ||
} |
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,8 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['<rootDir>/test/fixtures'], | ||
coveragePathIgnorePatterns: ['<rootDir>/test/'], | ||
setupFilesAfterEnv: ['./jest.setup.js'], | ||
coverageProvider: 'v8', | ||
}; |
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,2 @@ | ||
process.env.MIDWAY_TS_MODE = 'true'; | ||
jest.setTimeout(30000); |
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,37 @@ | ||
{ | ||
"name": "@midwayjs/bullmq", | ||
"version": "1.0.0", | ||
"description": "midway component for bullmq", | ||
"main": "dist/index.js", | ||
"typings": "index.d.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand", | ||
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit" | ||
}, | ||
"keywords": [ | ||
"midway", | ||
"IoC", | ||
"task", | ||
"bullmq", | ||
"plugin" | ||
], | ||
"author": "guo qicong", | ||
"files": [ | ||
"dist/**/*.js", | ||
"dist/**/*.d.ts", | ||
"index.d.ts" | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@midwayjs/core": "^3.19.0", | ||
"@midwayjs/mock": "^3.19.2" | ||
}, | ||
"dependencies": { | ||
"bullmq": "5.34.6" | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"repository": "https://github.com/midwayjs/midway.git" | ||
} |
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,26 @@ | ||
export const bullmq = { | ||
prefix: '{midway-bullmq}', | ||
defaultQueueOptions: { | ||
defaultJobOptions: { | ||
removeOnComplete: 3, | ||
removeOnFail: 10, | ||
}, | ||
}, | ||
defaultWorkerOptions: { | ||
concurrency: 1, | ||
}, | ||
clearRepeatJobWhenStart: true, | ||
contextLoggerApplyLogger: 'bullMQLogger', | ||
contextLoggerFormat: info => { | ||
const { jobId, from } = info.ctx; | ||
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${from.name}] ${info.message}`; | ||
}, | ||
}; | ||
|
||
export const midwayLogger = { | ||
clients: { | ||
bullMQLogger: { | ||
fileLogName: 'midway-bullmq.log', | ||
}, | ||
}, | ||
}; |
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,44 @@ | ||
import { | ||
Configuration, | ||
Init, | ||
Inject, | ||
MidwayDecoratorService, | ||
} from '@midwayjs/core'; | ||
import * as DefaultConfig from './config/config.default'; | ||
import { BullMQFramework } from './framework'; | ||
import { BULLMQ_QUEUE_KEY } from './constants'; | ||
|
||
@Configuration({ | ||
namespace: 'bullmq', | ||
importConfigs: [ | ||
{ | ||
default: DefaultConfig, | ||
}, | ||
], | ||
}) | ||
export class BullConfiguration { | ||
@Inject() | ||
framework: BullMQFramework; | ||
|
||
@Inject() | ||
decoratorService: MidwayDecoratorService; | ||
|
||
@Init() | ||
async init() { | ||
this.decoratorService.registerPropertyHandler( | ||
BULLMQ_QUEUE_KEY, | ||
( | ||
propertyName, | ||
meta: { | ||
queueName: string; | ||
} | ||
) => { | ||
return this.framework.getQueue(meta.queueName); | ||
} | ||
); | ||
} | ||
|
||
async onReady() { | ||
this.framework.loadConfig(); | ||
} | ||
} |
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,3 @@ | ||
// task | ||
export const BULLMQ_QUEUE_KEY = 'bullmq:queue'; | ||
export const BULLMQ_PROCESSOR_KEY = 'bullmq:processor'; |
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,40 @@ | ||
import { | ||
createCustomPropertyDecorator, | ||
Provide, | ||
saveClassMetadata, | ||
saveModule, | ||
Scope, | ||
ScopeEnum, | ||
} from '@midwayjs/core'; | ||
import { IQueueOptions, IWorkerOptions } from './interface'; | ||
import { BULLMQ_PROCESSOR_KEY, BULLMQ_QUEUE_KEY } from './constants'; | ||
import { JobsOptions } from 'bullmq'; | ||
|
||
export function Processor( | ||
queueName: string, | ||
jobOptions?: JobsOptions, | ||
workerOptions?: IWorkerOptions, | ||
queueOptions?: IQueueOptions | ||
): ClassDecorator { | ||
return function (target: any) { | ||
saveModule(BULLMQ_PROCESSOR_KEY, target); | ||
saveClassMetadata( | ||
BULLMQ_PROCESSOR_KEY, | ||
{ | ||
queueName, | ||
jobOptions, | ||
queueOptions, | ||
workerOptions, | ||
}, | ||
target | ||
); | ||
Provide()(target); | ||
Scope(ScopeEnum.Request)(target); | ||
}; | ||
} | ||
|
||
export function InjectQueue(queueName: string): PropertyDecorator { | ||
return createCustomPropertyDecorator(BULLMQ_QUEUE_KEY, { | ||
queueName, | ||
}); | ||
} |
Oops, something went wrong.