blob: 27ddc925cd484dd31c90a6d65af9a4c904adbc10 [file] [log] [blame]
Zack Williamsea89ebb2018-09-07 12:37:36 -07001/*
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 Scandolof1523582018-09-28 18:03:09 +020028 const connect = () => {
Zack Williamsea89ebb2018-09-07 12:37:36 -070029
30 try {
Matteo Scandolof1523582018-09-28 18:03:09 +020031 logger.log('debug',`Using librdkafka version: ${kafka.librdkafkaVersion}, kafka features: '${kafka.features}'`);
32 logger.log('debug',`Connecting to broker: ${config.kafka_bootstrap_servers}`);
Zack Williamsea89ebb2018-09-07 12:37:36 -070033
Matteo Scandolof1523582018-09-28 18:03:09 +020034 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 Scandolo1116b972018-12-18 09:33:59 -080048 logger.log('info', 'Failed to connect to kafka, reconnecting in 5 sec')
49 logger.log('debug', err);
50 setTimeout(connect, 5 * 1000);
Matteo Scandolof1523582018-09-28 18:03:09 +020051 });
52
53 stream.consumer.on('event.error', function (err) {
Matteo Scandolo1116b972018-12-18 09:33:59 -080054 logger.log('info', 'Failed to connect to kafka, reconnecting in 5 sec')
55 logger.log('debug', err);
56 setTimeout(connect, 5 * 1000);
Matteo Scandolof1523582018-09-28 18:03:09 +020057 });
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 Scandolo1116b972018-12-18 09:33:59 -080089 catch(err) {
90 logger.log('warning', 'Failed to connect to kafka, reconnecting in 5 sec')
91 logger.log('debug', err);
Matteo Scandolof1523582018-09-28 18:03:09 +020092 setTimeout(connect, 5 * 1000);
Zack Williamsea89ebb2018-09-07 12:37:36 -070093 }
Matteo Scandolof1523582018-09-28 18:03:09 +020094 }
Zack Williamsea89ebb2018-09-07 12:37:36 -070095
Matteo Scandolof1523582018-09-28 18:03:09 +020096 connect()
Zack Williamsea89ebb2018-09-07 12:37:36 -070097
98})();