Git Product home page Git Product logo

egg-queue's Introduction

egg-queue

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-queue --save

Usage

// {app_root}/config/plugin.js
exports.queue = {
  enable: true,
  package: 'egg-queue',
};

Configuration

// {app_root}/config/config.default.js
exports.queue = {
     register: true, //是否注册事件(queue和主工程分开,可以两个docker运行)
     listen: 7002,  //kue默认提供了一个[UI](https://github.com/Automattic/kue/blob/master/Readme.md#user-interface)
     prefix: 'kuequeue',
     redis: {
        port: 6379, // Redis port
        host: 'myredis', // Redis host
        password: null,
        db: 2,
     },
};

see config/config.default.js for more detail.

Example

这里注册task可以使用两种形式

  1. 导出一个函数
// app/queue/send_sms.js

'use strict';

module.exports = {

  /**
     * 执行queue中的任务
     * @param {Object} ctx egg中的ctx,方便访问egg中的资源
     * @param {Object} job kue中的job
     * @param {Object} kueCtx kue中的ctx,可以控制任务的暂停,重启
     * @param {function}  done 任务完成成功done(),失败done(err)
     * @return {void}
     */
  async task(ctx, job, kueCtx, done) {
    console.log(job);
    done();
  },

  /**
     *添加到queue中的标志
     *可选,默认action为js文件名 send_sms.js => sendSms
     * @return {string} action
     */
  get action() {
    return 'send_sms';
  },

  /**
     *任务的并发量
     *可选,默认为1
     * @return {number} concurrency
     */
  get concurrency() {
    return 2;
  },
};
  1. 导出一个class
// app/queue/send_sms3.js
'use strict';

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

class SmsSendSubscription extends Subscription {

  /**
     * 执行queue中的任务,this 为egg的ctx
     * @param {Object} job kue中的job
     * @param {Object} kueCtx kue中的ctx,可以控制任务的暂停,重启
     * @param {function}  done 任务完成成功done(),失败done(err)
     * @return {void}
     */
  async subscribe(job, kueCtx, done) {
    setTimeout(() => {
      done();
    }, 5000);
  }


  /**
     *添加到queue中的标志
     *可选,默认action为js文件名 send_sms.js => sendSms
     * @return {string} action
     */
  static get action() {
    return 'send_sms3';
  }

  /**
     *任务的并发量
     *可选,默认为1
     * @return {number} concurrency
     */
  get concurrency() {
    return 2;
  }

}

module.exports = SmsSendSubscription;

发布task

    const { queue } = ctx.app;

    const res = await queue.create('send_sms3', {
      phone: '139********',
    }).save();

queue即为kue中的queue

Questions & Suggestions

Please open an issue here.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.