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 midway task component #995

Merged
merged 5 commits into from
Apr 16, 2021
Merged

feat: add midway task component #995

merged 5 commits into from
Apr 16, 2021

Conversation

stone-jin
Copy link
Member

Midway-task

简介

Midway-task是为了能解决任务系列的模块,例如分布式定时任务、延迟任务调度。例如订单2小时后失效、每日定时的数据处理等工作。

安装方法

tnpm install @midwayjs/task -S

使用方法

在Configuration.ts导入子组件

import * as task from '@midwayjs/task';

@Configuration({
  imports: [task],
  importConfigs: [
    join(__dirname, 'config')
  ]
})
export class AutoConfiguration{
}

配置:

在 config.default.ts 文件中配置对应的模块信息:

export const taskConfig = {
  redis: `redis://127.0.0.1:32768`,
  prefix: 'midway-task',
  defaultJobOptions: {
    repeat: {
      tz: "Asia/Shanghai"
    }
  }
}

业务代码编写方式

分布式定时任务:

@Provide()
export class UserService {
  @Inject()
  helloService: HelloService;

  // 例如下面是每分钟执行一次,并且是分布式任务
  @Task({
    repeat: { cron: '* * * * *'}
  })
  async test(){
    console.log(this.helloService.getName())
  }
}

本地定时任务:

@Provide()
export class UserService {
  @Inject()
  helloService: HelloService;

  // 例如下面是每分钟执行一次
  @TaskLocal('* * * * * *')
  async test(){
    console.log(this.helloService.getName())
  }
}

定时执行任务:

@Provide()
export class UserService {
  @Inject()
  helloService: HelloService;

  // 例如下面是每分钟执行一次
  @TaskLocal('* * * * * *')
  async test(){
    console.log(this.helloService.getName())
  }
}

让用户定义任务

@Provide()
export class HelloTask{

  @Inject()
  service;


  Queue()
  async xxx(params){
    const xxx = 
  }

  @Queue()  // redis
  async data()
}

@controller()

/hello
console.log(this.xxx)
->调用扩容接口
this.queueService.add(HelloTask.xxx, params, {deplay: 1min}); //立马执行

其他

关于task任务的配置:

*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, optional)

@gitpod-io
Copy link

gitpod-io bot commented Apr 15, 2021

@stone-jin stone-jin requested a review from czy88840616 April 16, 2021 05:46
@stone-jin
Copy link
Member Author

#973

@stone-jin
Copy link
Member Author

#960

@czy88840616 czy88840616 changed the title chore: add midway task feat: add midway task component Apr 16, 2021
@czy88840616 czy88840616 merged commit befb81d into 2.x Apr 16, 2021
@czy88840616 czy88840616 deleted the feat/midway-task branch April 16, 2021 15:11
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.

2 participants