├── .gitignore ├── LICENSE ├── README.md ├── nested-job-queue.js ├── package.json └── simple-job-queue.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Prateek Bhatt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-job-queue 2 | =============== 3 | 4 | Implementation of a nested asynchronous job queue in Node.js using [Kue](http://github.com/learnboost/kue) 5 | 6 | Find the full tutorial on creating a simple job queue at [medium.com/p/ffcfbc824b01](https://medium.com/p/ffcfbc824b01) 7 | 8 | A redis-server must be running on your system. 9 | 10 | Clone this repo: 11 | 12 | git clone https://github.com/prateekbhatt/node-job-queue 13 | 14 | Enter the root directory: 15 | 16 | cd node-job-queue 17 | 18 | Test a simple job queue 19 | 20 | node simple-job-queue.js -------------------------------------------------------------------------------- /nested-job-queue.js: -------------------------------------------------------------------------------- 1 | var kue = require('kue') 2 | , jobs = kue.createQueue() 3 | ; 4 | 5 | function parentJob (done){ 6 | var job = jobs.create('parent', { 7 | type: 'PARENT' 8 | }); 9 | job 10 | .on('complete', function (){ 11 | console.log('Job', job.id, 'of type', job.data.type, 'is done'); 12 | done(); 13 | }) 14 | .on('failed', function (){ 15 | console.log('Job', job.id, 'of type', job.data.type, 'has failed'); 16 | done(); 17 | }) 18 | job.save(); 19 | } 20 | 21 | function childJob (done){ 22 | var job = jobs.create('child', { 23 | type: 'CHILD' 24 | }); 25 | job 26 | .on('complete', function (){ 27 | console.log('Job', job.id, 'of type', job.data.type, 'is done'); 28 | done(); 29 | }) 30 | .on('failed', function (){ 31 | console.log('Job', job.id, 'of type', job.data.type, 'has failed'); 32 | done(); 33 | }) 34 | job.save(); 35 | } 36 | 37 | jobs.process('parent', function (job, done){ 38 | /* carry out all the parent job functions here */ 39 | childJob(done); 40 | }) 41 | 42 | jobs.process('child', function (job, done){ 43 | /* carry out all the child job functions here */ 44 | done(); 45 | }) 46 | 47 | parentJob(function(){ }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-job-queue", 3 | "description": "Implementation of a nested asynchronous job queue in Node.js", 4 | "author": { 5 | "name": "Prateek Bhatt" 6 | }, 7 | "version": "0.1.0", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/prateekbhatt/node-job-queue.git" 11 | }, 12 | "bugs": { 13 | "url": "https://github.com/prateekbhatt/node-job-queue/issues" 14 | }, 15 | "licenses": [ 16 | { 17 | "type": "MIT", 18 | "url": "https://github.com/prateekbhatt/node-job-queue/raw/master/LICENSE" 19 | } 20 | ], 21 | "dependencies": { 22 | "kue": "~0.6.2" 23 | }, 24 | "devDependencies": {}, 25 | "readmeFilename": "README.md" 26 | } 27 | -------------------------------------------------------------------------------- /simple-job-queue.js: -------------------------------------------------------------------------------- 1 | var kue = require('kue') 2 | , jobs = kue.createQueue() 3 | ; 4 | 5 | function newJob (name){ 6 | name = name || 'Default_Name'; 7 | var job = jobs.create('new job', { 8 | name: name 9 | }); 10 | 11 | job 12 | .on('complete', function (){ 13 | console.log('Job', job.id, 'with name', job.data.name, 'is done'); 14 | }) 15 | .on('failed', function (){ 16 | console.log('Job', job.id, 'with name', job.data.name, 'has failed'); 17 | }) 18 | 19 | job.save(); 20 | } 21 | 22 | jobs.process('new job', function (job, done){ 23 | /* carry out all the job function here */ 24 | done && done(); 25 | }); 26 | 27 | 28 | setInterval(function (){ 29 | newJob('Send_Email'); 30 | }, 3000); --------------------------------------------------------------------------------