-
Notifications
You must be signed in to change notification settings - Fork 576
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 new bull component #2275
Conversation
eb93ce7
to
0401eb8
Compare
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.
最大的设计点: 定时任务也有点像Controller里面的get、post方法一样,比如需求发现要加一个定时任务,那我偏向直接一个类的方法函数,然后在这个函数上面加一个定时任务的装饰器类。
packages/bull/README.md
Outdated
## 安装方法 | ||
|
||
```bash | ||
tnpm install @midwayjs/bull -S |
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.
不是tnpm
packages/bull/README.md
Outdated
export const bull = { | ||
defaultQueueOptions: { | ||
redis: `redis://127.0.0.1:6379`, | ||
prefix: 'midway-task', |
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.
此处我发现,还是默认用: prefix: '{midway-task}' 这样用户不会碰到问题
packages/bull/README.md
Outdated
定义一个任务处理器 | ||
|
||
```typescript | ||
@Processor('test') |
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.
这里还需要用@provide吗?
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.
不用了。
packages/bull/README.md
Outdated
定时执行任务: | ||
|
||
```typescript | ||
@Processor('test', { |
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.
这个改造,感觉脱离原本的模式了? 原来我一个类可以有多个任务,这样改,一个类只能一个任务了吗?
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.
建议不要这样改,比如我一个类里面,想要让某个地方定时执行,我本来只要在这个方法前面加一下装饰器,就好了。现在我得不停的抽离出这样一个类。
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.
考虑再三,在方法上加其实是不合理的。
原因有几个:
1、普通的类方法如果也能被标记为任务执行,这是个奇怪的事情
比如:
@Provide()
class UserService {
invoke() {
// ...
}
@task()
invokeTask() {
}
}
在框架侧无法限定用户写的位置,尽可能避免比较好。
2、作用域的一致性问题
task 的类要求是请求作用域,而普通类可以调整作用域为单例等,在不确定调用关系的情况下,这个时候如果出现 inject ctx 可能有undefined的问题。
3、跟 API 相关的
现在新设计的 Processor 的部分,虽然逻辑和以前 task 很像,稍微还是有点区别,现在应该对应于原来的 @Queue 装饰器,移除了 Task 装饰器。
获取一个队列
以前:
queueService.getQueueTask(`HelloTask`, 'task')
现在:
queueService.getQueue('task')
旧的应该类会重复,因为以字符串作为 key。
动态添加一个任务
新:
@Processor()
class RepeatProcessor {}
framework.addProcessor(RepeatProcessor, 'task')
大概差异就是这样。
@@ -0,0 +1,24 @@ | |||
export const bull = { | |||
defaultQueueOptions: { | |||
prefix: '${midway-bull}', |
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.
直接改成{midway-bull},不用加$符号
concurrency?: number, | ||
jobOptions?: JobOptions | ||
): ClassDecorator; | ||
export function Processor( |
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.
对于改用类的装饰器持保留意见。在实际业务场景中,还是原来支持类函数装饰器的方式比较好。
packages/bull/src/decorator.ts
Outdated
}); | ||
} | ||
// | ||
// export function Queue(queueOptions?: QueueOptions): ClassDecorator; |
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.
不用的删除一下
} | ||
} | ||
|
||
protected async beforeStop() { |
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.
这个方法为什么是protected?
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.
Framework 继承而来。
const service = await ctx.requestContext.getAsync<IProcessor>( | ||
processor as any | ||
); | ||
const fn = await this.applyMiddleware(async ctx => { |
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.
这块middleware 的写法readme里面好像没看到,不知道用户层这块的middleware怎么样的
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.
和普通中间件一样。
Codecov ReportBase: 83.05% // Head: 83.05% // No change to project coverage 👍
Additional details and impacted files@@ Coverage Diff @@
## main #2275 +/- ##
=======================================
Coverage 83.05% 83.05%
=======================================
Files 434 434
Lines 15390 15390
Branches 3659 3659
=======================================
Hits 12782 12782
Misses 2385 2385
Partials 223 223 Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
4a89dc4
to
f74c6f1
Compare
e92b74a
to
4667fce
Compare
使用 bull 组件替代原有的 task,原有的 bull 和 cron 混在一起,不好隔离和升级。
related: #2090 #2246 #1958 #1742
功能
Queue 装饰器的能力和范围