[eggjs/egg]service不执行

2025-11-04 8 views
3

这是定时任务:

"use strict";

const Subscription = require("egg").Subscription;

class GetProxyIPs extends Subscription {
  static get schedule() {
    return {
      interval: "30s",
      type: "all",
      immediate: false,
      disable: false
    };
  }

  async subscribe() {
    //http://35.245.208.185:3128
    console.log('start proxy');
    const result = await this.ctx.service.api.smth.proxy_ip_pool.getIPs();
    console.log(result);
  }

}

module.exports = GetProxyIPs;

这是service:

"use strict";

const Service = require("egg").Service;

class ProxyIPPoolService extends Service {
  async getIPs() {
    console.log('get ip');
    const result = await this.app.mysql.insert("ip", {
      address: "35.245.208.185",
      port: "3128",
      protocal: "http"
    });

    return result;
  }

  async checkIP(ip) {}

  async saveIP(ip) {}

  async deleteIP(ip) {}
}

module.exports=ProxyIPPoolService;

现在问题是:定时任务里的’start proxy‘可以打印出来,但是service里getIPs()里的’get ip‘,没有打印出来,应该是函数没有执行,请问一下这是什么问题?

回答

8

提供最小可复现仓库

7

这已经是全部的代码了,请看一下上面的代码写法有没有问题,我写了的其他一些service事可以执行的

7

这是代码目录

3

按上面的指南提供下最小仓库

4

service 名的下划线会被转换为驼峰,具体看文档

9

重新命名后可以执行了,但官方文档中,没有看到有关命名规范的说明

4

https://eggjs.org/zh-cn/basics/service.html#注意事项

https://eggjs.org/zh-cn/advanced/loader.html#文件加载规则

0

在新的文档里面会更明确的声明,还没发布完