@desmart/queue
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "@desmart/queue", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Message Queue client abstraction", | ||
@@ -9,9 +9,13 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha ./test/*.spec.js ./test/**/*.spec.js", | ||
"test": "DEBUG=desmart:queue mocha ./test/*.spec.js ./test/**/*.spec.js", | ||
"lint": "standard", | ||
"lint:fix": "standard --fix", | ||
"prepush": "npm run lint" | ||
"prepush": "npm run lint", | ||
"tag:patch": "npm version patch -m 'chore(package): bump version to %s'", | ||
"tag:minor": "npm version minor -m 'chore(package): bump version to %s'", | ||
"tag:major": "npm version major -m 'chore(package): bump version to %s'" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"chai-as-promised-compat": "^7.0.3", | ||
"husky": "^0.14.3", | ||
@@ -23,3 +27,5 @@ "mocha": "^4.0.1", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"debug": "^3.1.0" | ||
}, | ||
"standard": { | ||
@@ -26,0 +32,0 @@ "globals": [ |
@@ -5,2 +5,27 @@ # @desmart/queue | ||
<!-- TOC --> | ||
- [@desmart/queue](#desmartqueue) | ||
- [installation](#installation) | ||
- [example](#example) | ||
- [job](#job) | ||
- [creating new job](#creating-new-job) | ||
- [handlers](#handlers) | ||
- [pushing to queue backend](#pushing-to-queue-backend) | ||
- [listening to new jobs](#listening-to-new-jobs) | ||
- [connectors](#connectors) | ||
- [api](#api) | ||
- [middlewares](#middlewares) | ||
- [adding middleware](#adding-middleware) | ||
- [bundled middlewares](#bundled-middlewares) | ||
- [`autoCommit`](#autocommit) | ||
- [`maxAttempts`](#maxattempts) | ||
- [`debug`](#debug) | ||
- [development](#development) | ||
- [tests & linting](#tests--linting) | ||
- [general practices](#general-practices) | ||
- [issues & PR](#issues--pr) | ||
<!-- /TOC --> | ||
# installation | ||
@@ -16,3 +41,3 @@ | ||
const { manager, job } = require('@desmart/queue') | ||
const syncConnector = require('@desmart/queue/src/connector/syncConnector') | ||
const { syncConnector } = require('@desmart/queue/connector') | ||
@@ -105,2 +130,70 @@ const queue = manager(syncConnector()) | ||
# middlewares | ||
It's possible to extend behaviour of manager with middlewares. | ||
Middleware is a function which should accept `job` and `next` callback. It's triggered once a job is fetched from backend and redirected to handler. | ||
Through middleware it's possible to modify job (note that job is immutable), or do some other stuff. Don't forget to call `next` once you want to pass control to another middleware. | ||
Every middleware should (if possible) return the result of `next()`. Remember also that other middlewares may return a Promise so `async/await` may be useful here. | ||
## adding middleware | ||
```js | ||
const { manager, job } = require('@desmart/queue') | ||
const { autoCommit } = require('@desmart/queue/middleware') | ||
const { syncConnector } = require('@desmart/queue/connector') | ||
const queue = manager(syncConnector()) | ||
queue.use(autoCommit()) | ||
// each handle will be converted to terminating middleware - add them after all middlewares | ||
queue.handle('job', () => {}) | ||
``` | ||
## bundled middlewares | ||
Package comes with some bundled middlewares. They can be imported from `@desmart/queue/middleware` module. | ||
### `autoCommit` | ||
```js | ||
const { autoCommit } = require('@desmart/queue/middleware') | ||
queue.use(autoCommit({ | ||
exponential: true, | ||
maxDelay: 6 * 3600 | ||
})) | ||
``` | ||
Waits for job to finish and removes it from queue. If job failed it will be released back to queue. | ||
This will works only when **job handler returns a Promise**. | ||
Job is released with exponential delay. After first attempt it will be released without a delay, with second attempt it will be delayed by 5 seconds, later by 15 seconds and so on.. By defualt, after multiple fails, job will be delayed by 6 hours. | ||
Available options: | ||
* `exponential` (Boolean) [`true`] should failed job be released with exponential delay | ||
* `maxDelay` (Integer) [`21600`] maximum delay for failed jobs; used only when `exponential == true` | ||
### `maxAttempts` | ||
```js | ||
const { maxAttempts } = require('@desmart/queue/middleware') | ||
queue.use(maxAttempts(max = 3)) | ||
``` | ||
Removes automatically a job which failed more than `max` times. | ||
### `debug` | ||
```js | ||
const { debug } = require('@desmart/queue/middleware') | ||
queue.use(debug()) | ||
``` | ||
Small utility which uses [debug](https://github.com/visionmedia/debug) to print information about processed job status. | ||
# development | ||
@@ -107,0 +200,0 @@ |
@@ -1,9 +0,11 @@ | ||
const createHandlers = require('./handlers') | ||
const syncConnector = require('./connector/syncConnector') | ||
const createMiddelware = require('./middleware') | ||
const manager = ( | ||
connector = syncConnector(), | ||
handlers = createHandlers() | ||
middleware = createMiddelware() | ||
) => { | ||
connector.onJob(handlers.dispatchJob) | ||
connector.onJob(job => { | ||
middleware.run(job) | ||
}) | ||
@@ -15,8 +17,12 @@ return { | ||
listen (queue = 'default', options = {}) { | ||
return connector.listen(queue, options) | ||
}, | ||
handle (jobName, fn) { | ||
handlers.bind(jobName, fn) | ||
return middleware.addHandler(jobName, fn) | ||
}, | ||
listen (queue = 'default', options = {}) { | ||
return connector.listen(queue, options) | ||
use (fn) { | ||
return middleware.use(fn) | ||
} | ||
@@ -23,0 +29,0 @@ } |
@@ -5,3 +5,3 @@ const chai = require('chai') | ||
const syncConnector = require('../../src/connector/syncConnector') | ||
const { syncConnector } = require('../../connector') | ||
const job = require('../../src/job') | ||
@@ -8,0 +8,0 @@ |
@@ -68,2 +68,69 @@ const chai = require('chai') | ||
}) | ||
describe('pipeline', () => { | ||
it('allows to add jobs middleware', () => { | ||
const spy = sinon.spy() | ||
const testJob = job.of('job.name') | ||
const connector = dummyConnector() | ||
const queue = manager(connector) | ||
queue.use(spy) | ||
connector.stubJob(testJob) | ||
expect(spy).to.have.been.calledOnce // eslint-disable-line | ||
expect(spy.getCall(0).args[0]).to.equal(testJob) | ||
}) | ||
it('passes modified job to handler', () => { | ||
const handler = sinon.spy() | ||
const connector = dummyConnector() | ||
const modifiedJob = job.of('foo') | ||
const queue = manager(connector) | ||
queue.use((job, next) => { | ||
next(modifiedJob) | ||
}) | ||
queue.handle('foo', handler) | ||
connector.stubJob(job.of('job.name')) | ||
expect(handler).to.have.been.calledWith(modifiedJob) | ||
}) | ||
it('should stop without calling next()', () => { | ||
const handler = sinon.spy() | ||
const connector = dummyConnector() | ||
const queue = manager(connector) | ||
queue.use(_ => {}) | ||
queue.handle('foo', handler) | ||
connector.stubJob(job.of('job.name')) | ||
expect(handler).not.to.have.been.called // eslint-disable-line | ||
}) | ||
it('should call multiple middlewares', () => { | ||
const handler = sinon.spy() | ||
const connector = dummyConnector() | ||
const fake = (job, next) => next(job) | ||
const middlewares = [ | ||
sinon.stub().callsFake(fake), | ||
sinon.stub().callsFake(fake) | ||
] | ||
const queue = manager(connector) | ||
middlewares.forEach(queue.use) | ||
queue.handle('job.name', handler) | ||
connector.stubJob(job.of('job.name')) | ||
expect(middlewares[0]).to.have.been.calledOnce // eslint-disable-line | ||
expect(middlewares[1]).to.have.been.calledOnce // eslint-disable-line | ||
expect(handler).to.have.been.calledOnce // eslint-disable-line | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72437
25
528
214
1
7
+ Addeddebug@^3.1.0
+ Addeddebug@3.2.7(transitive)
+ Addedms@2.1.3(transitive)