mywei1989 / egg-bus

🐣 用 egg 编写优雅的队列与事件
MIT License
51 stars 8 forks source link

Can not successfully load queue config #10

Closed windmemory closed 4 years ago

windmemory commented 4 years ago

I was not able to successfully load the queue config.

Then I found the reason is that the internal config load code will add prefix defined in the config to the queue name, then egg-bus will not be able to locate the correct config.

Example:

I have a job file inside job folder

// /app/job/schedule.ts
import { Job } from 'egg-bus';

export default class DemoJob extends Job {
  static get queue() {
    return 'schedule'; // 使用的队列名称
  }

  static get attempts() {
    return 5; // 重试次数
  }

  async run(data, job) {
    const { ctx } = this;
    ctx.logger.info(`Receive data: ${data}, job: ${job}`);
  }

  failed(data) {
    const { ctx } = this;
    ctx.logger.info(`Failed data: ${data}`);
  }
}

Here is the config file

config.bus = {
    debug: true, // Debug 模式下会打印更多日志信息
    concurrency: 1, // Bull 中队列处理的并发数:https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueprocess
    listener: {
      baseDir: 'listener',
      options: { // Bull Job 配置: https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueadd
        attempts: 5,
        backoff: {
          delay: 3000,
          type: 'fixed',
        },
      },
    },
    job: {
      options: { // Bull Job 配置: https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueadd
        attempts: 5,
        backoff: {
          delay: 3000,
          type: 'fixed',
        },
      },
    },
    bull: { // Bull 队列配置:https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queue
      redis: {
        host: 'localhost',
        port: 6379,
        db: 0,
      },
    },
    queues: {
      schedule: {
        limiter: {
          max: 1,
          duration: 1000,
        },
      },
    },
  };

Then the config won't work, there is no rateLimit for the queue.

I dig into the code a little, and print out the queue name when loading the config file, then I found it was bus:schedule, after I change the queue name in queues to bus:schedule, everything works fine. So I think it would be better to fix this, since this is not quite clear that the config needs to include the prefix.

windmemory commented 4 years ago

BTW, this project is well thought and well built, good work.

seekcx commented 4 years ago

You are right. I took a look at the code and confirmed the problem you said. I will fix it later. Thank you for your work.

seekcx commented 4 years ago

release 1.0.0-alpha.9.