Ver Fonte

定时任务

zs há 1 ano atrás
pai
commit
36c6470994
3 ficheiros alterados com 15 adições e 0 exclusões
  1. 1 0
      package.json
  2. 2 0
      src/configuration.ts
  3. 12 0
      src/service/schedule.service.ts

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
     "@midwayjs/logger": "^2.14.0",
     "@midwayjs/redis": "^3.11.3",
     "@midwayjs/swagger": "^3.11.3",
+    "@midwayjs/task": "^3.6.0",
     "@midwayjs/typegoose": "^3.11.3",
     "@midwayjs/validate": "^3.0.0",
     "@midwayjs/web": "^3.11.6",

+ 2 - 0
src/configuration.ts

@@ -6,6 +6,7 @@ import * as swagger from '@midwayjs/swagger';
 import * as jwt from '@midwayjs/jwt';
 import * as redis from '@midwayjs/redis';
 import * as axios from '@midwayjs/axios';
+import * as task from '@midwayjs/task'; // 导入模块
 import { IMidwayContainer } from '@midwayjs/core';
 import { join } from 'path';
 // freemidway组件项目
@@ -27,6 +28,7 @@ const axiosError = error => {
 };
 @Configuration({
   imports: [
+    task,
     FreeFrame,
     validate,
     jwt,

+ 12 - 0
src/service/schedule.service.ts

@@ -0,0 +1,12 @@
+import { Provide, Task } from '@midwayjs/decorator';
+
+@Provide()
+export class scheduleService {
+  // 例如下面是每分钟执行一次,并且是分布式任务
+  @Task({
+    repeat: { cron: '*/60 * * * * * *' },
+  })
+  async test() {
+    console.log('定时任务');
+  }
+}