333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const Subscription = require("egg").Subscription;
 
class autoTask extends Subscription {
  // 通过 schedule 属性来设置定时任务的执行间隔等配置
  static get schedule() {
    return {
      immediate: false,
      disable: true,
      interval: "6s", // 10秒钟间隔
      type: "worker" // 指定所有的 worker 都需要执行
    };
  }
 
  // subscribe 是真正定时任务执行时被运行的函数
  async subscribe() {
    this.doUrl();
  }
 
  async doUrl() {
    let log = this.ctx.getLogger("scheduleLogger");
    log.info("start-执行作业开始...");
    // // 执行
    let res = await this.ctx.curl("http://127.0.0.1:7001/api/task/start", {
      dataType: "json",
      timeout: 60 * 1000 // 1分钟超时
    });
    log.info("start-执行作业结束" + JSON.stringify(res.data));
 
    log.info("start-执行作业开始...");
    // // 执行
    res = await this.ctx.curl("http://127.0.0.1:7001/api/task/doStep", {
      dataType: "json",
      timeout: 60 * 1000 // 1分钟超时
    });
    log.info("start-执行作业结束" + JSON.stringify(res.data));
  }
}
 
module.exports = autoTask;