A remote worker system that uses AMQP
to coordinate jobs.
amqp-simple-pub-sub
— A library that simplifies use ofAMQP
based publishers and subscribers.
npm install amqp-delegate
const { makeWorker } = require('amqp-delegate')
const worker = makeWorker({
url: 'ampq://localhost:5672', // the default
name: 'the name of the worker', // required
task: async () => 'any pure async function', // required
onError: err => { // optional
console.error('A connection error happened', err) // or do something clever
}
onClose: () => { // optional
console.log('The connection has closed.') // or do something clever
}
})
// start it
worker.start().then(() => {
console.log('worker', worker.name, 'started')
})
// stop it
worker.stop().then(() => {
console.log('worker', worker.name, 'stopped')
})
const { makeDelegator } = require('amqp-delegate')
const delegator = makeWorker({
url: 'ampq://localhost:5672', // the default
onError: err => { // optional
console.error('A connection error happened', err) // or something clever
}
onClose: () => { // optional
console.log('The connection has closed.') // or something clever
}
})
delegator
.start()
.then(() => {
delegator.invoke('worker name', ...params)
console.log('job name', result)
})
.catch(err => {
console.error('worker name', err)
})
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)
})
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)
})
branch | status | coverage | Audit | notes |
---|---|---|---|---|
develop |
Work in progress | |||
main |
Latest stable release |
- NodeJS, 10.0+ (I use
nvm
to manage Node versions —brew install nvm
.) - Docker (Use Docker for Mac, not the homebrew version)
npm install
docker-compose up -d
Runs Rabbit MQ.
npm test
— runs the unit tests (does not needrabbitmq
)npm run test:unit:cov
— runs the unit tests with code coverage (does not needrabbitmq
)npm run test:integration
— runs the integration tests (needsrabbitmq
)
npm run lint
Please see the contributing notes.