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

feat: add new bull component #2275

Merged
merged 22 commits into from
Oct 7, 2022
Merged

feat: add new bull component #2275

merged 22 commits into from
Oct 7, 2022

Conversation

czy88840616
Copy link
Member

@czy88840616 czy88840616 commented Aug 29, 2022

使用 bull 组件替代原有的 task,原有的 bull 和 cron 混在一起,不好隔离和升级。

related: #2090 #2246 #1958 #1742

功能

  • 框架化,支持中间件,错误处理器
  • 集成 @types/bull 定义
  • Queue 装饰器的能力和范围
  • 动态添加管理 Queue
  • 手动执行,暂停,取消任务
  • 重复执行任务
  • 启动自动清理
  • 支持 bull-ui

@gitpod-io
Copy link

gitpod-io bot commented Aug 29, 2022

Copy link
Member

@stone-jin stone-jin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最大的设计点: 定时任务也有点像Controller里面的get、post方法一样,比如需求发现要加一个定时任务,那我偏向直接一个类的方法函数,然后在这个函数上面加一个定时任务的装饰器类。

## 安装方法

```bash
tnpm install @midwayjs/bull -S
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是tnpm

export const bull = {
defaultQueueOptions: {
redis: `redis://127.0.0.1:6379`,
prefix: 'midway-task',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处我发现,还是默认用: prefix: '{midway-task}' 这样用户不会碰到问题

定义一个任务处理器

```typescript
@Processor('test')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里还需要用@provide吗?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用了。

定时执行任务:

```typescript
@Processor('test', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改造,感觉脱离原本的模式了? 原来我一个类可以有多个任务,这样改,一个类只能一个任务了吗?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议不要这样改,比如我一个类里面,想要让某个地方定时执行,我本来只要在这个方法前面加一下装饰器,就好了。现在我得不停的抽离出这样一个类。

Copy link
Member Author

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}',
Copy link
Member

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于改用类的装饰器持保留意见。在实际业务场景中,还是原来支持类函数装饰器的方式比较好。

});
}
//
// export function Queue(queueOptions?: QueueOptions): ClassDecorator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不用的删除一下

}
}

protected async beforeStop() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法为什么是protected?

Copy link
Member Author

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 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块middleware 的写法readme里面好像没看到,不知道用户层这块的middleware怎么样的

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和普通中间件一样。

@codecov-commenter
Copy link

codecov-commenter commented Oct 1, 2022

Codecov Report

Base: 83.05% // Head: 83.05% // No change to project coverage 👍

Coverage data is based on head (1b952a1) compared to base (1b952a1).
Patch has no changes to coverable lines.

❗ Current head 1b952a1 differs from pull request most recent head e70ee90. Consider uploading reports for the commit e70ee90 to get more accurate results

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.
📢 Do you have feedback about the report comment? Let us know in this issue.

@czy88840616 czy88840616 merged commit 0a37b49 into main Oct 7, 2022
@czy88840616 czy88840616 deleted the new_bull_component branch October 7, 2022 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants