uci-base/examples/consumer2.js

83 lines
1.9 KiB
JavaScript

/*
simple generic consumer sending an ack
*
*/
import Base from '../src/base'
const delay = time => new Promise(res=>setTimeout(()=>res(),time))
let opts={name:'example-consumer-2', id:'example-consumer-2'}
let consumer = new Base(opts)
;
(async () => {
//
consumer.on('log',ev => {
switch (ev.level) {
// case 'trace':
// case 'debug':
case 'info':
case 'warning':
case 'error':
case 'fatal':
case 'testing':
console.log(ev.level.toUpperCase(),'\n',ev)
break
}
})
// example return processor using a namespace, will take precidence over default and root namespaces
consumer.cmds = {
reply: (packet) => {
// console.log('------REPLY--via cmds namespace----')
// console.log(packet)
},
error: (packet) => {
console.log('------Error--via cmds namespace----')
console.log(packet)
}
}
consumer.addNamespace('cmds','c') // comment this out to process via root namespace
consumer.registerSocket('to-example-socket-2','c','n',{name:'example-consumer-2', path:'named-pipe'})
consumer.ready.addObserver('example-socket:process')
consumer.on('example-socket:process', (ready,packet) => {
console.log('process state from socket', ready)
})
let res = await consumer.init()
if (res.error) {
console.log('errors during init')
process.kill(process.pid, 'SIGTERM')
}
consumer.ready.subscribe(async ready=>{
console.log('consumer 2 can be ready when socket process pushes it is ready', ready)
})
process.once('SIGINT', function() {
console.log('Caught interrupt signal')
consumer.removeAllListeners()
process.exit()
})
process.once('SIGUSR2', async () => {
consumer.removeAllListeners()
process.exit()
})
})().catch(err => {
console.error('FATAL: UNABLE TO START SYSTEM!\n',err)
consumer.removeAllListeners()
process.kill(process.pid, 'SIGTERM')
})