├── LICENSE ├── README.md ├── agent.js ├── app.js ├── config └── config.default.js ├── lib └── kue.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-kue 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | 5 | [npm-image]: https://img.shields.io/npm/v/egg-kue.svg?style=flat-square 6 | [npm-url]: https://npmjs.org/package/egg-kue 7 | 8 | A priority job queue backed by redis, built for eggjs. 9 | 10 | ## Install 11 | 12 | ```bash 13 | $ npm i egg-kue --save 14 | ``` 15 | 16 | ## Usage 17 | 18 | ```js 19 | // {app_root}/config/plugin.js 20 | exports.kue = { 21 | enable: true, 22 | package: 'egg-kue', 23 | }; 24 | ``` 25 | 26 | ## Configuration 27 | 28 | ```js 29 | // {app_root}/config/config.default.js 30 | 'use strict'; 31 | exports.kue = { 32 | app : true, 33 | agent : false, 34 | client: { 35 | queuePrefix: 'q', 36 | redis: { 37 | port: 6379, 38 | host: '127.0.0.1', 39 | auth: '', 40 | db: 3, 41 | // see https://github.com/mranney/node_redis#rediscreateclient 42 | options: { 43 | }, 44 | }, 45 | }, 46 | // clients: {} 47 | }; 48 | ``` 49 | 50 | see [config/config.default.js](config/config.default.js) for more detail. 51 | 52 | ## Example 53 | 54 | ```js 55 | app.kue.process('email', (job, done) => { 56 | // send email for this; 57 | email(job.data.to, done); 58 | }); 59 | 60 | app.kue.create('email', { 61 | title: 'welcome email for justin' 62 | , to: 'gdjyluxiaoyong@gmail.com' 63 | , template: 'welcome-email' 64 | }).save(); 65 | ``` 66 | ## License 67 | 68 | [MIT](LICENSE) 69 | -------------------------------------------------------------------------------- /agent.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = agent => { 4 | if (agent.config.kue.agent) require("./lib/kue.js")(agent); 5 | }; 6 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | if (app.config.kue.app) require("./lib/kue.js")(app); 5 | }; 6 | -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * egg-kue default config 5 | * @member Config#kue 6 | * @property {String} SOME_KEY - some description 7 | */ 8 | exports.kue = { 9 | app : true, 10 | agent : false, 11 | client: { 12 | queuePrefix: 'q', 13 | redis: { 14 | port: 6379, 15 | host: '127.0.0.1', 16 | auth: '', 17 | db: 3, 18 | // see https://github.com/mranney/node_redis#rediscreateclient 19 | options: {}, 20 | } 21 | }, 22 | // clients: {} 23 | }; 24 | -------------------------------------------------------------------------------- /lib/kue.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const kue = require('kue'); 5 | const Job = kue.Job; 6 | 7 | /** 8 | * 接受config和app两个参数,并返回初始化一个Kue实例 9 | * @param {Object} config 框架处理之后的配置项 10 | * @param {Application} app 当前应用 11 | * @return {Object} 返回创建的 Kue 实例 12 | */ 13 | 14 | module.exports = app => { 15 | app.addSingleton('kue', createDelayedJob); 16 | }; 17 | 18 | function createDelayedJob(config, app) { 19 | const { redis } = config; 20 | assert(redis && redis.host && redis.port && config.queuePrefix); 21 | 22 | const queue = kue.createQueue({ 23 | prefix: config.queuePrefix, 24 | redis, 25 | }); 26 | app.beforeStart(function* () { 27 | const count = yield new Promise((resolve, reject) => { 28 | Job.client.zcard(Job.client.getKey('jobs'), (err, count) => { 29 | if (err) { 30 | reject(err); 31 | } 32 | resolve(count); 33 | }); 34 | }); 35 | app.coreLogger.info(`[egg-kue] instance status OK, current job count is ${count}`); 36 | }); 37 | return queue; 38 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-kue", 3 | "version": "0.1.2", 4 | "description": "", 5 | "eggPlugin": { 6 | "name": "kue" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "eggPlugin", 11 | "egg-plugin" 12 | ], 13 | "dependencies": { 14 | "kue": "^0.11.6" 15 | }, 16 | "devDependencies": { 17 | "autod": "^2.8.0", 18 | "autod-egg": "^1.0.0", 19 | "egg": "^1.4.0", 20 | "egg-bin": "^3.4.0", 21 | "egg-ci": "^1.6.0", 22 | "egg-mock": "^3.7.0", 23 | "eslint": "^3.19.0", 24 | "eslint-config-egg": "^4.2.0", 25 | "supertest": "^3.0.0", 26 | "webstorm-disable-index": "^1.2.0" 27 | }, 28 | "engines": { 29 | "node": ">=6.0.0" 30 | }, 31 | "scripts": { 32 | "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", 33 | "test-local": "egg-bin test", 34 | "cov": "egg-bin cov", 35 | "lint": "eslint .", 36 | "ci": "egg-bin pkgfiles --check && npm run lint && npm run cov", 37 | "pkgfiles": "egg-bin pkgfiles", 38 | "autod": "autod" 39 | }, 40 | "files": [ 41 | "app.js", 42 | "agent.js", 43 | "config", 44 | "lib" 45 | ], 46 | "ci": { 47 | "version": "6, 8" 48 | }, 49 | "repository": { 50 | "type": "git", 51 | "url": "git+https://github.com/beyai/egg-kue.git" 52 | }, 53 | "homepage": "https://github.com/beyai/egg-kue#readme", 54 | "author": "chandre", 55 | "license": "MIT" 56 | } 57 | --------------------------------------------------------------------------------