-
Notifications
You must be signed in to change notification settings - Fork 577
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
feat: add package bullmq #4257
Merged
Merged
feat: add package bullmq #4257
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接用 Worker 的概念好了,Processor在 bullmq里已经没有了。