Zack Williams | ea89ebb | 2018-09-07 12:37:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | (function () { |
| 18 | 'use strict'; |
| 19 | |
| 20 | const socketIo = require('./websocket.js'); |
| 21 | const config = require('../config/config.js'); |
| 22 | const logger = require('../config/logger.js'); |
| 23 | const socket = socketIo.get(); |
| 24 | |
| 25 | // docs: https://github.com/Blizzard/node-rdkafka |
| 26 | var kafka = require('node-rdkafka'); |
| 27 | |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 28 | const connect = () => { |
Zack Williams | ea89ebb | 2018-09-07 12:37:36 -0700 | [diff] [blame] | 29 | |
| 30 | try { |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 31 | logger.log('debug',`Using librdkafka version: ${kafka.librdkafkaVersion}, kafka features: '${kafka.features}'`); |
| 32 | logger.log('debug',`Connecting to broker: ${config.kafka_bootstrap_servers}`); |
Zack Williams | ea89ebb | 2018-09-07 12:37:36 -0700 | [diff] [blame] | 33 | |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 34 | var stream = kafka.KafkaConsumer.createReadStream({ |
| 35 | 'metadata.broker.list': config.kafka_bootstrap_servers, |
| 36 | 'group.id': 'xos-ws', |
| 37 | 'socket.keepalive.enable': true, |
| 38 | 'enable.auto.commit': false |
| 39 | }, {}, { |
| 40 | topics: ['xos.gui_events'], |
| 41 | }); |
| 42 | |
| 43 | stream.on('ready', function () { |
| 44 | logger.log('info', 'Kafka connected'); |
| 45 | }); |
| 46 | |
| 47 | stream.on('error', function (err) { |
Matteo Scandolo | 1116b97 | 2018-12-18 09:33:59 -0800 | [diff] [blame] | 48 | logger.log('info', 'Failed to connect to kafka, reconnecting in 5 sec') |
| 49 | logger.log('debug', err); |
| 50 | setTimeout(connect, 5 * 1000); |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | stream.consumer.on('event.error', function (err) { |
Matteo Scandolo | 1116b97 | 2018-12-18 09:33:59 -0800 | [diff] [blame] | 54 | logger.log('info', 'Failed to connect to kafka, reconnecting in 5 sec') |
| 55 | logger.log('debug', err); |
| 56 | setTimeout(connect, 5 * 1000); |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 57 | }); |
| 58 | |
| 59 | stream.on('data', function (msg) { |
| 60 | logger.log('debug', `Topic: ${msg.topic}, Key: ${msg.key}, Timestamp: ${msg.timestamp}`); |
| 61 | |
| 62 | // strip diag messages |
| 63 | // NOTE: have to coerce to string (due to FFI?) |
| 64 | if (msg.key.toString() === 'Diag') { |
| 65 | return; |
| 66 | } |
| 67 | let msgobj; |
| 68 | |
| 69 | try { |
| 70 | // TODO find the user that needs to be notified for msg.object update |
| 71 | msgobj = JSON.parse(msg.value) |
| 72 | } |
| 73 | catch(e) { |
| 74 | // stringify the event if it is not JSON |
| 75 | msgobj = msg.value.toString() |
| 76 | } |
| 77 | |
| 78 | if (msgobj.deleted) { |
| 79 | logger.log('info', 'Remove on: ' + msg.key + ': ' + msg.value); |
| 80 | socket.emit('remove', {model: msg.key.toString(), msg: msgobj, deleted: true}); |
| 81 | } |
| 82 | else { |
| 83 | logger.log('info', 'Update on: ' + msg.key + ': ' + msg.value); |
| 84 | socket.emit('update', {model: msg.key.toString(), msg: msgobj}); |
| 85 | } |
| 86 | |
| 87 | }); |
| 88 | } |
Matteo Scandolo | 1116b97 | 2018-12-18 09:33:59 -0800 | [diff] [blame] | 89 | catch(err) { |
| 90 | logger.log('warning', 'Failed to connect to kafka, reconnecting in 5 sec') |
| 91 | logger.log('debug', err); |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 92 | setTimeout(connect, 5 * 1000); |
Zack Williams | ea89ebb | 2018-09-07 12:37:36 -0700 | [diff] [blame] | 93 | } |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 94 | } |
Zack Williams | ea89ebb | 2018-09-07 12:37:36 -0700 | [diff] [blame] | 95 | |
Matteo Scandolo | f152358 | 2018-09-28 18:03:09 +0200 | [diff] [blame] | 96 | connect() |
Zack Williams | ea89ebb | 2018-09-07 12:37:36 -0700 | [diff] [blame] | 97 | |
| 98 | })(); |