2

I'm setting up a server in nodejs that uses kafka. In order to do that I'm using kafkaJS. The problem here is that I don't want the server powering down everytime someone sends a post or reads a value from kafka.

Up until now I've tried this to read data

const readData = async (receiver, stream_name) => {
  return new Promise((resolve, reject) => {
    consumer.connect()
    consumer.subscribe({ topic: stream_name })
      .catch(e => {
        console.log(stream_name)
        console.log('failed to subscribe to topic')
        console.log('err: ', e)
        reject(e)
      })
    consumer.run({
      eachMessage: async ({ topic, message }) => {
        receiver(`- ${topic} ${message.timestamp} ${message.key}#${message.value}`)
        resolve('ok')
      }
    })
      .then(e => {
        console.log('reading')
        resolve('read')
      })
      .catch(e => {
        console.log('messed up', e)
        reject('fail')
      })
    setTimeout(() => {
      console.log('egging')
      return 0
    }, 10000)
  })
}

and this to create data

const pushData = async payload => {
  return new Promise((resolve, reject) => {
    producer.send(payload)
            .then( e => {
              resolve(e)
            })
            .catch(e => {
              console.log('Error pushing data to kafka broker:\n', e)
              reject(e)
            })
  })
}

// run ({String topic, {String key, JSON value[]} messages} payload)
const putData = async payload => {
  console.log('Connecting to kafka server...')
  await producer.connect()
  const a = await pushData(payload)
  console.log('Data sent: ', a)
  await producer.disconnect()
  process.abort()
}

This code works well but it needs to be put down in order to exit the method. I wanted a solution in which I could either kill the process/thread where the kafka interface is executing or a regular exit of the method.

Any help is appreciated.

Jose Pinho
  • 33
  • 1
  • 6

0 Answers0