Commit 7bfb26e46b49196377f018d1aa46207286464a99

Authored by Adhidarma Hadiwinoto
1 parent c59536643e
Exists in master

Fix warn on error

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

1 const MODULE_NAME = 'TRANSPORT'; 1 const MODULE_NAME = 'TRANSPORT';
2 const SLEEP_BEFORE_TERMINATE_ON_ERROR_MS = 5 * 1000; 2 const SLEEP_BEFORE_TERMINATE_ON_ERROR_MS = 5 * 1000;
3 3
4 const bot = require('simple-xmpp'); 4 const bot = require('simple-xmpp');
5 const config = require('komodo-sdk/config'); 5 const config = require('komodo-sdk/config');
6 const logger = require('tektrans-logger'); 6 const logger = require('tektrans-logger');
7 const messagingService = require('komodo-center-messaging-client-lib'); 7 const messagingService = require('komodo-center-messaging-client-lib');
8 const uniqid = require('uniqid'); 8 const uniqid = require('uniqid');
9 9
10 const customPing = require('./custom-ping'); 10 const customPing = require('./custom-ping');
11 const killOnIdle = require('./kill-on-idle'); 11 const killOnIdle = require('./kill-on-idle');
12 const sender = require('./sender'); 12 const sender = require('./sender');
13 13
14 let isReady; 14 let isReady;
15 15
16 bot.on('online', (data) => { 16 bot.on('online', (data) => {
17 logger.info(`XMPP transport connected, JID: ${data.jid.user}`); 17 logger.info(`XMPP transport connected, JID: ${data.jid.user}`);
18 18
19 sender.init(bot); 19 sender.init(bot);
20 20
21 if (config.custom_ping) { 21 if (config.custom_ping) {
22 customPing.setBot(bot); 22 customPing.setBot(bot);
23 } 23 }
24 24
25 bot.getRoster(); 25 bot.getRoster();
26 26
27 setTimeout( 27 setTimeout(
28 () => { 28 () => {
29 isReady = true; 29 isReady = true;
30 30
31 logger.verbose('Transport is ready'); 31 logger.verbose('Transport is ready');
32 32
33 if (messagingService.onOnline) { 33 if (messagingService.onOnline) {
34 messagingService.onOnline({ 34 messagingService.onOnline({
35 me: config.username, 35 me: config.username,
36 }); 36 });
37 } 37 }
38 }, 38 },
39 39
40 config.warming_up_ms || (30 * 1000), 40 config.warming_up_ms || (30 * 1000),
41 ); 41 );
42 }); 42 });
43 43
44 bot.on('chat', (partner, msg) => { 44 bot.on('chat', (partner, msg) => {
45 if (!msg || !msg.trim()) { 45 if (!msg || !msg.trim()) {
46 return; 46 return;
47 } 47 }
48 48
49 if ((partner || '').toLowerCase() === config.username.toLowerCase().replace(/\/.*$/, '')) { 49 if ((partner || '').toLowerCase() === config.username.toLowerCase().replace(/\/.*$/, '')) {
50 return; 50 return;
51 } 51 }
52 52
53 killOnIdle.touch(); 53 killOnIdle.touch();
54 54
55 const xid = uniqid(); 55 const xid = uniqid();
56 56
57 if (!isReady) { 57 if (!isReady) {
58 if (!customPing.isPingMessage(msg) && !customPing.isPongMessage(msg)) { 58 if (!customPing.isPingMessage(msg) && !customPing.isPongMessage(msg)) {
59 logger.warn('Warming up is not finished yet, ignoring message', { 59 logger.warn('Warming up is not finished yet, ignoring message', {
60 xid, me: config.username, partner, msg, 60 xid, me: config.username, partner, msg,
61 }); 61 });
62 } 62 }
63 return; 63 return;
64 } 64 }
65 65
66 if (msg.toLowerCase().indexOf('error') === 0) { 66 if (msg.toLowerCase().indexOf('error') === 0) {
67 return; 67 return;
68 } 68 }
69 69
70 if (msg.toLowerCase().indexOf('perintah salah') === 0) { 70 if (msg.toLowerCase().indexOf('perintah salah') === 0) {
71 return; 71 return;
72 } 72 }
73 73
74 if (msg.toLowerCase().indexOf('pembelian ') === 0) { 74 if (msg.toLowerCase().indexOf('pembelian ') === 0) {
75 return; 75 return;
76 } 76 }
77 77
78 if (customPing.isPongMessage(msg)) { 78 if (customPing.isPongMessage(msg)) {
79 if (config.custom_ping && config.custom_ping.verbose) { 79 if (config.custom_ping && config.custom_ping.verbose) {
80 logger.verbose(`${MODULE_NAME} 70CDD087: Got PONG message`, { 80 logger.verbose(`${MODULE_NAME} 70CDD087: Got PONG message`, {
81 xid, 81 xid,
82 partner, 82 partner,
83 msg, 83 msg,
84 }); 84 });
85 } 85 }
86 return; 86 return;
87 } 87 }
88 88
89 if (customPing.isPingMessage(msg)) { 89 if (customPing.isPingMessage(msg)) {
90 if (customPing.isAllowedPartner(partner)) { 90 if (customPing.isAllowedPartner(partner)) {
91 if (config.custom_ping && config.custom_ping.verbose) { 91 if (config.custom_ping && config.custom_ping.verbose) {
92 logger.verbose(`${MODULE_NAME} ED8C8786: Processing PING message`, { 92 logger.verbose(`${MODULE_NAME} ED8C8786: Processing PING message`, {
93 xid, 93 xid,
94 partner, 94 partner,
95 msg, 95 msg,
96 }); 96 });
97 } 97 }
98 98
99 customPing.sendPong(xid, partner, msg); 99 customPing.sendPong(xid, partner, msg);
100 } 100 }
101 101
102 return; 102 return;
103 } 103 }
104 104
105 logger.info('Incoming message via XMPP transport', { 105 logger.info('Incoming message via XMPP transport', {
106 xid, me: config.username, partner, msg, 106 xid, me: config.username, partner, msg,
107 }); 107 });
108 108
109 if (messagingService && messagingService.onIncomingMessage) { 109 if (messagingService && messagingService.onIncomingMessage) {
110 messagingService.onIncomingMessage( 110 messagingService.onIncomingMessage(
111 { 111 {
112 me: config.username, 112 me: config.username,
113 partner, 113 partner,
114 msg: msg.trim(), 114 msg: msg.trim(),
115 xid, 115 xid,
116 }, 116 },
117 ); 117 );
118 } 118 }
119 }); 119 });
120 120
121 bot.on('error', (err) => { 121 bot.on('error', (err) => {
122 const xid = uniqid(); 122 const xid = uniqid();
123 123
124 logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, { 124 logger.warn(`${MODULE_NAME} F2E53C12: Error detected.`, {
125 xid, 125 xid,
126 eCode: err.code, 126 eCode: err.code,
127 eMessage: err.message, 127 eMessage: err.message || err,
128 }); 128 });
129 129
130 if (!config.do_not_terminate_on_error) { 130 if (!config.do_not_terminate_on_error) {
131 logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, { 131 logger.warn(`${MODULE_NAME} BA6C0C55: Terminating on error`, {
132 xid, 132 xid,
133 millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS, 133 millisecondSleepBeforeTerminate: SLEEP_BEFORE_TERMINATE_ON_ERROR_MS,
134 }); 134 });
135 135
136 setTimeout(() => { 136 setTimeout(() => {
137 process.exit(1); 137 process.exit(1);
138 }, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS); 138 }, SLEEP_BEFORE_TERMINATE_ON_ERROR_MS);
139 } 139 }
140 }); 140 });
141 141
142 function send(partner, msg, xid) { 142 function send(partner, msg, xid) {
143 sender.send(partner, msg, xid); 143 sender.send(partner, msg, xid);
144 } 144 }
145 145
146 bot.on('subscribe', (from) => { 146 bot.on('subscribe', (from) => {
147 logger.verbose(`Incoming subscribe request from ${from}`); 147 logger.verbose(`Incoming subscribe request from ${from}`);
148 bot.acceptSubscription(from); 148 bot.acceptSubscription(from);
149 bot.subscribe(from); 149 bot.subscribe(from);
150 }); 150 });
151 151
152 function ping() { 152 function ping() {
153 const xid = uniqid(); 153 const xid = uniqid();
154 if (isReady) sender.send(config.username, `PING ${xid}`, xid, true); 154 if (isReady) sender.send(config.username, `PING ${xid}`, xid, true);
155 } 155 }
156 156
157 function init() { 157 function init() {
158 messagingService.setTransport(exports); 158 messagingService.setTransport(exports);
159 159
160 if (!killOnIdle.getDisabled()) { 160 if (!killOnIdle.getDisabled()) {
161 killOnIdle.init(); 161 killOnIdle.init();
162 } 162 }
163 163
164 bot.connect({ 164 bot.connect({
165 jid: config.username, 165 jid: config.username,
166 password: config.password, 166 password: config.password,
167 host: config.xmpp_host, 167 host: config.xmpp_host,
168 }); 168 });
169 169
170 setInterval( 170 setInterval(
171 ping, 171 ping,
172 config.ping_interval_ms || 25000, 172 config.ping_interval_ms || 25000,
173 ); 173 );
174 } 174 }
175 175
176 init(); 176 init();
177 177
178 exports.init = init; 178 exports.init = init;
179 exports.send = send; 179 exports.send = send;
180 180