Existing messages in a queue causes a script to exist unexpectedly after processing
If starting a script to read a queue and there are already messages in the queue, then the script will process the existing messages and then unexpectedly exit. If there are no messages in the queue when the script starts, then the script will run indefinitely and process new messages as desired.
Reproducing the issue:
Consumer:
var PosixMQ = require('posix-mq');
var readChannel = new PosixMQ();
readChannel.on('messages', function() {
var n;
while ((n = this.shift(readbuf)) !== false) {
var msg = readbuf.toString('utf8', 0, n);
console.log('fromSensor: ' + msg)
}
});
readChannel.open({
name: '/fromSensor',
create: true,
mode: '0777',
maxmsgs: 10,
msgsize: 256
});
readbuf = new Buffer(readChannel.msgsize);
console.log('Readchannel opened');
console.log('--------------------------------------');
Producer:
var PosixMQ = require('posix-mq');
var keypress = require('keypress');
var writechannel = new PosixMQ();
writechannel.open({
name: '/fromSensor',
create: true,
mode: '0777',
maxmsgs: 10,
msgsize: 256
});
console.log('writechannel opened');
console.log('--------------------------------------');
keypress(process.stdin);
process.stdin.on('keypress', function (ch, key) {
switch(ch) {
case 'q':
process.exit()
break;
case 'w':
var msg = 'test-write';
writechannel.push(msg);
console.log('Send');
break;
}
});
process.stdin.setRawMode(true);
process.stdin.resume();