# @midwayjs/task

> midway component for task and schedule

Latest version **3.6.0** (published 2022-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @midwayjs/task
pnpm add @midwayjs/task
yarn add @midwayjs/task
bun add @midwayjs/task
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.6.0 |
| Published | 2022-10-10 |
| First published | 2021-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 2 |
| Unpacked size | 21.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | mariodu, czy88840616, legendecas, echosoar, lellansin, lxxyx, stone-jin |
| Keywords | midway, IoC, task, plugin |

## Links

- npm: https://www.npmjs.com/package/@midwayjs/task
- npm.io page: https://npm.io/package/@midwayjs/task

## Dependencies (2)

- [bull](https://npm.io/package/bull.md) 4.10.0
- [cron](https://npm.io/package/cron.md) ^1.8.2

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 3.6.0 (latest) — 2022-10-10
- 2.14.0-beta.3 (beta2) — 2022-01-11
- 3.0.0-alpha.43 (alpha) — 2021-09-24
- 3.0.0-alpha.42 (alinode-stable) — 2021-09-23
- 3.0.0-alpha.2 (canary) — 2021-08-05
- 3.5.3 — 2022-09-25
- 3.5.1 — 2022-09-06
- 3.5.0 — 2022-08-29
- 3.4.13 — 2022-08-24
- 3.4.12 — 2022-08-20
- 3.4.11 — 2022-08-16
- 3.4.10 — 2022-08-12
- 3.4.9 — 2022-08-10
- 3.4.7 — 2022-08-01
- 3.4.6 — 2022-07-31
- … 117 more at https://npm.io/package/@midwayjs/task/versions

## README

# @midwayjs/task

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

## 安装方法

```bash
npm install @midwayjs/task -S
```

## 使用方法

在 Configuration.ts 导入子组件

```typescript

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

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

配置：

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

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

## 业务代码编写方式

分布式定时任务：

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

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

本地定时任务：

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

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

定时执行任务：

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

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

让用户定义任务

```typescript

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

  @Inject()
  service;

  async execute(params){
    console.log(params);
  }
}
```

```typescript
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)
```

---
_Source: https://npm.io/package/@midwayjs/task · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
