amqp-delegate
A remote worker system that uses AMQP
to coordinate jobs.
![NPM](https://nodei.co/npm/amqp-delegate.png)
See Also
Usage
npm install amqp-delegate
Worker
const { makeWorker } = require('amqp-delegate')
const worker = makeWorker({
url: 'ampq://localhost:5672',
name: 'the name of the worker',
task: async () => 'any pure async function',
onError: err => {
console.error('A connection error happened', err)
}
onClose: () => {
console.log('The connection has closed.')
}
})
worker.start().then(() => {
console.log('worker', worker.name, 'started')
})
worker.stop().then(() => {
console.log('worker', worker.name, 'stopped')
})
Delegator
const { makeDelegator } = require('amqp-delegate')
const delegator = makeWorker({
url: 'ampq://localhost:5672',
onError: err => {
console.error('A connection error happened', err)
}
onClose: () => {
console.log('The connection has closed.')
}
})
delegator
.start()
.then(() => {
delegator.invoke('worker name', ...params)
console.log('job name', result)
})
.catch(err => {
console.error('worker name', err)
})
A concrete example
The worker
const { makeWorker } = require('amqp-delegate')
const task = (a, b) => new Promise(resolve => setTimeout(() => resolve(a + b), 10))
const worker = makeWorker({
name: 'adder',
task
})
worker
.start()
.then(() => {
process.on('SIGINT', () => {
worker.stop().then(() => {
process.exit(0)
})
})
})
.catch(err => {
console.error('caught', err)
})
The delegator
const { makeDelegator } = require('amqp-delegate')
const delegator = makeDelegator()
delegator
.start()
.then(() => delegator.invoke('adder', 10, 15))
.then(result => {
console.log('result', result)
})
.catch(err => {
console.error('caught', err)
})
Development
Branches
branch | status | coverage | Audit | notes |
---|
develop | ![CircleCI](https://circleci.com/gh/davesag/amqp-delegate/tree/develop.svg?style=svg) | ![codecov](https://codecov.io/gh/davesag/amqp-delegate/branch/develop/graph/badge.svg) | ![Vulnerabilities](https://snyk.io/test/github/davesag/amqp-delegate/develop/badge.svg) | Work in progress |
master | ![CircleCI](https://circleci.com/gh/davesag/amqp-delegate/tree/master.svg?style=svg) | ![codecov](https://codecov.io/gh/davesag/amqp-delegate/branch/master/graph/badge.svg) | ![Vulnerabilities](https://snyk.io/test/github/davesag/amqp-delegate/master/badge.svg) | Latest stable release |
Prerequisites
Initialisation
npm install
To Start the queue server for integration testing.
docker-compose up -d
Runs Rabbit MQ.
Test it
npm test
— runs the unit tests (does not need rabbitmq
)npm run test:unit:cov
— runs the unit tests with code coverage (does not need rabbitmq
)npm run test:integration
— runs the integration tests (needs rabbitmq
)
Lint it
npm run lint
Contributing
Please see the contributing notes.