3.6.0 • Published 2 years ago

@midwayjs/task v3.6.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@midwayjs/task

简介

midwayjs/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:6379`,
  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())
  }
}

让用户定义任务

@Queue()
@Provide()
export class HelloTask{

  @Inject()
  service;

  async execute(params){
    console.log(params);
  }
}
import { QueueService } from '@midwayjs/task';
@Provide()
export class UserTask{

  @Inject()
  service;

  @Inject()
  queueService: QueueService;

  async execute(params){
    // 3秒后触发分布式任务调度。
    const xxx = this.queueService.execute(HelloTask, params, {delay: 3000});
  }
}

其他

关于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)
3.6.0

2 years ago

3.5.3

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.13

2 years ago

3.4.0-beta.11

2 years ago

3.4.0-beta.12

2 years ago

3.4.0-beta.10

2 years ago

3.4.0-beta.7

2 years ago

3.4.0-beta.6

2 years ago

3.4.0-beta.5

2 years ago

3.4.0-beta.4

2 years ago

3.4.0-beta.9

2 years ago

3.4.0-beta.8

2 years ago

3.4.0-beta.3

2 years ago

3.4.0-beta.2

3 years ago

3.4.0-beta.1

3 years ago

3.4.0

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.1

2 years ago

3.4.10

2 years ago

3.4.11

2 years ago

3.4.12

2 years ago

3.3.14

3 years ago

3.3.15

2 years ago

3.4.7

2 years ago

3.4.6

2 years ago

3.4.5

2 years ago

3.4.9

2 years ago

3.3.9

3 years ago

3.3.6

3 years ago

3.3.12

3 years ago

3.2.2

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.6

3 years ago

3.1.7-alpha.0

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.3.5

3 years ago

3.3.4

3 years ago

3.3.2

3 years ago

3.0.13

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.1.5

3 years ago

2.14.0-beta

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.10

3 years ago

3.0.2

3 years ago

3.0.11

3 years ago

3.0.1

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.0

3 years ago

3.0.4-beta.1

3 years ago

3.0.0-beta.12

3 years ago

3.0.0-beta.13

3 years ago

3.0.0-beta.14

3 years ago

3.0.0-beta.15

3 years ago

3.0.0-beta.16

3 years ago

3.0.0-beta.17

3 years ago

2.14.0-beta.1

3 years ago

3.0.9

3 years ago

2.14.6

3 years ago

2.14.3

3 years ago

2.14.2

3 years ago

2.14.0-beta.2

3 years ago

2.14.0-beta.3

3 years ago

3.0.0-beta.10

3 years ago

3.0.0-beta.11

3 years ago

3.0.0-beta.1

3 years ago

3.0.0-beta.3

3 years ago

3.0.0-beta.2

3 years ago

3.0.0-beta.5

3 years ago

3.0.0-beta.4

3 years ago

3.0.0-beta.7

3 years ago

3.0.0-beta.6

3 years ago

3.0.0-beta.9

3 years ago

3.0.0-beta.8

3 years ago

2.14.0

3 years ago

2.13.4

3 years ago

2.13.3

3 years ago

3.0.0-alpha.43

3 years ago

3.0.0-alpha.41

3 years ago

3.0.0-alpha.40

3 years ago

3.0.0-alpha.42

3 years ago

3.0.0-alpha.37

3 years ago

3.0.0-alpha.36

3 years ago

3.0.0-alpha.28

3 years ago

2.13.2

3 years ago

2.13.1

3 years ago

2.13.0

3 years ago

2.12.9

3 years ago

3.0.0-alpha.9

3 years ago

2.12.7

3 years ago

3.0.0-alpha.6

3 years ago

2.12.5

3 years ago

3.0.0-alpha.10

3 years ago

2.12.3

3 years ago

3.0.0-alpha.2

3 years ago

3.0.0-alpha.1

3 years ago

2.12.1

3 years ago

2.12.0

3 years ago

2.11.7-beta

3 years ago

2.11.7-beta.1

3 years ago

2.11.6

3 years ago

2.11.5

3 years ago

2.11.4

3 years ago

2.11.3

3 years ago

2.11.2

4 years ago

2.11.1

4 years ago

2.11.0

4 years ago

2.10.2-0.beta.1

4 years ago

2.10.2-0.beta

4 years ago

2.10.19

4 years ago

2.10.18

4 years ago

2.10.16

4 years ago

2.10.14

4 years ago

2.10.13

4 years ago

2.10.12

4 years ago

2.10.11

4 years ago

2.10.10

4 years ago

2.10.9

4 years ago

2.10.8

4 years ago

2.10.7

4 years ago

1.0.0

4 years ago