mongoose-kue
v0.9.26
Published
mongoose plugin to run mongoose schema methods in background
Downloads
182
Maintainers
Readme
mongoose-kue
mongoose plugin to run mongoose schema methods in background.
Note!: It's highly advice to run worker(s) in separate process for better performance
Requirements
- NodeJS v10.0+
Install
$ npm install --save mongoose kue mongoose-kue
Usage
const mongoose = require('mongoose');
const { Schema } = mongoose;
const { plugin: runInBackground, worker } = require('mongoose-kue');
...
/* queue sendEmail instance method to run in background */
const job =
user.runInBackground({ method: 'sendSMS', to: ['255714000111'] });
/* queue sendEmail static method in background */
const job =
User.runInBackground({
method: 'sendEmail',
to: ['[email protected]', '[email protected]']
});
/* ADVICED: in separate process start processing */
worker.start();
...
Options
Plugin
const { plugin: runInBackground } = require('mongoose-kue');
mongoose.plugin(runInBackground, {
name: 'mongoose',
prefix: 'q',
MONGODB_URI: process.env.MONGODB_URI,
redis: (process.env.REDIS_URL || {
port: 1234,
host: '10.0.50.20',
auth: 'password',
db: 3
})
});
...
name
- Name of the worker queue to process background work,MONGODB_URI
- Valid mongodb uri,attempts
- Failure Attemptsbackoff
- Failure Backoff- All applicable kue connection settings
Worker
Note!: I highly recommend to run this in separate process
const mongoose = require('mongoose');
const { worker } = require('mongoose-kue');
/* load and register your models */
...
/* start worker queue to process in background */
worker.start({
name: 'mongoose',
concurrency: 10,
prefix: 'q',
MONGODB_URI: process.env.MONGODB_URI,
redis: (process.env.REDIS_URL || {
port: 1234,
host: '10.0.50.20',
auth: 'password',
db: 3
})
});
name
- Name of the worker queue to process background work,MONGODB_URI
- Valid mongodb uri,concurrency
- Processing Concurrency. Default to 10,timeout
- Graceful shutdown delay,- All applicable kue connection settings
Environment
KUE_NAME=mongoose
KUE_JOB_TYPES=mongoose
KUE_TIMEOUT=5000
KUE_CONCURRENCY=10
KUE_MAX_ATTEMPTS=3
KUE_PRIORITY=normal
KUE_JOB_EVENTS=false
KUE_REMOVE_ON_COMPLETE=true
KUE_REDIS_URL=redis://127.0.0.1:3000
KUE_HTTP_PORT=5000
KUE_HTTP_USERNAME=kue
KUE_HTTP_PASSWORD=kue
REDIS_URL=redis://127.0.0.1:3000
References
Testing
Clone this repository
Install all development dependencies
$ npm install
- Then run test
$ npm test
Contribute
It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
Licence
The MIT License (MIT)
Copyright (c) lykmapipo & Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.